如何使用JUnit或Mockito测试使用无参数和返回值的Random()的方法 [英] How to test a method that uses Random(), without arguments and return value, using JUnit or Mockito

查看:914
本文介绍了如何使用JUnit或Mockito测试使用无参数和返回值的Random()的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习成为一名Java开发人员,现在我正在学习测试驱动的开发,这意味着对于JUnit和Mockito来说,这是非常新的.

I'm studying to be a Java developer and right now I'm learning test driven development, which means that im very new to JUnit and Mockito.

我已经挣扎了一段时间,被困住了.

I've been struggling for a while now and I'm stuck.

我不知道如何测试这种没有参数,没有返回值和随机数的特殊方法.

I have no idea how to test this particular method that has no arguments, no return value and a randomizer.

旧逻辑:

public void getPlayerToStart(int randomNr) {
    if (randomNr == 1) {
        currentPlayer = p1;
        opponentPlayer = p2;
    } else {
        currentPlayer = p2;
        opponentPlayer = p1;
    }
}

旧测试

@Test
void testSetCurrentPlayerSetToPlayer1() {
    gameEngine.getPlayerToStart(1);
    assertEquals(gameEngine.getP1(), gameEngine.getCurrentPlayer());
    assertEquals(gameEngine.getP2(), gameEngine.getOpponentPlayer());
}

@Test
void testSetCurrentPlayerSetToPlayer2() {
    gameEngine.getPlayerToStart(2);
    assertEquals(gameEngine.getP2(), gameEngine.getCurrentPlayer());
    assertEquals(gameEngine.getP1(), gameEngine.getOpponentPlayer());
}

新逻辑:

public void getPlayerToStart() {
    Random rand = new Random();
    int randomNr = rand.nextInt(2) + 1;
    if (randomNr == 1) {
        currentPlayer = p1;
        opponentPlayer = p2;
    } else {
        currentPlayer = p2;
        opponentPlayer = p1;
    }
}

我不确定如何在不带参数"randomNr"的情况下测试getPlayerToStart().有人可以向我指出正确的方向吗?

I'm not sure how to be able to test the getPlayerToStart() without the argument "randomNr".. Can someone please just point me in the right direction!

谢谢.

推荐答案

像这样将对new Random()的调用移动到自己的方法中.

Move the call to new Random() into its own method, like this.

您可以重写您的getPlayerToStart方法以使用另一个方法,以保存重复的代码(但不必这样做).

You can rewrite your getPlayerToStart method to use the other one, to save duplicated code (but you don't need to).

public void getPlayerToStart() {
    Random rand = makeRandom();
    int randomNumber = rand.nextInt(2) + 1
    getPlayerToStart(randomNumber);
}

public Random makeRandom() {
    return new Random();
}

现在您可以使用Mockito进行

Now you can use Mockito to

  • 制作一个模拟Random对象;
  • 监视您的类,这是您要测试的对象;
  • 对间谍的makeRandom方法进行存根,以便返回模拟Random;
  • 对模拟Random进行存根,以便在每次测试中返回您喜欢的任意值.
  • make a mock Random object;
  • make a spy of your class, which is the object you're going to test;
  • stub the makeRandom method of your spy, so that it returns your mock Random;
  • stub the mock Random so that it returns whichever value you like, in each test.

在那之后,您可以编写一个测试,其中播放器1应该启动,而另一个测试中,播放器2应该启动.

After that, you can write a test in which player 1 is expected to start, and another test in which player 2 is expected to start.

这篇关于如何使用JUnit或Mockito测试使用无参数和返回值的Random()的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆