Service not starting using Spring-boot during integration tests

2k views Asked by At

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)
1

There are 1 answers

3
Eddú Meléndez On BEST ANSWER

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

}