spring-boot 上的集成测试抛出连接被拒绝

Rob*_*rto 5 integration-testing spring-boot

我对 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);
    }
}
Run Code Online (Sandbox Code Playgroud)
  • 如果我在启动的应用程序上启动测试 - 测试正常

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

application.properties的单启动是一样的。

用于测试并位于资源和测试资源中。

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Fer*_*nix 9

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

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

  • @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)

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

你需要这个 maven 依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>        
</dependency>
Run Code Online (Sandbox Code Playgroud)

例子:

@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);
    }
}
Run Code Online (Sandbox Code Playgroud)

请阅读文档以获取更多参考


ℛɑƒ*_*ƒæĿ 8

对于使用@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是JUnit 4

  • 改变:@ExtendWith(SpringExtension.class)
  • 到:@RunWith(SpringRunner.class)

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

server:
  port: 8080
Run Code Online (Sandbox Code Playgroud)

或者如果有application.properties

server.port=8080
Run Code Online (Sandbox Code Playgroud)

参考: