Skip to content

Commit 61e90f5

Browse files
committed
Add tests to Data REST sample for search operations
Closes gh-1627
1 parent b798d00 commit 61e90f5

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

spring-boot-samples/spring-boot-sample-data-rest/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
<artifactId>spring-boot-starter-test</artifactId>
3838
<scope>test</scope>
3939
</dependency>
40+
<dependency>
41+
<groupId>com.jayway.jsonpath</groupId>
42+
<artifactId>json-path</artifactId>
43+
<scope>test</scope>
44+
</dependency>
4045
</dependencies>
4146
<build>
4247
<plugins>

spring-boot-samples/spring-boot-sample-data-rest/src/test/java/sample/data/jpa/SampleDataRestApplicationTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@
2929
import org.springframework.web.context.WebApplicationContext;
3030

3131
import static org.hamcrest.Matchers.containsString;
32+
import static org.hamcrest.Matchers.hasSize;
3233
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
3334
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
35+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
3436
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3537

3638
/**
3739
* Integration test to run the application.
3840
*
3941
* @author Oliver Gierke
42+
* @author Andy Wilkinson
4043
*/
4144
@RunWith(SpringJUnit4ClassRunner.class)
4245
@SpringApplicationConfiguration(classes = SampleDataRestApplication.class)
@@ -61,4 +64,22 @@ public void testHome() throws Exception {
6164
this.mvc.perform(get("/")).andExpect(status().isOk())
6265
.andExpect(content().string(containsString("hotels")));
6366
}
67+
68+
@Test
69+
public void findByNameAndCountry() throws Exception {
70+
71+
this.mvc.perform(
72+
get("/cities/search/findByNameAndCountryAllIgnoringCase?name=Melbourne&country=Australia"))
73+
.andExpect(status().isOk())
74+
.andExpect(jsonPath("_embedded.citys", hasSize(1)));
75+
}
76+
77+
@Test
78+
public void findByContaining() throws Exception {
79+
80+
this.mvc.perform(
81+
get("/cities/search/findByNameContainingAndCountryContainingAllIgnoringCase?name=&country=UK"))
82+
.andExpect(status().isOk())
83+
.andExpect(jsonPath("_embedded.citys", hasSize(3)));
84+
}
6485
}

spring-boot-samples/spring-boot-sample-data-rest/src/test/java/sample/data/jpa/service/CityRepositoryIntegrationTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@
2727
import sample.data.jpa.SampleDataRestApplication;
2828
import sample.data.jpa.domain.City;
2929

30+
import static org.hamcrest.Matchers.equalTo;
3031
import static org.hamcrest.Matchers.greaterThan;
3132
import static org.hamcrest.Matchers.is;
33+
import static org.hamcrest.Matchers.notNullValue;
3234
import static org.junit.Assert.assertThat;
3335

3436
/**
3537
* Integration tests for {@link CityRepository}.
3638
*
3739
* @author Oliver Gierke
40+
* @author Andy Wilkinson
3841
*/
3942
@RunWith(SpringJUnit4ClassRunner.class)
4043
@SpringApplicationConfiguration(classes = SampleDataRestApplication.class)
@@ -49,4 +52,20 @@ public void findsFirstPageOfCities() {
4952
Page<City> cities = this.repository.findAll(new PageRequest(0, 10));
5053
assertThat(cities.getTotalElements(), is(greaterThan(20L)));
5154
}
55+
56+
@Test
57+
public void findByNameAndCountry() {
58+
City city = this.repository.findByNameAndCountryAllIgnoringCase("Melbourne",
59+
"Australia");
60+
assertThat(city, notNullValue());
61+
assertThat(city.getName(), is(equalTo("Melbourne")));
62+
}
63+
64+
@Test
65+
public void findContaining() {
66+
Page<City> cities = this.repository
67+
.findByNameContainingAndCountryContainingAllIgnoringCase("", "UK",
68+
new PageRequest(0, 10));
69+
assertThat(cities.getTotalElements(), is(equalTo(3L)));
70+
}
5271
}

0 commit comments

Comments
 (0)