Get familiar with the Spring Testing and the Spring Boot Testing features.
Time: 20 minutes.
Code Snippet Manager JPA REST Testing
In this lab you will use the code from the Code Snippet Manager JPA Rest project. You will adding some tests.
-
Open a browser and hit the url: http://start.spring.io
-
Click the Switch to the full version link.
-
Fill out the Code Snippet Manager JPA REST Test Project metadata with (See Figure 1.0):
Table 1. Code Snippet Manager JPA REST Test App - metadata Property Value Group:
io.pivotal.workshop
Artifact:
code-snippet-manager-jpa-rest-test
Name:
code-snippet-manager-jpa-rest-test
Package Name:
io.pivotal.workshop.snippet
Dependencies:
Web, DevTools, Groovy Templates, JPA, Rest Repositories, H2, MySQL
Spring Boot:
2.0.0.M7
Figure 1.0: Spring Initializr - http://start.spring.io/TipYou can choose either Maven or Gradle project types. -
Type Web, DevTools, Groovy Templates, JPA, Rest Repositories, H2, and MySQL in the Dependencies field and press Enter.
-
Click the Generate Project button.
-
Unzip the file in any directory you want.
-
Import your project in any IDE you want.
-
Copy all the packages (with code) into the new project. None of the code will be changed.
-
In this lab you will use also a 3rd Party library called: Rest Assured. Add the following dependency to your pom.xml or build.gradle
pom.xml<dependency> <groupId>io.rest-assured</groupId> <artifactId>spring-mock-mvc</artifactId> <version>3.0.0</version> <scope>test</scope> </dependency>
build.gradletestCompile('io.rest-assured:spring-mock-mvc:3.0.0')
-
Add in the src/test/java folder in the package io.pivotal.workshop.snippet the following integration test:
io.pivotal.workshop.snippet.IntegrationTest.javapackage io.pivotal.workshop.snippet; import static io.restassured.module.mockmvc.RestAssuredMockMvc.when; import static org.assertj.core.api.Assertions.assertThat; import static org.hamcrest.CoreMatchers.equalTo; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import java.util.Collection; import java.util.Collections; import java.util.stream.StreamSupport; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.core.ParameterizedTypeReference; import org.springframework.hateoas.Resource; import org.springframework.hateoas.Resources; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import io.pivotal.workshop.snippet.domain.Snippet; import io.restassured.module.mockmvc.RestAssuredMockMvc; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class IntegrationTest { @Autowired private TestRestTemplate restTemplate; @Autowired private WebApplicationContext context; @Before public void setUp() { RestAssuredMockMvc.mockMvc(MockMvcBuilders.webAppContextSetup(context).build()); } @Test public void homePageTest() { String body = this.restTemplate.getForObject("/", String.class); assertThat(body).contains("Hello World"); } @Test public void linksTests() { when().get("/api/snippets").then().assertThat(status().isOk()).body("_links.self.href", equalTo("http://localhost/api/snippets")); } }
Take a look at the code and see that is exposing the RestAssuredMockMvc and also the usage of the TestRestTemplate.
Challenges: Integration Tests
-
Analyze the code and run the test, either using your IDE, maven or gradle.
-
Add a new test method restControllerTest that asserts the HAL/JSON response.
TipUse the ResponseEntity<Resources<Resource<Snippet>>> instance and the restTemplate.exchange method.
Challenges: Slices
-
Add a new JsonTest class and use the @JsonTest annotation. You will use the JacksonTester<T> class and the assertj library to do the assertions.
-
Add a new JpaTests class and use the @DataJpaTest annotation. You will use the TestEntityManager class and the hamcrest library to do the assertions.