0

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!!!

GottaGaming
  • 51
  • 1
  • 10
  • because even though you create a Mock, your test doesn't know you intend to use it. – Stultuske Mar 05 '20 at 12:36
  • @Stultuske and how to fix it. Dont know what youre really mean – GottaGaming Mar 05 '20 at 12:38
  • you have mocked a.getInput(), but the moment you instantiate GuessRefactor, it doesn't know anything about your mock. You need to create your GuessRefactor class level (in your test) and annotate it with @InjectMocks – Stultuske Mar 05 '20 at 12:42
  • @Stultuske you got some code for me ? – GottaGaming Mar 05 '20 at 12:46
  • @Stultuske Still not works!! https://prnt.sc/rc02hq – GottaGaming Mar 05 '20 at 12:48
  • https://www.baeldung.com/mockito-series – Stultuske Mar 05 '20 at 12:48
  • Hey brozilla! If you're getting an `NPE`, it sounds like your mock isn't being initialized. Try doing it manually at the start of your test method with `a = Mockito.mock(SystemClass.class);` and see what happens. – Kayaman Mar 05 '20 at 13:13
  • Yeah I'm a regular StackOverflow saint. The problem is, it shouldn't be necessary to manually mock the object, as the `@RunWith(MockitoJUnitRunner.class)` and `@Mock` annotations should handle that automatically. Although it might be a version issue, since it should be `import org.mockito.junit.MockitoJUnitRunner;` and not the one you're using. – Kayaman Mar 05 '20 at 13:45
  • Gotch ya again bro.. did you use this at Test class [import org.mockito.runners.MockitoJUnitRunner;] – Aditya Rewari Mar 05 '20 at 15:31
  • @AdityaRewari Yes used it. everythings fine. Kayaman was right. TY BRO. – GottaGaming Mar 06 '20 at 06:15

0 Answers0