Skip to content

Commit cb8f58a

Browse files
forketyforkpivovarit
authored andcommitted
Added examples for the @restclienttest article, fixed mail port (eugenp#513)
1 parent 3efa23b commit cb8f58a

6 files changed

Lines changed: 139 additions & 3 deletions

File tree

‎spring-boot/pom.xml‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<parent>
1313
<groupId>org.springframework.boot</groupId>
1414
<artifactId>spring-boot-starter-parent</artifactId>
15-
<version>1.3.6.RELEASE</version>
15+
<version>1.4.0.RC1</version>
1616
<relativePath /> <!-- lookup parent from repository -->
1717
</parent>
1818

@@ -116,4 +116,41 @@
116116

117117
</build>
118118

119+
<repositories>
120+
<repository>
121+
<id>spring-snapshots</id>
122+
<name>Spring Snapshots</name>
123+
<url>https://repo.spring.io/snapshot</url>
124+
<snapshots>
125+
<enabled>true</enabled>
126+
</snapshots>
127+
</repository>
128+
<repository>
129+
<id>spring-milestones</id>
130+
<name>Spring Milestones</name>
131+
<url>https://repo.spring.io/milestone</url>
132+
<snapshots>
133+
<enabled>false</enabled>
134+
</snapshots>
135+
</repository>
136+
</repositories>
137+
<pluginRepositories>
138+
<pluginRepository>
139+
<id>spring-snapshots</id>
140+
<name>Spring Snapshots</name>
141+
<url>https://repo.spring.io/snapshot</url>
142+
<snapshots>
143+
<enabled>true</enabled>
144+
</snapshots>
145+
</pluginRepository>
146+
<pluginRepository>
147+
<id>spring-milestones</id>
148+
<name>Spring Milestones</name>
149+
<url>https://repo.spring.io/milestone</url>
150+
<snapshots>
151+
<enabled>false</enabled>
152+
</snapshots>
153+
</pluginRepository>
154+
</pluginRepositories>
155+
119156
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung.client;
2+
3+
public class Details {
4+
5+
private String name;
6+
7+
private String login;
8+
9+
public Details() {
10+
}
11+
12+
public Details(String name, String login) {
13+
this.name = name;
14+
this.login = login;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public String getLogin() {
26+
return login;
27+
}
28+
29+
public void setLogin(String login) {
30+
this.login = login;
31+
}
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.client;
2+
3+
import org.springframework.boot.web.client.RestTemplateBuilder;
4+
import org.springframework.stereotype.Service;
5+
import org.springframework.web.client.RestTemplate;
6+
7+
@Service
8+
public class DetailsServiceClient {
9+
10+
private final RestTemplate restTemplate;
11+
12+
public DetailsServiceClient(RestTemplateBuilder restTemplateBuilder) {
13+
restTemplate = restTemplateBuilder.build();
14+
}
15+
16+
public Details getUserDetails(String name) {
17+
return restTemplate.getForObject("/{name}/details", Details.class, name);
18+
}
19+
20+
}

‎spring-boot/src/test/java/org/baeldung/SpringBootMailTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class SpringBootMailTest {
3737

3838
@Before
3939
public void setUp() throws Exception {
40-
final int TEST_PORT = 25;
40+
final int TEST_PORT = 8025;
4141
wiser = new Wiser(TEST_PORT);
4242
wiser.start();
4343
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.baeldung.client;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
9+
import org.springframework.http.MediaType;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
import org.springframework.test.web.client.MockRestServiceServer;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
15+
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
16+
17+
@RunWith(SpringRunner.class)
18+
@RestClientTest(DetailsServiceClient.class)
19+
public class DetailsServiceClientTest {
20+
21+
@Autowired
22+
private DetailsServiceClient client;
23+
24+
@Autowired
25+
private MockRestServiceServer server;
26+
27+
@Autowired
28+
private ObjectMapper objectMapper;
29+
30+
@Before
31+
public void setUp() throws Exception {
32+
String detailsString = objectMapper.writeValueAsString(new Details("John Smith", "john"));
33+
this.server.expect(requestTo("/john/details"))
34+
.andRespond(withSuccess(detailsString, MediaType.APPLICATION_JSON));
35+
}
36+
37+
@Test
38+
public void whenCallingGetUserDetails_thenClientExecutesCorrectCall() throws Exception {
39+
40+
Details details = this.client.getUserDetails("john");
41+
42+
assertThat(details.getLogin()).isEqualTo("john");
43+
assertThat(details.getName()).isEqualTo("John Smith");
44+
45+
}
46+
47+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
spring.mail.host=localhost
2-
spring.mail.port=25
2+
spring.mail.port=8025
33
spring.mail.properties.mail.smtp.auth=false

0 commit comments

Comments
 (0)