forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added examples for the @restclienttest article, fixed mail port (euge…
- Loading branch information
1 parent
3efa23b
commit cb8f58a
Showing
6 changed files
with
139 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
spring-boot/src/main/java/org/baeldung/client/Details.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
spring-boot/src/main/java/org/baeldung/client/DetailsServiceClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
spring-boot/src/test/java/org/baeldung/client/DetailsServiceClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |