Skip to content

Commit d2f78ec

Browse files
committed
Move Spring Data REST Starbucks example to Testcontainers.
1 parent 0d3df30 commit d2f78ec

File tree

4 files changed

+77
-29
lines changed

4 files changed

+77
-29
lines changed

rest/starbucks/pom.xml

+13
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@
9393
<artifactId>webjars-locator-core</artifactId>
9494
<scope>runtime</scope>
9595
</dependency>
96+
97+
<!-- Testcontainers -->
98+
<dependency>
99+
<groupId>org.springframework.boot</groupId>
100+
<artifactId>spring-boot-testcontainers</artifactId>
101+
<scope>test</scope>
102+
</dependency>
103+
104+
<dependency>
105+
<groupId>org.testcontainers</groupId>
106+
<artifactId>mongodb</artifactId>
107+
<scope>test</scope>
108+
</dependency>
96109

97110
</dependencies>
98111

rest/starbucks/src/test/java/example/springdata/rest/stores/StarbucksClient.java

+17-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,43 +26,30 @@
2626

2727
import org.junit.jupiter.api.Test;
2828
import org.springframework.beans.factory.annotation.Autowired;
29-
import org.springframework.boot.autoconfigure.SpringBootApplication;
3029
import org.springframework.boot.test.context.SpringBootTest;
3130
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
31+
import org.springframework.boot.test.web.client.TestRestTemplate;
3232
import org.springframework.boot.test.web.server.LocalServerPort;
33-
import org.springframework.context.annotation.Bean;
3433
import org.springframework.hateoas.EntityModel;
3534
import org.springframework.hateoas.MediaTypes;
3635
import org.springframework.hateoas.client.Traverson;
3736
import org.springframework.hateoas.server.core.TypeReferences.CollectionModelType;
3837
import org.springframework.hateoas.server.core.TypeReferences.EntityModelType;
3938
import org.springframework.hateoas.server.core.TypeReferences.PagedModelType;
40-
import org.springframework.http.RequestEntity;
41-
import org.springframework.web.client.RestOperations;
42-
import org.springframework.web.client.RestTemplate;
39+
import org.springframework.web.client.RestClient;
4340

4441
/**
4542
* A test case to discover the search resource and execute a predefined search with it.
4643
*
4744
* @author Oliver Gierke
4845
* @author Divya Srivastava
4946
*/
50-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
5147
@Slf4j
48+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
5249
class StarbucksClient {
5350

54-
@SpringBootApplication
55-
static class Config {
56-
57-
@Bean
58-
public RestTemplate restTemplate() {
59-
return new RestTemplate();
60-
}
61-
}
62-
6351
@LocalServerPort int port;
64-
65-
@Autowired RestOperations restOperations;
52+
@Autowired TestRestTemplate template;
6653

6754
private static final String SERVICE_URI = "http://localhost:%s/api";
6855

@@ -102,17 +89,23 @@ void accessServiceUsingRestTemplate() {
10289

10390
// Access root resource
10491

105-
var uri = URI.create(String.format(SERVICE_URI, port));
106-
var request = RequestEntity.get(uri).accept(HAL_JSON).build();
107-
var rootLinks = restOperations.exchange(request, new EntityModelType<>() {}).getBody();
108-
var links = rootLinks.getLinks();
92+
var client = RestClient.create(template.getRestTemplate());
93+
94+
var links = client.get()
95+
.uri(URI.create(String.format(SERVICE_URI, port)))
96+
.accept(HAL_JSON)
97+
.retrieve()
98+
.body(new EntityModelType<>() {})
99+
.getLinks();
109100

110101
// Follow stores link
111102

112103
var storesLink = links.getRequiredLink("stores").expand();
113104

114-
request = RequestEntity.get(URI.create(storesLink.getHref())).accept(HAL_JSON).build();
115-
var stores = restOperations.exchange(request, new CollectionModelType<Store>() {}).getBody();
105+
var stores = client.get().uri(storesLink.toUri())
106+
.accept(HAL_JSON)
107+
.retrieve()
108+
.body(new CollectionModelType<Store>() {});
116109

117110
stores.getContent().forEach(store -> log.info("{} - {}", store.name, store.address));
118111
}

rest/starbucks/src/test/java/example/springdata/rest/stores/StoreRepositoryIntegrationTests.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2021 the original author or authors.
2+
* Copyright 2014-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@
2424
import org.junit.jupiter.api.AfterEach;
2525
import org.junit.jupiter.api.BeforeEach;
2626
import org.junit.jupiter.api.Test;
27-
2827
import org.springframework.beans.factory.annotation.Autowired;
2928
import org.springframework.boot.test.context.SpringBootTest;
3029
import org.springframework.data.domain.PageRequest;
@@ -38,8 +37,8 @@
3837
* @author Oliver Gierke
3938
* @author Mark Paluch
4039
*/
41-
@SpringBootTest(classes = Application.class)
42-
public class StoreRepositoryIntegrationTests {
40+
@SpringBootTest(classes = { Application.class, TestApplication.class })
41+
class StoreRepositoryIntegrationTests {
4342

4443
@Autowired StoreRepository repository;
4544

@@ -50,7 +49,7 @@ public void clearDb() {
5049
}
5150

5251
@Test
53-
public void findsStoresByLocation() {
52+
void findsStoresByLocation() {
5453

5554
var location = new Point(-73.995146, 40.740337);
5655
var store = new Store(UUID.randomUUID(), "Foo", new Address("street", "city", "zip", location));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.rest.stores;
17+
18+
import org.springframework.boot.SpringApplication;
19+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.testcontainers.containers.MongoDBContainer;
23+
import org.testcontainers.utility.DockerImageName;
24+
25+
/**
26+
* @author Oliver Drotbohm
27+
*/
28+
@Configuration
29+
public class TestApplication {
30+
31+
@Bean
32+
@ServiceConnection
33+
MongoDBContainer mongoDBContainer() {
34+
return new MongoDBContainer(DockerImageName.parse("mongodb/mongodb-community-server"));
35+
}
36+
37+
public static void main(String[] args) {
38+
39+
SpringApplication.from(Application::main)
40+
.with(TestApplication.class)
41+
.run(args);
42+
}
43+
}

0 commit comments

Comments
 (0)