Skip to content

Commit

Permalink
Added examples for the @restclienttest article, fixed mail port (euge…
Browse files Browse the repository at this point in the history
  • Loading branch information
forketyfork authored and pivovarit committed Jul 19, 2016
1 parent 3efa23b commit cb8f58a
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 3 deletions.
39 changes: 38 additions & 1 deletion spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.6.RELEASE</version>
<version>1.4.0.RC1</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

Expand Down Expand Up @@ -116,4 +116,41 @@

</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>
32 changes: 32 additions & 0 deletions spring-boot/src/main/java/org/baeldung/client/Details.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class SpringBootMailTest {

@Before
public void setUp() throws Exception {
final int TEST_PORT = 25;
final int TEST_PORT = 8025;
wiser = new Wiser(TEST_PORT);
wiser.start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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");

}

}
2 changes: 1 addition & 1 deletion spring-boot/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring.mail.host=localhost
spring.mail.port=25
spring.mail.port=8025
spring.mail.properties.mail.smtp.auth=false

0 comments on commit cb8f58a

Please sign in to comment.