Mockito,测试依赖注入依赖项的对象(Spring)?
我是使用 Mockito 的新手,并且试图了解一种对依赖注入依赖项的类进行单元测试的方法。我想要做的是创建依赖项的模拟对象,并使我正在测试的类使用这些对象,而不是由 Spring 注入的常规注入依赖项。我一直在阅读教程,但对如何做到这一点有点困惑。
我有一个我想像这样测试的类:
package org.rd.server.beans;
import org.springframework.beans.factory.annotation.Autowired;
public class TestBean1 {
@Autowired
private SubBean1 subBean1;
private String helloString;
public String testReturn () {
subBean1.setSomething("its working");
String something = subBean1.getSomething();
helloString = "Hello...... " + something;
return helloString;
}
然后我有一个我想用作模拟对象的类(而不是常规的 SubBean1
类,如下所示:
package org.rd.server.beans.mock;
public class SubBean1Mock {
private String something;
public String getSomething() {
return something;
}
public void setSomething(String something) {
this.something = something;
}
}
}
我只想尝试运行像这样的简单测试:
package test.rd.beans;
import org.rd.server.beans.TestBean1;
import junit.framework.*;
public class TestBean1Test extends TestCase
{
private TestBean1 testBean1;
public TestBean1Test(String name)
{
super(name);
}
public void setUp()
{
testBean1 = new TestBean1();
// Somehow inject the mock dependency SubBean1Mock ???
}
public void test1() {
assertEquals(testBean1.testReturn(),"working");
}
}
我认为必须有一些相当简单的方法来做到这一点,但我似乎无法理解这些教程,因为我还没有上下文来理解他们正在做/解释的所有事情(如果有人可以摆脱)。对此我将不胜感激。
I'm new to using Mockito and am trying to understand a way to make a unit test of a class that relies on injected dependencies. What I want to do is to create mock objects of the dependencies and make the class that I am testing use those instead of the regular injected dependencies that would be injected by Spring. I have been reading tutorials but am a bit confused on how to do this.
I have one the class I want to test like this:
package org.rd.server.beans;
import org.springframework.beans.factory.annotation.Autowired;
public class TestBean1 {
@Autowired
private SubBean1 subBean1;
private String helloString;
public String testReturn () {
subBean1.setSomething("its working");
String something = subBean1.getSomething();
helloString = "Hello...... " + something;
return helloString;
}
Then I have the class that I want to use as a mock object (rather than the regular SubBean1
class, like below:
package org.rd.server.beans.mock;
public class SubBean1Mock {
private String something;
public String getSomething() {
return something;
}
public void setSomething(String something) {
this.something = something;
}
}
}
I just want to try running a simple test like this:
package test.rd.beans;
import org.rd.server.beans.TestBean1;
import junit.framework.*;
public class TestBean1Test extends TestCase
{
private TestBean1 testBean1;
public TestBean1Test(String name)
{
super(name);
}
public void setUp()
{
testBean1 = new TestBean1();
// Somehow inject the mock dependency SubBean1Mock ???
}
public void test1() {
assertEquals(testBean1.testReturn(),"working");
}
}
I figure there must be some fairly simple way to do this but I can't seem to understand the tutorials as I don't have the context yet to understand everything they are doing / explaining. If anyone could shed some light on this I would appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 Mockito,则可以通过调用 Mockito 的静态模拟方法来创建模拟。然后,您可以将模拟传递给您要测试的类。您的设置方法将如下所示:
然后,您可以将适当的行为添加到您的模拟对象中,以用于您尝试使用 Mockito 的静态 when 方法进行测试的任何内容,例如:
If you're using Mockito you create mocks by calling Mockito's static mock method. You can then just pass in the mock to the class you're trying to test. Your setup method would look something like this:
You can then add the appropriate behavior to your mock objects for whatever you're trying to test with Mockito's static when method, for example:
在 Mockito 中,您并不真正要创建新的“模拟”实现,而是通过告诉 Mockito 在调用方法时返回什么来模拟注入依赖项的接口上的方法。
我使用 Mockito 编写了 Spring MVC 控制器的测试,并像对待任何其他 java 类一样对待它。我能够模拟出我拥有的各种其他 Spring bean,并使用 Spring 的 ReflectionTestUtils 来注入这些 bean,以传入基于 Mockito 的值。我早在二月份就在我的博客中写过这件事。它具有测试类的完整源代码和控制器的大部分源代码,因此将内容放在这里可能太长了。
http://digitaljoel.nerd-herders。 com/2011/02/05/mock-testing-spring-mvc-controller/
In Mockito you aren't really going to create new "mock" implementations, but rather you are going to mock out the methods on the interface of the injected dependency by telling Mockito what to return when the method is called.
I wrote a test of a Spring MVC Controller using Mockito and treated it just like any other java class. I was able to mock out the various other Spring beans I had and inject those using Spring's ReflectionTestUtils to pass in the Mockito based values. I wrote about it in my blog back in February. It has the full source for the test class and most of the source from the controller, so it's probably too long to put the contents here.
http://digitaljoel.nerd-herders.com/2011/02/05/mock-testing-spring-mvc-controller/
我在尝试为稍微复杂的情况设置一些模拟时偶然发现了这个线程,并认为我应该为后代分享我的结果。
我的情况类似,因为我需要模拟依赖项,但我也想模拟我正在测试的类上的一些方法。这就是解决方案:
请注意,“dependantService”需要与您在控制器上命名的服务实例相匹配。如果不匹配,反射将找不到它并为您注入模拟。
这种方法允许默认模拟控制器上的所有方法,然后您可以具体调用您想要使用真实方法的哪个方法。然后使用反射来设置相应模拟对象所需的任何依赖项。
希望这对某人有所帮助,因为它困扰了我一段时间。
I stumbled on this thread while trying to set up some mocks for a slightly more complicated situation and figured I'd share my results for posterity.
My situation was similar in the fact that I needed to mock dependencies, but I also wanted to mock some of the methods on the class I was testing. This was the solution:
Note that "dependantService" needs to match whatever you have named the instance of the service on your controller. If that doesn't match the reflection will not find it and inject the mock for you.
This approach allows all the methods on the controller to be mocked by default, then you can specifically call out which method you want to use the real one. Then use the reflection to set any dependencies needed with the respective mock objects.
Hope this helps someone down the road as it stumped me for a while.