在Mockito框架中@Mock和@InjectMocks之间有什么区别?
当前回答
@Mock注释模拟相关对象。
@InjectMocks注释允许将@Mock创建的不同(和相关的)模拟注入到底层对象中。
两者是相辅相成的。
其他回答
很多人已经在这里给出了关于@Mock和@InjectMocks的很好的解释。我喜欢它,但我认为我们的测试和应用程序应该以这样一种方式编写,我们不应该使用@InjectMocks。
进一步阅读的示例参考:https://tedvinke.wordpress.com/2014/02/13/mockito-why-you-should-not-use-injectmocks-annotation-to-autowire-fields/
@Mock创建一个mock。@InjectMocks创建类的一个实例,并将使用@Mock(或@Spy)注释创建的模拟注入到该实例中。
注意,您必须使用@RunWith(MockitoJUnitRunner.class)或Mockito.initMocks(this)来初始化这些模拟并注入它们(JUnit 4)。
在JUnit 5中,必须使用@ExtendWith(MockitoExtension.class)。
@RunWith(MockitoJUnitRunner.class) // JUnit 4
// @ExtendWith(MockitoExtension.class) for JUnit 5
public class SomeManagerTest {
@InjectMocks
private SomeManager someManager;
@Mock
private SomeDependency someDependency; // this will be injected into someManager
// tests...
}
@Mock用于创建和注入模拟实例,而无需调用Mockito。手动模拟。在本例中,实例是ClassB。
而@InjectMocks用于自动将模拟字段注入到测试对象中。在这种情况下,它将是a类
虽然上面的答案已经涵盖了,但我只是试图添加我看到缺失的细节。它们背后的原因(为什么)。
说明:
Sample.java
---------------
public class Sample{
DependencyOne dependencyOne;
DependencyTwo dependencyTwo;
public SampleResponse methodOfSample(){
dependencyOne.methodOne();
dependencyTwo.methodTwo();
...
return sampleResponse;
}
}
SampleTest.java
-----------------------
@RunWith(PowerMockRunner.class)
@PrepareForTest({ClassA.class})
public class SampleTest{
@InjectMocks
Sample sample;
@Mock
DependencyOne dependencyOne;
@Mock
DependencyTwo dependencyTwo;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
public void sampleMethod1_Test(){
//Arrange the dependencies
DependencyResponse dependencyOneResponse = Mock(sampleResponse.class);
Mockito.doReturn(dependencyOneResponse).when(dependencyOne).methodOne();
DependencyResponse dependencyTwoResponse = Mock(sampleResponse.class);
Mockito.doReturn(dependencyOneResponse).when(dependencyTwo).methodTwo();
//call the method to be tested
SampleResponse sampleResponse = sample.methodOfSample()
//Assert
<assert the SampleResponse here>
}
}
参考
@Mock为你需要的类创建一个模拟实现。 @InjectMock创建类的一个实例,并将用@Mock注释标记的模拟注入其中。
例如
@Mock
StudentDao studentDao;
@InjectMocks
StudentService service;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
这里我们需要服务类的DAO类。因此,我们模拟它并将它注入到服务类实例中。 类似地,在Spring框架中,所有@Autowired bean都可以在jUnits中被@Mock模拟,并通过@InjectMocks注入到bean中。
initmocks (this)方法初始化这些模拟,并为每个测试方法注入它们,因此需要在setUp()方法中调用。
这个链接有一个很好的Mockito框架教程
推荐文章
- 一对多、多对一、多对多的区别?
- 从枚举中选择一个随机值?
- 如何从URL获取参数与JSP
- 如何在Eclipse中生成Javadoc注释?
- 找到java类从哪里加载
- 从集合中随机选取一个元素
- 为什么x == (x = y)和(x = y) == x不一样?
- 什么Java 8流。收集等价物可在标准Kotlin库?
- 等待未来的名单
- 如何检查JSON键是否存在?
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 即使从未抛出异常,使用try-catch块的代价是否昂贵?
- 什么时候我们应该使用观察者和可观察对象?
- Java中的split()方法对点(.)不起作用。
- Eclipse调试器总是阻塞在ThreadPoolExecutor上,没有任何明显的异常,为什么?