1 package de.spring.webservices.rest;
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 org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.springframework.http.HttpHeaders;
16 import org.springframework.http.MediaType;
17 import org.springframework.test.context.ContextConfiguration;
18 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
19 import org.springframework.test.web.servlet.MockMvc;
20 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
22 import com.fasterxml.jackson.core.JsonProcessingException;
23 import com.fasterxml.jackson.databind.ObjectMapper;
25 import de.spring.webservices.domain.Car;
28 @RunWith(SpringJUnit4ClassRunner.class)
29 @ContextConfiguration({ "classpath*:spring-configuration/mvc/rest/*.xml"})
30 public class CarControllerIntegrationTest {
31 private CarController controller;
32 private MockMvc mockMvc;
36 controller = new CarController();
37 mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
41 public void testWhenGetAllCarsThenRetrieveJsonValues() throws Exception {
42 mockMvc.perform(get("/api/cars/")
43 .accept(MediaType.APPLICATION_JSON_UTF8))
45 .andExpect(status().isOk())
46 .andExpect(jsonPath("$[0].id", any(Integer.class)))
47 .andExpect(jsonPath("$[0].content", is("Car: 1")))
48 .andExpect(jsonPath("$[1].content", is("Car: 2")))
49 .andExpect(jsonPath("$[1].id", any(Integer.class)))
50 .andExpect(jsonPath("$[2].content", is("Car: 3")))
51 .andExpect(jsonPath("$[2].id", any(Integer.class)))
52 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
56 public void testWhenGetOneCarThenRetrieveJsonValue() throws Exception {
57 mockMvc.perform(get("/api/cars/{id}", 1L)
58 .accept(MediaType.APPLICATION_JSON_UTF8))
60 .andExpect(status().isOk())
61 .andExpect(jsonPath("id", any(Integer.class)))
62 .andExpect(jsonPath("content", is("Car: 1")))
63 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
67 public void testWhenCreateNewCarThenRetrieveJsonValue() throws Exception {
68 Car car = new Car(2L, "nothing");
69 mockMvc.perform(post("/api/cars/")
70 .contentType(MediaType.APPLICATION_JSON_UTF8)
71 .content(asJsonString(car))
72 .accept(MediaType.APPLICATION_JSON_UTF8))
74 .andExpect(status().isCreated())
75 .andExpect(jsonPath("id", any(Integer.class)))
76 .andExpect(jsonPath("content", is("Car: 1")))
77 .andExpect(header().string(HttpHeaders.LOCATION, "/api/cars/1"))
78 .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
81 private static String asJsonString(final Object obj) throws JsonProcessingException {
82 final ObjectMapper mapper = new ObjectMapper();
83 return mapper.writeValueAsString(obj);