java Mockito,测试依赖注入依赖项(Spring)的对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5767936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito, Testing an object that relies on injected dependencies (Spring)?
提问by Rick
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.
我是使用 Mockito 的新手,并试图了解一种对依赖注入依赖项的类进行单元测试的方法。我想要做的是创建依赖项的模拟对象,并使我正在测试的类使用这些对象,而不是由 Spring 注入的常规注入依赖项。我一直在阅读教程,但对如何做到这一点有点困惑。
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:
然后我有我想用作模拟对象的类(而不是常规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;
}
}
}
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.
我认为必须有一些相当简单的方法来做到这一点,但我似乎无法理解教程,因为我还没有上下文来理解他们正在做/解释的一切。如果有人能对此有所了解,我将不胜感激。
采纳答案by Ed Kurowski
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:
如果您使用 Mockito,您可以通过调用 Mockito 的静态模拟方法来创建模拟。然后,您可以将模拟传递给您要测试的类。您的设置方法如下所示:
testBean1 = new TestBean1();
SubBean1 subBeanMock = mock(SubBean1.class);
testBean1.setSubBean(subBeanMock);
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 的静态 when 方法进行测试,例如:
when(subBeanMock.getSomething()).thenReturn("its working");
回答by digitaljoel
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.
在 Mockito 中,您并不是真的要创建新的“模拟”实现,而是要通过告诉 Mockito 在调用方法时返回什么来模拟注入依赖项的接口上的方法。
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.
我使用 Mockito 编写了一个 Spring MVC 控制器的测试,并像对待任何其他 Java 类一样对待它。我能够模拟我拥有的各种其他 Spring bean,并使用 Spring 的 ReflectionTestUtils 注入它们以传入基于 Mockito 的值。我在二月份的博客中写过它。它有测试类的完整源代码和控制器的大部分源代码,所以把内容放在这里可能太长了。
http://digitaljoel.nerd-herders.com/2011/02/05/mock-testing-spring-mvc-controller/
http://digitaljoel.nerd-herders.com/2011/02/05/mock-testing-spring-mvc-controller/