Java 随机数法的Junit检验

Java 随机数法的Junit检验,java,junit,mockito,Java,Junit,Mockito,我试图为一个方法创建一个Junit测试,该方法使用一个简单的随机数调用另一个简单的方法,该方法根据参数的值返回rock、paper或scissor 我不知道怎么做 public class Play { public String player2Choice() { // Create a Random object. Random random = new Random(); // Ramdon number between 1-3

我试图为一个方法创建一个Junit测试,该方法使用一个简单的随机数调用另一个简单的方法,该方法根据参数的值返回rock、paper或scissor

我不知道怎么做

public class Play {

    public String player2Choice() {

        // Create a Random object.
        Random random = new Random();

        // Ramdon number between 1-3
        int numberChoice = random.nextInt(3) + 1;

        // Return the player random choice
        return getItem(numberChoice);
    }

    public String getItem(int numberChoice) {

        String item;

        switch (numberChoice) {
        case 1:
            item = "rock";
            break;
        case 2:
            item = "scissors";
            break;
        case 3:
            item = "paper";
            break;
        default:
            item = null;
        }

        return item;
    }
}
现在我有一个测试:

Play play;

@Before
public void setup() {

    play = new Play();
}

@Test
public void testPlayer2Choice() {

    int param = 1;
    Play play2 = Mockito.mock(Play.class);


    play2.getItem(param);
    Mockito.verify(play2).getItem(param);

    String result = play.player2Choice();

    assertEquals("rock", result);


}

Play
Random
紧密耦合,它实际上无法控制。这使得测试特定的数字选择变得困难

如果目标是测试给定数量的
1
player2Choice
返回
“rock”
,则需要对所谓的随机性进行更多的控制

public interface RandomInteger {
    int nextInt(int max);
}
接口的实现将封装一个实际的
Random
类,并代理所需的行为/功能

public class RandomIntegerImplementation : RandomInteger {
    private Random random ;

    public RandomIntegerImplementation() {
        // Create a Random object.
        random  = new Random();
    }

    public int nextInt(int max) {
        return random.nextInt(max);
    }
}
Play随后需要重构,以明确依赖于现在的抽象随机提供者

public class Play {
    private RandomInteger random;

    public Play(RandomInteger random) {
        this.random = random;
    }

    public String player2Choice() {
        // Ramdon number between 1-3
        int numberChoice = random.nextInt(3) + 1;

        // Return the player random choice
        return getItem(numberChoice);
    }

    //...code removed for brevity
}
因此,现在如果要满足上述测试用例,可以模拟依赖项,使其按照预期的方式运行

RandomInteger random;

Play play;

@Before
public void setup() {
    random = Mockito.mock(RandomInteger.class);
    play = new Play(random);
}

@Test
public void testPlayer2Choice_Given_Zero() {
    //Arrange
    String expected = "rock";
    int numberChoice = 0;

    when(random.nextInt(3)).thenReturn(numberChoice);

    //Act
    String actual = play.player2Choice();

    //Assert
    Mockito.verify(play).getItem(numberChoice + 1);
    assertEquals(expected, actual);
}
这应该足够开始了。其他测试用例也可以这样做


最终,
getItem
也可以重构为自己的服务,但这超出了原始问题的范围。

任何类型的测试都有一个一般原则。在您编写测试之前,请在您自己的头脑中明确您正试图测试的需求。不要用“测试这个类”或“测试这个方法”来思考。从测试需求的角度考虑。如果你不知道你在测试什么需求,你将永远无法写出一个好的测试。