如何使用mockito测试给定的代码

时间:2017-08-29 09:20:58

标签: java unit-testing mockito tdd

我想测试是否调用fan.setState方法

class OffState implements State {

    @Override
    public void changeState(Fan fan) {
        fan.setState(new OnState());
    }

}

1 个答案:

答案 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));
    }
}