【发布时间】:2019-11-05 16:31:14
【问题描述】:
我在我的一个单元测试中模拟 2 个类,用 Mockito 定义行为。何时调用函数。
其中一个模拟类完全按预期工作,另一个返回null。我不知道这两者之间有什么区别。
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();
}
}
最让我困惑的是,在queryService.getRecordsByQuery() 中,elasticConnectionService.getRowIDsFromQuery() 模拟返回了预期的结果,但hbaseConnectionService.getRowsByIDs() 模拟返回了null。
elastic 和 hbase 连接服务类都定义在同一个文件夹中,它们唯一的注解是@Service。如果两者都失败,我会认为我配置了一些错误,但 elasticConnectionService 调用按预期工作的事实告诉我正在发生其他事情。
【问题讨论】:
-
JSONObject的封装是什么?
-
@StvnBrkdll 我正在使用 org.json
标签: java spring unit-testing testing mockito