Representational State Transfer is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called Rest API, provide interoperability between computer systems on the Internet.We are implementing it with Sping framework with the help of jackson.
EPL-2.0 License
Representational State Transfer is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called Rest API, provide interoperability between computer systems on the Internet.We are implementing it with Sping framework with the help of jackson.
This project shows different ways to test Spring Boot Application.
Libraries used:
spring boot web
spring boot test
There are multiple ways to test spring applications. See improvements in Spring Boot 1.4.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyTest {
// will have whatever port tomcat starts on
@Value("${local.server.port}")
private int port;
// ...
}
@Before
public void setup() {
logger.info("setting RestAssured port: " + port);
RestAssured.baseURI = "http://localhost:" + port;
}
@Test
public void getAllBooksShouldReturnListOfAllBooksWithRestAssured() throws Exception {
final String expectedTitle = "The Lightning Thief";
Response response = given().
when().
get(baseUrl + getAllBooksURL).
then().
statusCode(HttpServletResponse.SC_OK).
contentType("application/json").extract().response();
String res = response.asString();
jsonPath(res, hasSize(4));
}
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HomeRestControllerRestTemplateTest {
...
@Autowired
private TestRestTemplate restTemplate;
...
@Test
public void getAllBooksShouldReturnListOfAllBooks() throws Exception {
final String expectedTitle = "The Lightning Thief";
//another way to test
ResponseEntity<Book[]> responseEntity = restTemplate.getForEntity(baseUrl + getAllBooksURL
, Book[].class);
Book[] books = responseEntity.getBody();
List<Book> booksList = Arrays.asList(books);
assertThat(booksList.isEmpty(), is(false));
assertThat(booksList.size(), is(4));
assertThat(booksList.get(0).getTitle(), is(expectedTitle));
}
@RunWith(SpringRunner.class)
@WebMvcTest(HomeRestController.class)
public class HomeRestControllerMockMVCTest {
...
@Autowired
private MockMvc mockMvc;
@Test
public void getAllBooksShouldExist() throws Exception{
mockMvc.perform(get(baseUrl+getAllBooksURL).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
private ApplicationContext appContext;
Resource resource = appContext.getResource("classpath:data.json");
ObjectMapper mapper = new ObjectMapper();
Book[] b = mapper.readValue(resource.getFile(), Book[].class);