java mockito 中@injectMocks 和@Autowired 用法的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25893247/
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
Difference between @injectMocks and @Autowired usage in mockito?
提问by Sai Srinadh Kurra
When I was writing test case using the Mockito and Junit, I was using the @InjectMocks
for the class to be tested. In other parts of project, I also see @Autowired
is being used for the class to be tested.
当我使用 Mockito 和 Junit 编写测试用例时,我使用的@InjectMocks
是要测试的类。在项目的其他部分,我也看到@Autowired
正在用于要测试的类。
When can I use @InjectMocks
and @Autowired
? What is the difference between two when we are trying to use them with class to be tested ?
我什么时候可以使用@InjectMocks
and @Autowired
?当我们尝试将它们与要测试的类一起使用时,两者有什么区别?
采纳答案by Mark Peters
@InjectMocks
is a Mockito mechanism for injecting declared fields in the testclass into matching fields in the class under test. It doesn't require the class under test to be a Spring component.
@InjectMocks
是一个机构的Mockito在注入声明的字段测试类成匹配字段中的类下测试。它不要求被测类是 Spring 组件。
@Autowired
is Spring's annotation for autowiring a bean into a production, non-test class.
@Autowired
是 Spring 的注释,用于将 bean 自动装配到生产、非测试类中。
If you wanted to leverage the @Autowired
annotations in the class under test, another approach would be to use springockitowhich allows you to declare mock beans so that they will be autowired into the class under test the same way that Spring would autowire the bean. But typically that's not necessary.
如果您想利用@Autowired
被测类中的注释,另一种方法是使用springockito,它允许您声明模拟 bean,以便它们将自动装配到被测类中,就像 Spring 自动装配 bean 一样。但通常这不是必需的。
回答by luboskrnac
@InjectMocks
annotation tells to Mockito to inject all mocks (objects annotated by @Mock
annotation) into fields of testing object. Mockito uses Reflection for this.
@InjectMocks
注释告诉 Mockito 将所有模拟(由@Mock
注释注释的对象)注入到测试对象的字段中。Mockito 为此使用反射。
@Autowired
annotation tells to Spring framework to inject bean from its IoC container. Spring also uses reflection for this when it is private field injection.
You can even use even use @Inject
annotation (part of Java EE specification) with the same effect.
@Autowired
注释告诉 Spring 框架从其 IoC 容器注入 bean。在私有字段注入时,Spring 也为此使用了反射。您甚至可以使用@Inject
具有相同效果的注释(Java EE 规范的一部分)。
But I would suggest to look at benefits of Constructor injection over Field injection. In that case you don't need to use @InjectMocks
at all, because you can pass mocks into testing object via constructor. There wouldn't be Reflection needed under the hood in your test nor in production.
但我建议看看Constructor injection 优于 Field injection 的好处。在这种情况下,您根本不需要使用@InjectMocks
,因为您可以通过构造函数将模拟传递给测试对象。在您的测试或生产中,引擎盖下不需要反射。
If you want to create integration test with subset of Spring beans I would suggest to take a look at @DirtiesContext
annotation. It is part of Spring framework module commonly called "Spring Test".
如果您想使用 Spring bean 的子集创建集成测试,我建议您查看@DirtiesContext
注释。它是 Spring 框架模块的一部分,通常称为“Spring 测试”。