【发布时间】:2022-01-02 18:09:15
【问题描述】:
我已经编写了几个单元测试,现在转而在我们的 Java (Spring Boot) 应用程序中编写集成测试。我们使用 JUnit 和 Mockito 库进行测试。
据我所知,集成测试检查的是整个环而不是一个函数。但是,我很困惑,如果我还应该在集成测试时检查方法中的 if 条件。这是一个示例服务方法:
@Override
public CountryDTO create(CountryRequest request) {
if (countryRepository.existsByCodeIgnoreCase(countryCode)) {
throw new EntityAlreadyExistsException();
}
final Country country = new Country();
country.setCode("UK");
country.setName("United Kingdom");
final Country created = countryRepository.save(country);
return new CountryDTO(created);
}
我的问题是:
1.我可以为 Service 或 Repository 类编写集成测试吗?
2. 当我在上面的服务中测试 create 方法时,我想我只是在我的 Test 类中创建了正确的请求值 (CountryRequest),然后将它们传递给这个 create 方法,然后检查返回值。真的吗?还是我还需要测试 if 子句中的条件(countryRepository.existsByCodeIgnoreCase(countryCode))?
3. 当我测试 find 方法时,我认为我应该首先通过调用 create 方法来创建记录,而正确的位置是 @BeforeEach setup() {} 方法。这是真的吗?
【问题讨论】:
标签: java spring-boot testing junit integration-testing