I need to programm Mastermind and ive done this with a random key. I now need to Unit test them. its easy for the control methods in my Helperclass but i dont know how to really mock or test the method with a Random Object from Class Random or a method using an Object from Scanner Class here the Code:
public int[] randomKey(){
int[] key = new int[4];
Random random1 = new Random();
for(int i = 0;i < 4;i++) {
key[i] = random1.nextInt(6);//Random Zahl for the Key
}
return key;
}
Thats the Random method in HelperClass How you do there the Mock object and test
private String[] guess(){ //Input from guessing
int k = 1;
Scanner eingabewert = new Scanner(System.in);
String[] guess = new String[4];
for(int i = 0; i < 4;i++){
k = 1;
while(k==1) {
System.out.println("Jetzt die " + (i + 1) + ".te Farbe eingeben ");
guess[i] = eingabewert.nextLine();
if(guess[i].equals("red")||guess[i].equals("blue")||guess[i].equals("yellow")||guess[i].equals("green")||guess[i].equals("purple")||guess[i].equals("brown")){
k = 0;
}
}
}
return guess;
}
my helpless try was in the Test class for the method with the Random() Object
@Mock
private java.util.Random random;
@InjectMocks
private HelperClass helperClass;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
void testRandomKey(){
Random mockedItem = Mockito.mock(Random.class);
when(mockedItem.nextInt(6)).thenReturn(1);
}
}
I dont know how to go on or basically understood how this actually works pls help me and try to find a short solution for the test. okay if its just to the randomKey method. Im using Intellij Idea as IDE btw.
Aucun commentaire:
Enregistrer un commentaire