2016-07-16 00:48:09 +03:00
|
|
|
package org.baeldung;
|
|
|
|
|
|
2017-01-06 18:01:48 +01:00
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
|
|
import static org.hamcrest.Matchers.hasSize;
|
|
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
|
|
|
|
|
2017-01-09 08:26:31 +01:00
|
|
|
import org.baeldung.domain.Modes;
|
2016-07-16 00:48:09 +03:00
|
|
|
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.SpringApplicationConfiguration;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
|
|
|
import org.springframework.test.context.web.WebAppConfiguration;
|
|
|
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
|
|
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
|
|
|
|
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
|
|
|
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
|
|
|
|
import org.springframework.web.context.WebApplicationContext;
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringJUnit4ClassRunner.class)
|
|
|
|
|
@SpringApplicationConfiguration(classes = Application.class)
|
|
|
|
|
@WebAppConfiguration
|
2016-10-20 13:44:16 +02:00
|
|
|
public class SpringBootApplicationIntegrationTest {
|
2016-07-16 00:48:09 +03:00
|
|
|
@Autowired
|
|
|
|
|
private WebApplicationContext webApplicationContext;
|
|
|
|
|
private MockMvc mockMvc;
|
|
|
|
|
|
|
|
|
|
@Before
|
|
|
|
|
public void setupMockMvc() {
|
2016-10-12 08:02:05 +03:00
|
|
|
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
|
2016-07-16 00:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception {
|
2016-10-12 08:02:05 +03:00
|
|
|
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
2016-07-16 00:48:09 +03:00
|
|
|
|
2016-10-12 08:02:05 +03:00
|
|
|
mockMvc.perform(MockMvcRequestBuilders.get("/entity/all")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$", hasSize(4)));
|
2016-07-16 00:48:09 +03:00
|
|
|
}
|
2017-01-06 18:01:48 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect() throws Exception {
|
|
|
|
|
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
|
|
|
|
|
|
|
|
|
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30"))
|
|
|
|
|
.andExpect(MockMvcResultMatchers.status().isOk())
|
|
|
|
|
.andExpect(MockMvcResultMatchers.content().contentType(contentType))
|
|
|
|
|
.andExpect(jsonPath("$.id", equalTo(1)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-07 17:31:53 +01:00
|
|
|
@Test
|
|
|
|
|
public void givenRequestHasBeenMade_whenMeetsFindByModeOfGivenConditions_thenCorrect() throws Exception {
|
|
|
|
|
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
|
|
|
|
|
2017-01-09 08:26:31 +01:00
|
|
|
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name()))
|
2017-01-07 17:31:53 +01:00
|
|
|
.andExpect(MockMvcResultMatchers.status().isOk())
|
|
|
|
|
.andExpect(MockMvcResultMatchers.content().contentType(contentType))
|
|
|
|
|
.andExpect(jsonPath("$.id", equalTo(1)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 18:01:48 +01:00
|
|
|
@Test
|
|
|
|
|
public void givenRequestHasBeenMade_whenMeetsFindByVersionOfGivenConditions_thenCorrect() throws Exception {
|
|
|
|
|
MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8"));
|
|
|
|
|
|
|
|
|
|
mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0"))
|
|
|
|
|
.andExpect(MockMvcResultMatchers.status().isOk())
|
|
|
|
|
.andExpect(MockMvcResultMatchers.content().contentType(contentType))
|
|
|
|
|
.andExpect(jsonPath("$.id", equalTo(1)));
|
|
|
|
|
}
|
2016-07-16 00:48:09 +03:00
|
|
|
}
|