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 }
}