I have following code and I am trying to write test for it:
In my class (MyService) I have following method getItemBySiteUuidAndItemUuids
which invokes a service in two different threads and gets the results.
@Override
public void getItemBySiteUuidAndItemUuids(String param1, String param2) {
ExecutorService executorService = Executors.newFixedThreadPool(2);
Future<String> future1 = fetchData1(executorService, param1);
Future<String> future2 = fetchData2(executorService, param1, param2);
try {
String dat1 = future1.get();
String data2 = future2.get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
private Future<List<ItemMaster>> fetchData1(
ExecutorService executorService, String param1) {
return executorService.submit(() -> {
return externalService.getData(param1);
});
}
private Future<List<ItemMaster>> fetchData2(
ExecutorService executorService, String param1, String param2) {
return executorService.submit(() -> {
return externalService.getData(param1, param2);
});
}
In my test class, I mocked externalService (which is injected in this class) and defined mock behavior:
@Test
public void givenValidSiteUuidAndItemUuids_getItemBySiteUuidAndItemUuids_givesItemData() {
Mockito.when(externalService.getData(Mockito.anyString())).thenReturn(getMockData());
String data = myService.getData(
"mockParam1", "mockParam2");
assertThat(data, is(expectedData));
}
Now when the test runs, it fails with a NullPointerException when it tries to get future1.get().
I am not able to understand how to get past this. Any pointers will help.
Thanks.
Aucun commentaire:
Enregistrer un commentaire