From 8a5ff707c5fe6b7dd58696749a1b44a901b0c628 Mon Sep 17 00:00:00 2001 From: Jorge Collado Date: Mon, 7 May 2018 18:27:48 +0200 Subject: [PATCH] [BAEL-1686] - Added integration testing --- .../main/java/com/baeldung/Application.java | 4 +- .../IntegrationTest.java | 66 +++++++++++++++++++ .../SpringBootIntegrationTests.java | 43 ------------ 3 files changed, 68 insertions(+), 45 deletions(-) create mode 100644 spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java delete mode 100644 spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/SpringBootIntegrationTests.java diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/Application.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/Application.java index db87e920e1..cae2d62f05 100644 --- a/spring-data-rest-querydsl/src/main/java/com/baeldung/Application.java +++ b/spring-data-rest-querydsl/src/main/java/com/baeldung/Application.java @@ -25,13 +25,13 @@ public class Application { @PostConstruct private void initializeData() { // Create John - final Address johnsAddress = new Address(UUID.randomUUID().toString(), "Fake Street 1", "Fake country"); + final Address johnsAddress = new Address(UUID.randomUUID().toString(), "Fake Street 1", "Fake Country"); addressRepository.save(johnsAddress); final Person john = new Person(UUID.randomUUID().toString(), "John", johnsAddress); personRepository.save(john); // Create Lisa - final Address lisasAddress = new Address(UUID.randomUUID().toString(), "Real Street 1", "Real country"); + final Address lisasAddress = new Address(UUID.randomUUID().toString(), "Real Street 1", "Real Country"); addressRepository.save(lisasAddress); final Person lisa = new Person(UUID.randomUUID().toString(), "Lisa", lisasAddress); personRepository.save(lisa); diff --git a/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java b/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java new file mode 100644 index 0000000000..63d0f56651 --- /dev/null +++ b/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java @@ -0,0 +1,66 @@ +package com.baeldung.springdatarestquerydsl; + +import com.baeldung.Application; +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.context.SpringBootTest; +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.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.nio.charset.Charset; + +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) @WebAppConfiguration +public class IntegrationTest { + + final MediaType contentType = + new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); + + @Autowired private WebApplicationContext webApplicationContext; + + private MockMvc mockMvc; + + @Before public void setupMockMvc() { + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } + + @Test public void givenRequestHasBeenMade_whenQueryOverNameAttribute_thenGetJohn() throws Exception { + // Get John + mockMvc.perform(get("/personQuery?name=John")).andExpect(status().isOk()).andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$", hasSize(1))).andExpect(jsonPath("$[0].name", is("John"))) + .andExpect(jsonPath("$[0].address.address", is("Fake Street 1"))) + .andExpect(jsonPath("$[0].address.country", is("Fake Country"))); + } + + @Test public void givenRequestHasBeenMade_whenQueryOverNameAttribute_thenGetLisa() throws Exception { + // Get Lisa + mockMvc.perform(get("/personQuery?name=Lisa")).andExpect(status().isOk()).andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$", hasSize(1))).andExpect(jsonPath("$[0].name", is("Lisa"))) + .andExpect(jsonPath("$[0].address.address", is("Real Street 1"))) + .andExpect(jsonPath("$[0].address.country", is("Real Country"))); + } + + @Test public void givenRequestHasBeenMade_whenQueryWithoutAttribute_thenCorrect() throws Exception { + final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), + Charset.forName("utf8")); + + mockMvc.perform(get("/personQuery")).andExpect(status().isOk()).andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$", hasSize(2))) + // Get John + .andExpect(jsonPath("$[0].name", is("John"))).andExpect(jsonPath("$[0].address.address", is("Fake Street 1"))) + .andExpect(jsonPath("$[0].address.country", is("Fake Country"))) + // Get Lisa + .andExpect(jsonPath("$[1].name", is("Lisa"))).andExpect(jsonPath("$[1].address.address", is("Real Street 1"))) + .andExpect(jsonPath("$[1].address.country", is("Real Country"))); + } +} diff --git a/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/SpringBootIntegrationTests.java b/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/SpringBootIntegrationTests.java deleted file mode 100644 index 803f0834bb..0000000000 --- a/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/SpringBootIntegrationTests.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.baeldung.springdatarestquerydsl; - -import com.baeldung.Application; -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.context.SpringBootTest; -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; - -import static org.hamcrest.Matchers.hasSize; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; - -@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) @WebAppConfiguration -public class SpringBootIntegrationTests { - - @Autowired private WebApplicationContext webApplicationContext; - - private MockMvc mockMvc; - - @Before public void setupMockMvc() { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - } - - @Test public void givenRequestHasBeenMade_whenMeetsAllOfGivenConditions_thenCorrect() throws Exception { - MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), - Charset.forName("utf8")); - - mockMvc.perform(MockMvcRequestBuilders.get("/personQuery?name=john")).andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$", hasSize(4))); - } -} - -