java 使用 MockitoJUnitRunner.class 而不是 SpringJUnit4ClassRunner.class
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31101693/
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
Using MockitoJUnitRunner.class instead of SpringJUnit4ClassRunner.class
提问by Ayaskant
I have a question about the usage of SpringJUnit4ClassRunner
. For pure Junits or Unit Test cases should we use Spring based annotations such as @Autowired
along with SpringJUnit4ClassRunner
or should we use only the MockitoJUnitRunner
instead with the @RunWith
annotation at the top of the Test class?
我有一个关于SpringJUnit4ClassRunner
. 对于纯 Junit 或单元测试用例,我们应该使用基于 Spring 的注解,例如@Autowired
withSpringJUnit4ClassRunner
还是只使用 theMockitoJUnitRunner
来代替@RunWith
Test 类顶部的注解?
I mean replacing
我的意思是更换
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:test-applicationContext.xml" })
with just
只是
@RunWith(MockitoJUnitRunner.class)
at the top of the class. It works for me.
在班级的顶端。这个对我有用。
In Junits we normally do not make any external calls such as calls to DB or call to some other web service. We have to mock these external calls using @Mock
annotations on this service objects. And then create a real object of the class that we are testing and that depends on these mocks. We can then use @InjectMocks
on the real object so that it will be injected with the mocked objects.
在 Junit 中,我们通常不会进行任何外部调用,例如调用 DB 或调用其他一些 Web 服务。我们必须使用@Mock
此服务对象上的注释来模拟这些外部调用。然后创建我们正在测试的类的真实对象,这取决于这些模拟。然后我们可以@InjectMocks
在真实对象上使用,以便将模拟对象注入它。
Example Service-A->Calls->Service-B->Calls->Service-C
示例 Service-A->Calls->Service-B->Calls->Service-C
While testing A we should mock Service B & while testing Service-B we should mock Service-C.
在测试 A 时,我们应该模拟 Service B,而在测试 Service-B 时,我们应该模拟 Service-C。
Some code Snippet
一些代码片段
@RunWith(MockitoJUnitRunner.class)
public class TestServiceA {
@Mock
B mockObj;
@InjectMocks
A realObj;
@Test
public void testServiceA() {
.....
.....
}
}
So, I feel for Unit test cases we need not rely on Spring container to provide us the instance of the class we are testing.
所以,我觉得对于单元测试用例,我们不需要依赖 Spring 容器来为我们提供我们正在测试的类的实例。
Please give your suggestions.
请提出您的建议。
Using SpringJUnit4ClassRunner.class instead of MockitoJUnitRunner.class
使用 SpringJUnit4ClassRunner.class 而不是 MockitoJUnitRunner.class
回答by Sven
If you try to simply unit test a class without the dependencies, like you describe, there is no need for the SpringJUnit4ClassRunner
. This runner is able to generate a complete Spring context with (mock) objects you can define in your (test) application context configuration. With this mechanism the SpringJUnit4ClassRunner
is much slower than the regular MockitoJUnitRunner
.
如果您尝试简单地对没有依赖项的类进行单元测试,就像您描述的那样,则不需要SpringJUnit4ClassRunner
. 这个运行器能够生成一个完整的 Spring 上下文,其中包含您可以在(测试)应用程序上下文配置中定义的(模拟)对象。使用这种机制,SpringJUnit4ClassRunner
比常规 慢得多MockitoJUnitRunner
。
The SpringJUnit4ClassRunner
is very powerful for integration test purposes.
的SpringJUnit4ClassRunner
是集成测试的目的非常强大。
I default start with the MockitoJUnitRunner
and if I reach the limits of this runner, for instance because I need to mock constructors, static methods or private variables, I switch to PowerMockJUnitRunner
. This is for me a last resort as it normally tells the code is ‘bad' and not written to be tested. Other runners are normally not necessary for isolated unit tests.
我默认先从MockitoJUnitRunner
如果我达到这个亚军的限制,比如因为我需要模拟构造函数,静态方法或私有变量,我切换到PowerMockJUnitRunner
。这对我来说是最后的手段,因为它通常告诉代码是“坏的”而不是为了测试而编写的。隔离单元测试通常不需要其他运行程序。
回答by ank
Building on Sven's answer, Let us say that you needed to test an assembly of classes while mocking out bits that go to the database, or call an external service, you would look to run the test with SpringJUnit4ClassRunner.
If you were trying to test a Single Java Class as a Unit, mocking out both the integration bits and local collaborators, then running the test with MockitoJUnitRunner is sufficient and faster as well.
基于 Sven 的回答,假设您需要在模拟进入数据库的位或调用外部服务的同时测试类的程序集,您将希望使用 SpringJUnit4ClassRunner 运行测试。
如果您尝试将单个 Java 类作为一个单元进行测试,模拟集成位和本地协作者,那么使用 MockitoJUnitRunner 运行测试就足够了,而且速度也更快。