我目前在我正在工作的项目中使用 ES 5 和 Jest,并且我被分配到与 DB 交互的类的集成和单元测试。
假设我有这样的方法:
@Override
public Aptitude save(Aptitude aptitude) {
Index index = new Index.Builder(aptitude).index(aptitudeIndexName).type(aptitudeTypeName).refresh(true).build();
try {
client.execute(index);
return aptitude;
} catch (IOException e) {
logger.error("The aptitude couldn't be saved in the data base " + e.getMessage());
throw new RuntimeException(e);
}
}
或这样的:
@Override
public List<Aptitude> findAll() {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchSourceBuilder.sort("id", SortOrder.ASC);
Search search = new Search.Builder(searchSourceBuilder.toString()).addIndex(aptitudeIndexName).build();
try {
SearchResult result = client.execute(search);
if (!result.isSucceeded()) {
return null;
}
List<Hit<Aptitude, Void>> aptitudes = result.getHits(Aptitude.class);
return aptitudes.stream().map(this::getAptitude).collect(Collectors.toList());
} catch (IOException e) {
logger.error("The search couldn't be executed" + e.getMessage());
throw new RuntimeException(e);
}
}
我的任务是编写一些测试(单元和集成)以确保我的所有类都正常工作并获得更好的测试覆盖率,我在网上搜索了一些 ES 测试,但可以完全掌握我应该如何进行测试。
我做了一些看起来像这样的测试:
@Test
public void aptitudeSaveDelete() {
Aptitude aptitude = new Aptitude();
aptitude.setId(1993L);
aptitude.setEn("ENaptitudeEN");
aptitude.setEs("ESaptitudeES");
Aptitude aptitudeSaved = elasticsearchAptitudeRepository.save(aptitude);
assertEquals(aptitude.getEs(), aptitudeSaved.getEs());
assertEquals(aptitude.getEn(), aptitudeSaved.getEn());
assertEquals(aptitude.getId(), aptitudeSaved.getId());
Aptitude aptitudeFoundById = elasticsearchAptitudeRepository.findById(String.valueOf(aptitude.getId()));
assertEquals(aptitude.getId(), aptitudeFoundById.getId());
assertEquals(aptitude.getEn(), aptitudeFoundById.getEn());
assertEquals(aptitude.getEs(), aptitudeFoundById.getEs());
boolean wasDeleted;
wasDeleted = elasticsearchAptitudeRepository.deleteAptitudeById(String.valueOf(aptitude.getId()));
assertEquals(true, wasDeleted);
}
但我的同事告诉我,这种测试损害了数据库,如果他们出了问题,那么数据库将不得不手动修复。
所以这就是为什么我在这里不知所措的原因,任何形式的帮助都是值得的