简体   繁体   中英

Service not starting using Spring-boot during integration tests

I am writting my tests useing rest-assured and 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
}

In the log I see it says starting Tomcat on port and STarting service Tomcat but then it fails with this error -

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)

For web integration testing, you should use @WebIntegrationTest instead.

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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