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.
Merge pull request eugenp#5472 from swapanpramanick2004/BAEL-2221
Bael 2221
- Loading branch information
Showing
6 changed files
with
226 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
spring-resttemplate/src/main/java/org/baeldung/web/model/Employee.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,48 @@ | ||
package org.baeldung.web.model; | ||
|
||
import java.util.Date; | ||
import java.util.Objects; | ||
|
||
public class Employee { | ||
|
||
private String id; | ||
private String name; | ||
|
||
public Employee(String id, String name) { | ||
this.id = id; | ||
this.name = name; | ||
} | ||
|
||
public Employee() { | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
Employee employee = (Employee) o; | ||
return Objects.equals(id, employee.id); | ||
} | ||
|
||
@Override public int hashCode() { | ||
|
||
return Objects.hash(id); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
spring-resttemplate/src/main/java/org/baeldung/web/service/EmployeeService.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,29 @@ | ||
package org.baeldung.web.service; | ||
|
||
import org.baeldung.web.model.Employee; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Service | ||
public class EmployeeService { | ||
|
||
static final String EMP_URL_PREFIX = "http://localhost:8080/employee"; | ||
static final String URL_SEP = "/"; | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(EmployeeService.class); | ||
|
||
@Autowired | ||
private RestTemplate restTemplate; | ||
|
||
public Employee getEmployee(String id) { | ||
|
||
ResponseEntity<Employee> resp = restTemplate.getForEntity("http://localhost:8080/employee/" + id, | ||
Employee.class); | ||
return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
spring-resttemplate/src/test/java/org/baeldung/SpringTestConfig.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,19 @@ | ||
package org.baeldung; | ||
|
||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
@EnableAutoConfiguration | ||
@ComponentScan("org.baeldung") | ||
public class SpringTestConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
.../src/test/java/org/baeldung/web/service/EmployeeServiceMockRestServiceServerUnitTest.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,66 @@ | ||
package org.baeldung.web.service; | ||
|
||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; | ||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; | ||
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus; | ||
|
||
import java.net.URI; | ||
|
||
import org.baeldung.SpringTestConfig; | ||
import org.baeldung.web.model.Employee; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.client.ExpectedCount; | ||
import org.springframework.test.web.client.MockRestServiceServer; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@RunWith(SpringRunner.class) | ||
@ContextConfiguration(classes = SpringTestConfig.class) | ||
public class EmployeeServiceMockRestServiceServerUnitTest { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceMockRestServiceServerUnitTest.class); | ||
|
||
@Autowired | ||
private EmployeeService empService; | ||
|
||
@Autowired | ||
private RestTemplate restTemplate; | ||
|
||
private MockRestServiceServer mockServer; | ||
|
||
private ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@Before | ||
public void init() { | ||
mockServer = MockRestServiceServer.createServer(restTemplate); | ||
} | ||
|
||
@Test | ||
public void givenMockingIsDoneByMockRestServiceServer_whenGetIsCalled_shouldReturnMockedObject() throws Exception { | ||
Employee emp = new Employee("E001", "Eric Simmons"); | ||
|
||
mockServer.expect(ExpectedCount.once(), | ||
requestTo(new URI("http://localhost:8080/employee/E001"))) | ||
.andExpect(method(HttpMethod.GET)) | ||
.andRespond(withStatus(HttpStatus.OK) | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.body(mapper.writeValueAsString(emp))); | ||
|
||
Employee employee = empService.getEmployee("E001"); | ||
mockServer.verify(); | ||
Assert.assertEquals(emp, employee); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
spring-resttemplate/src/test/java/org/baeldung/web/service/EmployeeServiceUnitTest.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,41 @@ | ||
package org.baeldung.web.service; | ||
|
||
import org.baeldung.web.model.Employee; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.runners.MockitoJUnitRunner; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class EmployeeServiceUnitTest { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceUnitTest.class); | ||
|
||
@Mock | ||
private RestTemplate restTemplate; | ||
|
||
@InjectMocks | ||
private EmployeeService empService = new EmployeeService(); | ||
|
||
@Test | ||
public void givenMockingIsDoneByMockito_whenGetIsCalled_shouldReturnMockedObject() throws Exception { | ||
Employee emp = new Employee("E001", "Eric Simmons"); | ||
Mockito.when(restTemplate.getForEntity("http://localhost:8080/employee/E001", Employee.class)) | ||
.thenReturn(new ResponseEntity(emp, HttpStatus.OK)); | ||
|
||
Employee employee = empService.getEmployee("E001"); | ||
|
||
Assert.assertEquals(emp, employee); | ||
} | ||
|
||
} |
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,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n | ||
</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter"> | ||
<level value="DEBUG" /> | ||
</logger> | ||
|
||
<logger name="org.springframework" level="WARN" /> | ||
<logger name="org.springframework.transaction" level="WARN" /> | ||
|
||
<!-- in order to debug some marshalling issues, this needs to be TRACE --> | ||
<logger name="org.springframework.web.servlet.mvc" level="WARN" /> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
</configuration> |