I am mocking 2 classes in one of my unit tests, defining the behavior with Mockito.when and then calling the functions.
One of the mocked classes works exactly as expected, the other returns null. I can't figure out what the difference is between the two.
QueryServiceTest.java
@Import({ QueryServiceTestConfig.class })
@RunWith(SpringRunner.class)
public class QueryServiceTest {
@Autowired
private QueryService queryService;
@MockBean
private ElasticConnectionService elasticConnectionService;
@MockBean
private HBaseConnectionService hbaseConnectionService;
@Test
public void test_getRecordsFromQuery() throws IOException {
// creation of sample data for inputs and outputs goes here
// This mock works when called from queryService.getRecordsFromQuery()
when(elasticConnectionService.getRowIdsFromQuery(filterParams, testIndex)).thenReturn(getRowIdsFromQuery_result);
List<JSONObject> matches = queryService.getMatchingRowIds(getRowIdsFromQuery_result);
// matchesArray is directly defined to make sure its exactly the same as in queryService.getRecordsFromQuery()
JSONObject matchesArray = new JSONObject("{\"testTable\":[\"testUUID\"]}");
// This mock fails when called from queryService.getRecordsFromQuery()
when(hbaseConnectionService.getRowsByIDs(matchesArray)).thenReturn(getRowsByIDs_result);
// This returns getRowsByIDs_result as expected
JSONArray test = hbaseConnectionService.getRowsByIDs(matchesArray);
// This returns null
JSONArray actual = new JSONArray(queryService.getRecordsFromQuery(filterParams, testIndex));
}
}
QueryService.java
@Service
public class QueryService {
@Autowired
private ElasticConnectionService elasticConnectionService;
@Autowired
private HBaseConnectionService hbaseConnectionService;
@Autowired
private PSQLConnectionService psqlConnectionService;
public String getRecordsFromQuery(
Map<String,String> filterParams,
String tablename) throws IOException {
/**
* Get records that match simple key/value filters
*/
// This mocked method returns exactly what was expected
List<List<JSONObject>> lookupsList = elasticConnectionService.getRowIdsFromQuery(filterParams, tablename);
List<JSONObject> matches = getMatchingRowIds(lookupsList);
// matchesArray is exactly the same as in the test class
JSONObject matchesArray = new JSONObject("{\"testTable\":[\"testUUID\"]}");
// This returns null
JSONArray hbResults = hbaseConnectionService.getRowsByIDs(matchesArray);
return hbResults.toString(4);
}
}
QueryServiceTestConfig.java
@Configuration
public class QueryServiceTestConfig {
@Bean
public QueryService queryService() {
return new QueryService();
}
@Bean
public ElasticConnectionService elasticConnectionService() {
return new ElasticConnectionService();
}
@Bean
public HBaseConnectionService hbaseConnectionService() {
return new HBaseConnectionService();
}
@Bean
public PSQLConnectionService psqlConnectionService() {
return new PSQLConnectionService();
}
}
What confuses me most is that in queryService.getRecordsByQuery, the elasticConnectionService.getRowIDsFromQuery() mock returns what was expected, but the hbaseConnectionService.getRowsByIDs() mock returns null.
The elastic and hbase connection service classes are both defined in the same folder and the only annotation they have is @Service. I would think I had configured something wrong if both failed, but the fact that the elasticConnectionService call works as expected tells me something else is happening.
Aucun commentaire:
Enregistrer un commentaire