Added examples for the @RestClientTest article, fixed mail port (#513)

This commit is contained in:
Sergey Petunin 2016-07-19 19:01:18 +06:00 committed by Grzegorz Piwowarek
parent 3efa23b6b5
commit cb8f58a9e7
6 changed files with 139 additions and 3 deletions

View File

@ -12,7 +12,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version> <version>1.4.0.RC1</version>
<relativePath /> <!-- lookup parent from repository --> <relativePath /> <!-- lookup parent from repository -->
</parent> </parent>
@ -116,4 +116,41 @@
</build> </build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project> </project>

View File

@ -0,0 +1,32 @@
package org.baeldung.client;
public class Details {
private String name;
private String login;
public Details() {
}
public Details(String name, String login) {
this.name = name;
this.login = login;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
}

View File

@ -0,0 +1,20 @@
package org.baeldung.client;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class DetailsServiceClient {
private final RestTemplate restTemplate;
public DetailsServiceClient(RestTemplateBuilder restTemplateBuilder) {
restTemplate = restTemplateBuilder.build();
}
public Details getUserDetails(String name) {
return restTemplate.getForObject("/{name}/details", Details.class, name);
}
}

View File

@ -37,7 +37,7 @@ public class SpringBootMailTest {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
final int TEST_PORT = 25; final int TEST_PORT = 8025;
wiser = new Wiser(TEST_PORT); wiser = new Wiser(TEST_PORT);
wiser.start(); wiser.start();
} }

View File

@ -0,0 +1,47 @@
package org.baeldung.client;
import com.fasterxml.jackson.databind.ObjectMapper;
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.autoconfigure.web.client.RestClientTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@RunWith(SpringRunner.class)
@RestClientTest(DetailsServiceClient.class)
public class DetailsServiceClientTest {
@Autowired
private DetailsServiceClient client;
@Autowired
private MockRestServiceServer server;
@Autowired
private ObjectMapper objectMapper;
@Before
public void setUp() throws Exception {
String detailsString = objectMapper.writeValueAsString(new Details("John Smith", "john"));
this.server.expect(requestTo("/john/details"))
.andRespond(withSuccess(detailsString, MediaType.APPLICATION_JSON));
}
@Test
public void whenCallingGetUserDetails_thenClientExecutesCorrectCall() throws Exception {
Details details = this.client.getUserDetails("john");
assertThat(details.getLogin()).isEqualTo("john");
assertThat(details.getName()).isEqualTo("John Smith");
}
}

View File

@ -1,3 +1,3 @@
spring.mail.host=localhost spring.mail.host=localhost
spring.mail.port=25 spring.mail.port=8025
spring.mail.properties.mail.smtp.auth=false spring.mail.properties.mail.smtp.auth=false