[BAEL-1686] - Added integration testing

This commit is contained in:
Jorge Collado 2018-05-07 18:27:48 +02:00 committed by José Carlos Valero Sánchez
parent b3fd211763
commit 8a5ff707c5
3 changed files with 68 additions and 45 deletions

View File

@ -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);

View File

@ -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")));
}
}

View File

@ -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)));
}
}