I´ve got a Method that should read in Input 4 times and do this in an Array. Fine that works. I refactored all for making a Unit test with mock objects. Now the method when().thenReturn always ends in a NullPointerExeption . Source Code is from 3 different classes below :
import java.util.Scanner;
public class SystemClass {
Scanner valueIn = new Scanner(System.in);
public String getInput(){
return valueIn.nextLine();
}
}
This the extracted ScannerClass
import java.util.Arrays;
public class GuessRefactor {
private SystemClass systemObj;
public String[] guess(SystemClass systemObj){
int k = 1;
String[] guess = new String[4];
for(int i = 0; i < 4;i++){
k = 1;
while(k==1) {
System.out.println("now the " + (i + 1) + "color ");
guess[i] = systemObj.getInput();
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;
}
public static void main(String[] args) {
System.out.println(Arrays.toString(new GuessRefactor().guess(new SystemClass())));
}
}
This the guessing ( Its for Mastermind )
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
class TestClass {
@Mock
SystemClass a;
@Test
public void test() {
String b = "rot";
GuessRefactor guessRefactor = new GuessRefactor();
//Mockito.when(a.getInput()).thenReturn(b);
Mockito.when(a.getInput()).thenReturn(b);
String[] expectedOutput = {"red", "red", "red", "red",};
String[] output = guessRefactor.guess(a);
Assert.assertArrayEquals(expectedOutput, output);
}
}
This is the TestClass.
Pls Help me!!!