我想测试是否调用fan.setState
方法
class OffState implements State {
@Override
public void changeState(Fan fan) {
fan.setState(new OnState());
}
}
答案 0 :(得分:2)
像这样:
@RunWith(MockitoJUnitRunner.class)
public class OffStateTest {
@Mock
private Fan fan;
@Test
public void testChangeState() {
//Arrange
OffState offState = new OffState();
//Act
offState.changeState(fan);
//Assert
Mockito.verify(fan).setState(Mockito.any(OnState.class));
}
}