mindreader
mindreader

Reputation: 1813

Unable to mock method call using mockito

I'm writing a test case where I'm trying to use mockito to avoid entering text through console. But on running the test case, it waits for something to be entered in console (which is again something I'm unable to do) instead of getting it from mockito. Would appreciate if someone can help. The test code looks like this:

SongsNameUpdater songsNameUpdater = new SongsNameUpdater();
bufferedReader = mock(BufferedReader.class);
when(bufferedReader.readLine()).thenReturn("test Path");
songsNameUpdater.updateSongNames();

The main code is:

public class SongsNameUpdater {
       public void updateSongNames() throws IOException {
               bufferedReader = new BufferedReader(new InputStreamReader(System.in));
               String path = bufferedReader.readLine();
               System.out.println(path);
       }

}

Upvotes: 0

Views: 1533

Answers (1)

Omnaest
Omnaest

Reputation: 3096

You have to pass your BufferedReader mock e.g. like updateSongNames(bufferedReader) and not instantiating it within the method

Upvotes: 5

Related Questions