How to write Junit test for methods with local variables
I have written a method which asks user to press enter to continue and timeouts after some time. I am facing difficulty in writing Junit tests for this method use Mockito. Below is the method.
private static final ExecutorService l = Executors.newFixedThreadPool(1);
private static String getUserInputWithTimeout(int timeout) {
Callable<String> k = () -> new Scanner(System.in).nextLine();
LocalDateTime start = LocalDateTime.now();
Future<String> g = l.submit(k);
while (ChronoUnit.SECONDS.between(start, LocalDateTime.now()) < timeout) {
if (g.isDone()) {
try {
String choice = g.get();
return choice;
} catch (InterruptedException | ExecutionException | IllegalArgumentException e) {
logger.error("ERROR", e);
g = l.submit(k);
}
}
}
logger.info("Timeout...");
g.cancel(true);
return null;
}
I tried mocking Callable and Future but as this method is creating them locally creating them in Test has no impact.
I tried few things but didnt work as expected I might be doing it wrong.
@Test
public void testgetUserInputWithUserInput() throws Exception {
Scanner scanner = new Scanner(System.in);
Callable<String> callable = PowerMockito.mock(Callable.class);
ExecutorService executorServiceMock = PowerMockito.mock(ExecutorService.class);
Future<String> futureMock = PowerMockito.mock(Future.class);
when(executorServiceMock.submit(any(Callable.class))).thenReturn(futureMock);
assertEquals("", getUserInputWithTimeout(3));
}
Solution 1:
I would say you need to slightly change your method, take the callable object out of method and pass it as a parameter, this should solve your problem with mocking.
private static final ExecutorService l = Executors.newFixedThreadPool(1);
private static String getUserInputWithTimeout(int timeout, Callable<String> k) {
LocalDateTime start = LocalDateTime.now();
Future<String> g = l.submit(k);
while (ChronoUnit.SECONDS.between(start, LocalDateTime.now()) < timeout) {
if (g.isDone()) {
try {
String choice = g.get();
return choice;
} catch (InterruptedException | ExecutionException | IllegalArgumentException e) {
logger.error("ERROR", e);
g = l.submit(k);
}
}
}
logger.info("Timeout...");
g.cancel(true);
return null;
}
Your tests should look like below :
@Test
public void testgetUserInputWithUserInput() throws Exception {
String ENTER = " ";
System.setIn(new ByteArrayInputStream(ENTER.getBytes()));
Callable<String> callable = () -> new Scanner(System.in).nextLine();
assertEquals(ENTER, getUserInputWithTimeout(5, callable));
}