【问题标题】:Integration tests on spring-boot throws Connection refusedspring-boot 上的集成测试抛出连接被拒绝
【发布时间】:2019-02-19 11:29:34
【问题描述】:

我在 Spring Boot 上进行了单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class CustomerControllerIT {
    private RestTemplate restTemplate = new RestTemplate();
    @Test
    public void findAllCustomers() throws Exception {
        ResponseEntity<List<Customer>> responseEntity = restTemplate.exchange(
                "http://localhost:8080/Customer", HttpMethod.GET, null,
                new ParameterizedTypeReference<List<Customer>>() {
                });
        List<Customer> list = responseEntity.getBody();
        Assert.assertEquals(list.size(), 0);
    }
}
  • 如果我在已启动的应用程序上启动测试 - 测试正常

  • 如果我尝试仅启动 IT,则会出现连接被拒绝错误

我的application.properties 与单次启动相同。

用于测试并位于resources和testResources中。

Application.class 是:

@ComponentScan({"mypackage"})
@EntityScan(basePackages = {"mypackage.model"})
@EnableJpaRepositories(basePackages = {"mypackage.persistence"})
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

【问题讨论】:

    标签: spring-boot junit integration-testing


    【解决方案1】:

    您必须使用正在运行的服务器运行测试

    如果你需要启动一个完整运行的服务器,你可以使用随机端口:

    • @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)

    每次测试运行时都会随机选择一个可用端口

    你需要这个 maven 依赖:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-test</artifactId>        
    </dependency>
    

    例子:

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
    public class TestRest {
    
        @Autowired
        private TestRestTemplate restTemplate;
    
        @Test
        public void findAllCustomers() throws Exception {
            ResponseEntity<List<Customer>> responseEntity = restTemplate.exchange(
                   "/Customer", HttpMethod.GET, null,
                   new ParameterizedTypeReference<List<Customer>>(){});
            List<Customer> list = responseEntity.getBody();
            Assert.assertEquals(list.size(), 0);
        }
    }
    

    更多信息请阅读文档reference

    【讨论】:

      【解决方案2】:

      要使用静态端口运行 @SpringBootTest 和 JUnit5 (Jupiter),您可以使用:

      @ExtendWith(SpringExtension.class)
      @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
      class MyUnitTests {
          @Test
          void checkActuator() throws Exception {
              final String url = "http://localhost:8080/actuator/health";
              @SuppressWarnings("rawtypes")
              ResponseEntity<Map> re = new RestTemplate().getForEntity(url, Map.class);
              System.out.println(re);
              assertEquals(HttpStatus.OK, re.getStatusCode());
              re.getStatusCode();
          }
      }
      

      如果您使用的是 JUnit 4

      • 更改:@ExtendWith(SpringExtension.class)
      • 收件人:@RunWith(SpringRunner.class)

      然后将端口属性添加到您的application.yml(文件夹内src/main/resources):

      server:
        port: 8080
      

      或者如果有application.properties:

      server.port=8080
      

      参考:

      【讨论】:

      • 简单的解决方案是最好的,乐于助人! ;)
      • 谢谢!对于我的 Junit5 测试,我将 @RunWith 更改为 @ExtendWith .,因此:@ExtendWith(SpringExtension.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) ,然后我的测试成功了。
      猜你喜欢
      • 2019-05-30
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 2020-01-19
      • 2020-12-31
      • 1970-01-01
      • 2018-09-22
      • 2019-06-06
      相关资源
      最近更新 更多