Using SpringBootTest annotation
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:
Matt Damon
Updated on July 10, 2022Comments
-
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 viagradle test
so I can keep them independent. I've setup a gradle taskgradle 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 ajava.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 about 6 yearsSadly just adding in
@RunWith(SpringRunner.class)
hasn't fixed the issue :( -
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 about 6 yearsah 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.