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.mockito.BDDMockito.given;
6 import static org.mockito.Mockito.mock;
7 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
8 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
9 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
10 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
11 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
12 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.junit.Before;
18 import org.junit.Ignore;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.springframework.data.domain.PageImpl;
22 import org.springframework.data.domain.PageRequest;
23 import org.springframework.http.HttpHeaders;
24 import org.springframework.http.MediaType;
25 import org.springframework.test.context.ContextConfiguration;
26 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27 import org.springframework.test.web.servlet.MockMvc;
28 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
30 import com.fasterxml.jackson.core.JsonProcessingException;
31 import com.fasterxml.jackson.databind.ObjectMapper;
33 import de.spring.webservices.domain.Car;
34 import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
37 // jsonPath, how to: https://github.com/jayway/JsonPath | http://jsonpath.herokuapp.com/
40 @RunWith(SpringJUnit4ClassRunner.class)
41 @ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"})
42 public class CarControllerIntegrationTest {
43 private static final int PAGE = 2;
44 private static final int PAGE_SIZE = 10;
45 private static final String TEMPLATE = "Car: %s";
47 private AwesomeBusinessLogic awesomeBusinessLogic;
48 private CarController controller;
49 private MockMvc mockMvc;
53 awesomeBusinessLogic = mock(AwesomeBusinessLogic.class);
54 controller = new CarController(awesomeBusinessLogic);
55 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
59 public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
60 final List<Car> cars = new ArrayList<>();
61 cars.add(new Car(1L, String.format(TEMPLATE, 1)));
62 given(awesomeBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE))).willReturn(new PageImpl<>(cars));
64 mockMvc.perform(get("/api/cars/")
65 .accept(MediaType.APPLICATION_JSON_UTF8))
67 .andExpect(status().isOk())
68 .andExpect(jsonPath("$.content[0].id", any(Integer.class)))
69 .andExpect(jsonPath("$.content[0].content", is("Car: 1")))
70 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
74 public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
75 given(awesomeBusinessLogic.findById(1L)).willReturn(new Car(1L, String.format(TEMPLATE, 1)));
77 mockMvc.perform(get("/api/cars/{id}", 1L)
78 .accept(MediaType.APPLICATION_JSON_UTF8))
80 .andExpect(status().isOk())
81 .andExpect(jsonPath("id", any(Integer.class)))
82 .andExpect(jsonPath("content", is("Car: 1")))
83 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
87 public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
88 Car car = new Car(null, "nothing");
89 given(awesomeBusinessLogic.create(car)).willReturn(new Car(1L, String.format(TEMPLATE, 1)));
91 mockMvc.perform(post("/api/cars/")
92 .contentType(MediaType.APPLICATION_JSON_UTF8)
93 .content(asJsonString(car))
94 .accept(MediaType.APPLICATION_JSON_UTF8))
96 .andExpect(status().isCreated())
97 .andExpect(jsonPath("id", any(Integer.class)))
98 .andExpect(jsonPath("content", is("Car: 1")))
99 .andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/1"))
100 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
103 private static String asJsonString(final Object obj) throws JsonProcessingException {
104 final ObjectMapper mapper = new ObjectMapper();
105 return mapper.writeValueAsString(obj);