Mockito:测试void方法会产生InvalidUseOfMatchersException

时间:2017-07-06 16:48:18

标签: java unit-testing junit mockito

我遇到两个void方法的问题。在encouragedVenturesScoring我已经按照这个答案mocking an arraylist that will be looped in a for loop并且没有嘲笑列表,但是传递了一个真实的列表并添加了模拟对象。

Mockito在这一行给我一个InvalidUseOfMatchersException

verify(effectList.get(Mockito.anyInt())).execute(playerHandler);

关于此异常的SO有很多问题,我认为这是因为anyInt()。无论如何我把它改成了

verify(effectList.get(0)).execute(playerHandler);

现在它说Wanted but not invoked effect.execute(playerHandler) Actually there were zero interactions with this mock

是因为我放了什么? doNothing().when(effect).execute(playerHandler);

在我的第二种方法militaryStrengthScoring()方法中,有没有办法跳过第一块代码并测试if..else条件?测试这种方法的最佳方法是什么?

感谢您的时间。

这是要测试的课程

 public class EndGameScoringBaseController implements EndGameScoringHandler {

private static final int[] TERRITORIES_REWARD = {0,0,1,4,10,20};
private static final int[] CHARACTERS_REWARD = {1,3,6,10,15,21};
private static final int RESOURCES_RATE = 5;
private static final int FIRST_MILITARY_REWARD = 5;
private static final int SECOND_MILITARY_REWARD = 2;

private PlayerHandler player;

public EndGameScoringBaseController(PlayerHandler player) {
    super();
    this.player = player;
}

@Override
public void encouragedVenturesScoring() {
    for (DevelopmentCard card : player.getPlayer().getPersonalBoard().getVentures()) {
        for (Effect e : card.getPermanentEffects())
            e.execute(player);
    }
}

@Override
public void militaryStrengthScoring(GameController game) {
    Set<Integer> points = new HashSet<>();
    int myPoints = this.player.getPointsHandler().getMilitaryPoints();

    for (PlayerHandler p: game.getPlayers()) {
        points.add(p.getPointsHandler().getMilitaryPoints());
    }
    int[] rank = new int[points.size()];
    int j = 0;
    for (Integer i : points) {
        rank[j] = i;
        j++;
    }

    Arrays.sort(rank);

    if (rank[rank.length-1] == myPoints) {
        player.getPointsHandler().winMilitaryPoints(FIRST_MILITARY_REWARD);
    }
    else if (rank[rank.length-2] == myPoints) {
        player.getPointsHandler().winVictoryPoints(SECOND_MILITARY_REWARD);
    }

}

encouVenturesScoring的测试方法

@Test
public void encouragedVenturesScoringTest() {
    //given
    List<DevelopmentCard> ventureList;
    ventureList = Arrays.asList(developmentCard, developmentCard);
    when(playerHandler.getPlayer().getPersonalBoard().getVentures()).thenReturn(ventureList);
    List<Effect> effectList;
    effectList = Arrays.asList(effect, effect);
    when(developmentCard.getPermanentEffects()).thenReturn(effectList);
    doNothing().when(effect).execute(playerHandler);

    //when
    endgameController.encouragedVenturesScoring();

    //then
    verify(effectList.get(Mockito.anyInt())).execute(playerHandler);
}

军事实力训练测试方法不完整

@Test
public void militaryStrengthScoringTest() {

//given     
when(playerHandler.getPointsHandler().getMilitaryPoints()).thenReturn(4);
doNothing().when(playerHandler.getPointsHandler()).winMilitaryPoints(FIRST_MILITARY_REWARD);

//when
endgameController.militaryStrengthScoring(gameController);

//then
/../    
}

2 个答案:

答案 0 :(得分:1)

您只能验证Mockito创建的模拟对象。

但是 effectList 是一个&#34;真实&#34;名单。因此,Mockito对这个目标一无所知。因此,任何验证该列表的尝试都必须失败。

如果你想验证那个对象 - 那么你必须嘲笑它​​!

当然,这意味着您已指定将转到模拟列表的所有调用。

答案 1 :(得分:1)

你是对的,这就是问题所在:

verify(effectList.get(Mockito.anyInt())).execute(playerHandler);

由于internal implementation of matchers,Mockito仅允许any()anyInt()这样的调用代表模拟本身的参数。

/*  OK */  when(yourMock.yourMethod(anyInt())).thenReturn(42);
/* BAD */  when(yourList.get(anyInt()).yourMethod(0)).thenReturn(42);

/*  OK */  verify(yourMock).yourMethod(anyInt());
/* BAD */  verify(yourList.get(anyInt())).yourMethod(0);

get(0)失败可能是实际失败,可能与您的encouragedVenturesScoringTest实际上没有调用encouragedVenturesScoring这一事实有关,它正在调用{{1} }}。如果在修复错误后继续给您带来麻烦,请以与Mockito相关的方式编辑您的问题。