Using SpringBootTest annotation

12,024

By co-incidence I was working on something very similar today, and hopefully can provide some useful pointers.

When I compare with this code I think you are missing the @RunWith(SpringRunner.class) annotation !

Edit: other than moving your test to the same package as the "Application" class, you can try a Spring "first-principles" approach which side-steps all the black-magic of the Spring annotations. At least you can troubleshoot things better and control what's going on:

https://github.com/ptrthomas/spring-testing/blob/master/src/test/java/example/HelloE2ERestKarateTest.java

Share:
12,024
Matt Damon
Author by

Matt Damon

Updated on July 10, 2022

Comments

  • Matt Damon
    Matt Damon almost 2 years

    I've built an API using Spring Boot and have a few unit tests associated with it so far. As expected these are ran locally and when Jenkins builds the project via gradle build on the build server. I'm looking to add some integration tests to the project now using rest-assured so I can actually test request and responses but I'm not overly clear how to add them to the project.

    So far I've added an integrationTests folder to src/test and have told gradle to exclude the rest-assured tests when gradle build or via gradle test so I can keep them independent. I've setup a gradle task gradle integrationTest to trigger the integration tests which runs fine as long as the API is currently running.

    Apparently I can annotate the test class with @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) to instruct spring to start up an embedded server for testing automatically but it doesn't appear to be doing anything, the tests just fail with a java.net.ConnectException error until I start the API in another window. Is there anything wrong with the below sample code or am I missing something else?

    com.example.restassured;
    
    import org.junit.Test;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
    
    import static io.restassured.RestAssured.*;
    
    @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) 
    public class RunSomeTestsIT {
    
        @Test public void url_should_return_200_response() {
           get("/v1/test/123456").then().statusCode(200);
        }
    
    }
    

    build.gradle

    task integrationTest(type: Test) {
        include 'com/example/restassured/**'
        // Run all tests each time. Do not cache.
        outputs.upToDateWhen { false } 
    }
    
  • Matt Damon
    Matt Damon about 6 years
    Sadly just adding in @RunWith(SpringRunner.class) hasn't fixed the issue :(
  • Peter Thomas
    Peter Thomas about 6 years
    @MattDamon okay. try moving the test to the same package as the "Application" class entry point for your spring boot project. else I'm out of ideas, and you see my edit
  • Matt Damon
    Matt Damon about 6 years
    ah ha! So after running with -info I can see it is in fact starting up a server but it was quietly failing due to a liquibase parameter missing. I've created an application.properties file for the test resources and set liquibase to inactive there so now the server is starting. Just getting "connection refused" errors to work through now which I think are rest-assured problems.