【发布时间】:2018-09-28 14:59:42
【问题描述】:
我正在尝试测试 Game 类何时被实例化,start 方法被调用。但是我收到以下错误:
Wanted but not invoked:
game.start();
Actually, there were zero interactions with this mock.
我有一个名为Game的类
public class Game {
private Hand player_hand;
private Hand dealer_hand;
public static Boolean isInPlay;
public Game() {
player_hand = new Hand();
dealer_hand = new Hand();
start();
}
public void start() {
isInPlay = true;
player_hand.hit();
player_hand.hit();
System.out.println("Player hand: ");
player_hand.printHands();
instantWinLose();
dealer_hand.hit();
dealer_hand.hit();
}
}
我有一个名为GameTest的测试类
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class GameTest {
@InjectMocks
Game game;
@Mock
Hand hand;
@Test
public void testGameConstruction() {
Game mockedGame = mock(Game.class);
verify(mockedGame, times(1)).start();
}
}
我是 Mockito 的新手。我在Difference between @Mock and @InjectMocks 中尝试过以下示例,但我仍然遇到同样的错误
【问题讨论】:
标签: java unit-testing mocking mockito