简体   繁体   English

集成测试中的测试条件和异常?

[英]Testing conditions and exceptions in Integration Tests?

I have written several Unit Tests and now switched to write Integration Test in our Java (Spring Boot) app.我已经编写了几个单元测试,现在转而在我们的 Java (Spring Boot) 应用程序中编写集成测试。 We use JUnit and Mockito libraries for testing.我们使用 JUnit 和 Mockito 库进行测试。

As far as I know, Integration Tests check the entire rings rather than a function.据我所知,集成测试检查整个环而不是 function。 However, I am confused that if I should also check the if conditions in the methods while integration testing.但是,我很困惑,如果我还应该在集成测试时检查方法中的 if 条件。 Here is an example service method:这是一个示例服务方法:

@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);
}

My questions are:我的问题是:

1. Can I write integration test for a Service or a Repository class? 1.我可以为服务或存储库 class 编写集成测试吗?

2. when I test create method in my service above, I think I just create the proper request values ( CountryRequest ) in my Test class, then pass them to this create method and then check the returned value. 2.当我在上面的服务中测试 create 方法时,我想我只是在我的 Test class 中创建了正确的请求值( CountryRequest ),然后将它们传递给这个 create 方法,然后检查返回的值。 Is that true?真的吗? Or do I also need to test the condition in the if clause ( countryRepository.existsByCodeIgnoreCase(countryCode) )?还是我还需要测试 if 子句( countryRepository.existsByCodeIgnoreCase(countryCode) )中的条件?

3. When I test find methods, I think I should first create record by calling create method and the proper place for this is @BeforeEach setup() {} method. 3.我在测试find方法的时候,我想我应该先调用create方法来创建记录,这个合适的地方是@BeforeEach setup() {}方法。 Is that true?真的吗?

  1. If you wrote Unit tests that made sure, your services and repositories are working correctly (for example by validation and parameterized tests) I believe, you don't have to write integration tests for them.如果您编写的单元测试可以确保您的服务和存储库正常工作(例如通过验证和参数化测试),我相信您不必为它们编写集成测试。 You should write integration tests to check the behavior of your app.您应该编写集成测试来检查您的应用程序的行为。 By testing if your controller is working correctly you will also check if service and repo are ok.通过测试您的 controller 是否正常工作,您还将检查服务和回购是否正常。
  2. I believe unit test should check it.我相信单元测试应该检查它。
  3. Do you ask if you should create record in db?你问是否应该在数据库中创建记录? If you want to test if repository is correctly communicating with service and it with controller, you have to do it with some data.如果您想测试存储库是否与服务正确通信以及与 controller 通信,您必须使用一些数据来完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM