java @InjectMocks 的等效方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12854344/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:29:58  来源:igfitidea点击:

Method equivalent for @InjectMocks

javajunitmockingmockito

提问by Kevindra

What is method equivalent for the following:

什么是以下方法的等效方法:

@Mock
MyType1 myType1;

@Autowired
@InjectMocks
MyType2 myType2;

I can replace @Mockwith mock(MyType1.class).

我可以替换@Mockmock(MyType1.class).

But how can I replace @InjectMockswith a method call? Something like this:

但是如何@InjectMocks用方法调用替换?像这样的东西:

injectMocks(MyType2.class)

采纳答案by Alban

Why using Autowiredin your junit test? Since you are mocking the dependencies for MyType2you must know its concreate implementation when you write your test.

为什么Autowired在您的 junit 测试中使用?由于您正在模拟依赖项,因此MyType2您在编写测试时必须知道它的 concreate 实现。

Then you don't need and shouldn't use Spring or any injection framework to create the instance of MyType2that you want to test. Create it directly in your test initialization! I know that after some years of using IoC frameworks, it's difficult to write myType2 = new MyType2Impl(mock(myType1.class))but it will really makes your tests simpler, and faster (because no application context to build). E.g.:

那么你不需要也不应该使用 Spring 或任何注入框架来创建MyType2你想要测试的实例。直接在您的测试初始化​​中创建它!我知道使用 IoC 框架多年后,编写myType2 = new MyType2Impl(mock(myType1.class))起来很困难,但它确实会让您的测试更简单、更快(因为没有要构建的应用程序上下文)。例如:

@Before
public void setup() {
  myType1 = mock(MyType1.class);
  myType2 = new MyType2Impl(myType1);
}

But if you really want to use IoC in your junit tests, use springockitohas suggested by Brice, and build your mock MyType1in your application context.

但是,如果您真的想在 junit 测试中使用 IoC,请使用Brice 建议的springockito,并MyType1在您的应用程序上下文中构建您的模拟。

回答by Brice

There is no public API in Mockito for mock injection. Plus as this annotation is mostly driven on the way things are laid out in a test, it is fairly related to the initialization phase of the test.

Mockito 中没有用于模拟注入的公共 API。此外,由于此注释主要是根据测试中事物的布局方式驱动的,因此它与测试的初始化阶段相当相关。

Though it might change at some point in the future.

虽然它可能会在未来的某个时候改变。

However Mockito annotated fields can be initialized either by the MockitoJUnitRunneror by MockitoAnnotations.initMocks(). They both create mock instances and perform injection.

然而,Mockito 注释字段可以由MockitoJUnitRunner或初始化MockitoAnnotations.initMocks()。它们都创建模拟实例并执行注入。

Also I see in your code that you are using @Autowired- hence spring stuff, probably configured via XML. @InjectMockswasn't really developed to work with other dependency injection frameworks, as the development was driven by unit test use cases, not integration tests.

另外我在你的代码中看到你正在使用@Autowired- 因此弹簧的东西,可能是通过 XML 配置的。@InjectMocks并没有真正开发为与其他依赖注入框架一起使用,因为开发是由单元测试用例驱动的,而不是集成测试。

You might want to take a look at springockito, which is another project that tries to ease Mockito mock creation in Spring.

您可能想看看springockito,这是另一个试图在 Spring 中简化 Mockito 模拟创建的项目。

Hope that helps

希望有帮助