1 package de.spring.webservices.rest.controller;
3 import static org.hamcrest.Matchers.any;
4 import static org.hamcrest.Matchers.is;
5 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
6 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
7 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
8 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
9 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
10 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
12 import javax.inject.Inject;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
18 //import org.springframework.boot.test.mock.mockito.MockBean;
19 import org.springframework.http.HttpHeaders;
20 import org.springframework.http.MediaType;
21 import org.springframework.test.context.junit4.SpringRunner;
22 import org.springframework.test.web.servlet.MockMvc;
23 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
24 import org.springframework.web.context.WebApplicationContext;
26 import com.fasterxml.jackson.databind.DeserializationFeature;
27 import com.fasterxml.jackson.databind.MapperFeature;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.SerializationFeature;
30 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
32 import de.spring.webservices.domain.Car;
35 @RunWith(SpringRunner.class)
36 @WebMvcTest(CarController.class)
37 public class CarControllerIntegrationTest {
39 // For injecting and mocking services which could be used in the Controller under test.
41 //private CarService carService;
44 private WebApplicationContext context;
47 private ObjectMapper objectMapper;
49 private MockMvc mockMvc;
53 mockMvc = MockMvcBuilders
54 .webAppContextSetup(context)
58 .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
59 .disable(MapperFeature.DEFAULT_VIEW_INCLUSION)
60 .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS)
61 .enable(SerializationFeature.INDENT_OUTPUT)
62 .registerModule(new JavaTimeModule());
66 public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
67 mockMvc.perform(get("/api/cars/")
68 .accept(MediaType.APPLICATION_JSON_UTF8))
70 .andExpect(status().isOk())
71 .andExpect(jsonPath("$[0].id", any(Integer.class)))
72 .andExpect(jsonPath("$[0].content", is("Car: 1")))
73 .andExpect(jsonPath("$[1].content", is("Car: 2")))
74 .andExpect(jsonPath("$[1].id", any(Integer.class)))
75 .andExpect(jsonPath("$[2].content", is("Car: 3")))
76 .andExpect(jsonPath("$[2].id", any(Integer.class)))
77 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
81 public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
82 mockMvc.perform(get("/api/cars/{id}", 1L)
83 .accept(MediaType.APPLICATION_JSON_UTF8))
85 .andExpect(status().isOk())
86 .andExpect(jsonPath("id", any(Integer.class)))
87 .andExpect(jsonPath("content", is("Car: 1")))
88 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
92 public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
93 Car car = new Car(2L, "nothing");
94 mockMvc.perform(post("/api/cars/")
95 .contentType(MediaType.APPLICATION_JSON_UTF8)
96 .content(objectMapper.writeValueAsString(car))
97 .accept(MediaType.APPLICATION_JSON_UTF8))
99 .andExpect(status().isCreated())
100 .andExpect(jsonPath("id", any(Integer.class)))
101 .andExpect(jsonPath("content", is("Car: 2")))
102 .andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/2"))
103 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));