java - JUnit 调用一个调用另一个方法的方法返回 nullPointerException

标签 java android junit mockito

我想测试 randomPlayerStart 方法,它从 Player 类调用 setter 方法。我创建了一个测试用例来检查播放器的 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);
    }
}

我已尝试覆盖 测试文件中的方法并尝试使用 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();
}

我不确定接下来要尝试什么,我没有测试经验,所以非常感谢任何意见或指示。

最佳答案

如何将 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(随机随机)

一旦将依赖项移到外部测试就容易多了,不是吗? :)

关于java - JUnit 调用一个调用另一个方法的方法返回 nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33552238/

相关文章:

java - hasNextInt() 和 nextInt() 方法流程

java - 将参数从 Java 传递到 Python 时出错

java - 我应该在以下示例中使用哪种 JavaFX 数据结构?

android - FFmpegFrameRecorder videoBroadcasting audio comes faster than video frame in 3G 网络

android - Facebook 无法完成()

java - 访问 TestRule 中的自定义注释

java - 如何使用 Mockito 在模拟上显示所有调用

Java Regex - 按空格分割字符串 - 忽略引号和转义引号中的空格

Android Debounce/TakeLast RxJava 在 OnCompleted() 之后从未收到最后一项

junit - "play test"类路径不同于 "sbt test"类路径