我想测试从Player类调用setter方法的方法randomPlayerStart
。我创建了一个测试用例,用于检查播放器的boolean
值。
@Before
public void setUp() throws Exception {
playerA = new Player("Player A", false);
playerB = new Player("Player B", false);
activity = new MainActivity();
activity.randomPlayerStart();
}
@Test
public void testrandomPlayerStart() throws Exception {
assertEquals(true,playerB.isTurn());
}
这是randomPlayerStart
方法。
public void randomPlayerStart() {
Random random = new Random();
boolean player = random.nextBoolean();
if (player) {
playerA.setTurn(true);
} else {
playerB.setTurn(true);
}
}
我已经尝试了overriding
测试文件中的方法,并试图从mockito框架中使用Spy,但它没有用。当我使用doNothing而不是doAnswer时,我没有收到nullException
错误,但是该方法没有调用。
@Before
public void setUp() throws Exception {
Turn = false;
playerA = new Player("Player A", false) {
public void setTurn(boolean turn) {
Turn = turn;
}
};
playerB = new Player("Player B", false) {
public void setTurn(boolean turn) {
Turn = turn;
}
};
activity = new MainActivity() {
public void randomPlayerStart() {
Random random = new Random();
boolean player = random.nextBoolean();
if (player) {
playerA.setTurn(true);
} else {
playerB.setTurn(true);
}
}
};
MainActivity spy = Mockito.spy(activity);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
activity.randomPlayerStart();
return null;
}
}).when(spy).randomPlayerStart();
spy.randomPlayerStart();
}
我不确定接下来要尝试什么,我没有测试经验,因此非常感谢任何评论或指示。
答案 0 :(得分:1)
如果您将Random
移到randomPlayerStart
之外,那么您可以mock
然后轻松测试您的方法。
//omitted declarations
Random random;
@Before
public void setUp() throws Exception {
playerA = new Player("Player A", false);
playerB = new Player("Player B", false);
activity = new MainActivity();
random = mock(Random.class); //static import of Mockito.mock
}
@Test
public void testRandomPlayerStart_playerA() throws Exception {
when(random.nextBoolean()).thenReturn(Boolean.TRUE); //static import of Mockito.when
activity.randomPlayerStart(random);
assertEquals(true,playerA.isTurn());
}
@Test
public void testRandomPlayerStart_playerB() throws Exception {
when(random.nextBoolean()).thenReturn(Boolean.FALSE);
activity.randomPlayerStart(random);
assertEquals(true,playerB.isTurn());
}
您的randomPlayerStart
需要接受随机参数
public void randomPlayerStart(Random random)
一旦将依赖关系移到外面测试就容易多了,不是吗? :)
答案 1 :(得分:0)
发表评论njzk2。我决定创建另一个类(MainActivity presenter),它接受三个值,一个接口和两个播放器对象。
public class MainActivityPresenter {
MainActivityView view;
Player playerA, playerB;
public MainActivityPresenter(MainActivityView view, Player playerA, Player playerB) {
this.view = view;
this.playerA = playerA;
this.playerB = playerB;
}
public void randomPlayerStart() {
Random random = new Random();
boolean player = random.nextBoolean();
if (player) {
playerA.setTurn(true);
} else {
playerB.setTurn(true);
}
}
public void initPlayers() {
playerA = new Player("Player A", false);
playerB = new Player("Player B", false);
}
}
然后我创建了一个我在MainActivity类中实现的接口。现在可以实例化MainActivityPresenter类。
public void startGame(){
initPlayers();
linkIds();
initBtns();
initCells();
initLinkedList();
randomPlayerStart();
hand = new Hand(0);
switchTurn();
presenter = new MainActivityPresenter(MainActivity.this, playerA, playerB );
}
public void randomPlayerStart() {
presenter.randomPlayerStart();
}
这意味着我可以通过实例化MainActivityPresenter类而不是MainActivity类从测试类中调用randomPlayerStart()方法,而不会出现NullException错误。
谢谢大家