集成测试中的java测试条件和异常?
我已经编写了几个单元测试,现在转而在Java(SpringBoot)应用程序中编写集成测试。我们使用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我可以为服务或存储库类编写集成测试吗
2当我在上面的服务中测试create方法时,我想我只是在测试类中创建了正确的请求值(CountryRequest
),然后将它们传递给这个create方法,然后检查返回的值。这是真的吗?或者我还需要测试if子句(countryRepository.existsByCodeIgnoreCase(countryCode)
)中的条件吗
3当我测试find方法时,我想我应该首先通过调用create方法来创建记录,这个方法的正确位置是@BeforeEach setup() {}
方法。这是真的吗
# 1 楼答案