在Mockito框架中@Mock和@InjectMocks之间有什么区别?
当前回答
@Mock用于声明/模拟依赖bean的引用,而@InjectMocks用于模拟正在为其创建测试的bean。
例如:
public class A{
public class B b;
public void doSomething(){
}
}
A类测试:
public class TestClassA{
@Mocks
public class B b;
@InjectMocks
public class A a;
@Test
public testDoSomething(){
}
}
其他回答
@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框架教程
虽然上面的答案已经涵盖了,但我只是试图添加我看到缺失的细节。它们背后的原因(为什么)。
说明:
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如何工作的示例代码。
假设我们有Game和Player类。
class Game {
private Player player;
public Game(Player player) {
this.player = player;
}
public String attack() {
return "Player attack with: " + player.getWeapon();
}
}
class Player {
private String weapon;
public Player(String weapon) {
this.weapon = weapon;
}
String getWeapon() {
return weapon;
}
}
如你所见,Game类需要玩家执行攻击。
@RunWith(MockitoJUnitRunner.class)
class GameTest {
@Mock
Player player;
@InjectMocks
Game game;
@Test
public void attackWithSwordTest() throws Exception {
Mockito.when(player.getWeapon()).thenReturn("Sword");
assertEquals("Player attack with: Sword", game.attack());
}
}
Mockito将使用when and thenReturn方法模拟Player类及其行为。最后,使用@InjectMocks Mockito将该玩家放入游戏中。
注意,您甚至不需要创建一个新的Game对象。Mockito会给你注射的。
// you don't have to do this
Game game = new Game(player);
使用@Spy注释也会得到相同的行为。即使属性名不同。
@RunWith(MockitoJUnitRunner.class)
public class GameTest {
@Mock Player player;
@Spy List<String> enemies = new ArrayList<>();
@InjectMocks Game game;
@Test public void attackWithSwordTest() throws Exception {
Mockito.when(player.getWeapon()).thenReturn("Sword");
enemies.add("Dragon");
enemies.add("Orc");
assertEquals(2, game.numberOfEnemies());
assertEquals("Player attack with: Sword", game.attack());
}
}
class Game {
private Player player;
private List<String> opponents;
public Game(Player player, List<String> opponents) {
this.player = player;
this.opponents = opponents;
}
public int numberOfEnemies() {
return opponents.size();
}
// ...
这是因为Mockito将检查Game类的Type Signature,即Player和List<String>。
很多人已经在这里给出了关于@Mock和@InjectMocks的很好的解释。我喜欢它,但我认为我们的测试和应用程序应该以这样一种方式编写,我们不应该使用@InjectMocks。
进一步阅读的示例参考:https://tedvinke.wordpress.com/2014/02/13/mockito-why-you-should-not-use-injectmocks-annotation-to-autowire-fields/
注意,@InjectMocks即将被弃用
弃用@InjectMocks并计划在Mockito 3/4中删除
你可以关注@avp的答案和链接:
为什么你不应该使用InjectMocks注释来自动装配字段
推荐文章
- 从导入的模块中模拟函数
- 使用Jackson将JSON字符串转换为漂亮的打印JSON输出
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- Javadoc @see或{@link}?
- 在单元测试中设置HttpContext.Current.Session
- 在准备语句中使用“like”通配符
- Android Eclipse -无法找到*.apk
- javac和Eclipse编译器之间的区别是什么?
- 工厂模式和策略模式之间的区别是什么?
- 在Java中使用正则表达式提取值
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Intellij IDEA Java类在保存时不能自动编译
- 何时使用Mockito.verify()?
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合