在集成测试期间服务未使用 Spring-boot 启动

Service not starting using Spring-boot during integration tests

我正在使用 rest-assured 和 spring-boot 编写我的测试。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApplicationSErvice.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")//dynamically pick up ports
public class MyTestClass{
 @Value("${local.server.port}")
    int port;

    @Before
    public void setup(){
        RestAssured.port=port;
    }
@Test
public void testMethod(){
//asserting
}

在日志中我看到它说在端口上启动 Tomcat 和启动服务 Tomcat 但随后失败并出现此错误 -

ERROR 8480 --- [cat-startStop-1] org.apache.catalina.core.ContainerBase   : A child container failed during start

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/service]]
    at java.util.concurrent.FutureTask.report(FutureTask.java:122)
    at java.util.concurrent.FutureTask.get(FutureTask.java:188)

对于 Web 集成测试,您应该使用 @WebIntegrationTest

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebIntegrationTest
public class ApplicationTest {

    @Value("${local.server.port}")
    private int port;

    @Before
    public void setup() {
        RestAssured.baseURI = "http://localhost:" + port;
    }

    @Test
    public void testStatus() {
        given().contentType(ContentType.JSON).get("/greeting").prettyPeek().then().statusCode(200);
    }

    @Test
    public void testMessage() {
        given().contentType(ContentType.JSON).get("/greeting").then()
            .body("content", is("Hello, World!"));
    }

}