BAEL-2221
This commit is contained in:
parent
1dfeb49045
commit
fcef80587c
|
@ -0,0 +1,38 @@
|
||||||
|
package org.baeldung.web.dto;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class EmployeeDto {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private Double salary;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getSalary() {
|
||||||
|
return salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSalary(Double salary) {
|
||||||
|
this.salary = salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public String toString() {
|
||||||
|
return "EmployeeDto{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", salary=" + salary + '}';
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
package org.baeldung.web.model;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
public class Employee {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
private String name;
|
||||||
|
private Double salary;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getSalary() {
|
||||||
|
return salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSalary(Double salary) {
|
||||||
|
this.salary = salary;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public String toString() {
|
||||||
|
return "Employee{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", salary=" + salary + '}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@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) && Objects.equals(name, employee.name) && Objects.equals(salary, employee.salary);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public int hashCode() {
|
||||||
|
|
||||||
|
return Objects.hash(id, name, salary);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package org.baeldung.web.service;
|
||||||
|
|
||||||
|
import org.baeldung.web.dto.EmployeeDto;
|
||||||
|
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 EmployeeDto getEmployee(String id) throws Exception {
|
||||||
|
|
||||||
|
Employee emp = null;
|
||||||
|
try {
|
||||||
|
|
||||||
|
ResponseEntity<Employee> resp = restTemplate.getForEntity(EMP_URL_PREFIX
|
||||||
|
+ URL_SEP + id, Employee.class);
|
||||||
|
|
||||||
|
if (resp == null || resp.getStatusCode() != HttpStatus.OK
|
||||||
|
|| resp.getBody() == null) {
|
||||||
|
|
||||||
|
throw new Exception("Employee details could not be fetched.");
|
||||||
|
}
|
||||||
|
|
||||||
|
emp = resp.getBody();
|
||||||
|
|
||||||
|
EmployeeDto dto = new EmployeeDto();
|
||||||
|
dto.setId(emp.getId());
|
||||||
|
dto.setName(emp.getName());
|
||||||
|
dto.setSalary(emp.getSalary());
|
||||||
|
return dto;
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error occurred while fetching employee details", e);
|
||||||
|
throw new Exception("Error occurred while fetching employee details", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
package org.baeldung.web.service;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import org.baeldung.SpringTestConfig;
|
||||||
|
import org.baeldung.web.dto.EmployeeDto;
|
||||||
|
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.MockRestServiceServer;
|
||||||
|
import org.springframework.test.web.client.match.MockRestRequestMatchers;
|
||||||
|
import org.springframework.test.web.client.response.MockRestResponseCreators;
|
||||||
|
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
|
||||||
|
EmployeeService empService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
RestTemplate restTemplate;
|
||||||
|
|
||||||
|
MockRestServiceServer mockServer;
|
||||||
|
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void initMocks() {
|
||||||
|
mockServer = MockRestServiceServer.createServer(restTemplate);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMockingIsDoneByMockRestServiceServer_whenGetIsCalled_shouldReturnMockedObject() throws Exception {
|
||||||
|
String id = "E001";
|
||||||
|
Employee emp = new Employee();
|
||||||
|
emp.setId(id);
|
||||||
|
emp.setName("Eric Simmons");
|
||||||
|
emp.setSalary(10000.00d);
|
||||||
|
//employeeDao.create(emp);
|
||||||
|
|
||||||
|
mockServer.expect(MockRestRequestMatchers.requestTo(new URI(EmployeeService.EMP_URL_PREFIX
|
||||||
|
+ EmployeeService.URL_SEP + id)))
|
||||||
|
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
|
||||||
|
.andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK)
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.body(mapper.writeValueAsString(emp)));
|
||||||
|
|
||||||
|
EmployeeDto employeeDto = empService.getEmployee(id);
|
||||||
|
logger.info("Employee received as: {}", employeeDto);
|
||||||
|
Assert.assertEquals(emp.getName(), employeeDto.getName());
|
||||||
|
Assert.assertEquals(emp.getId(), employeeDto.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,49 @@
|
||||||
|
package org.baeldung.web.service;
|
||||||
|
|
||||||
|
import org.baeldung.web.dto.EmployeeDto;
|
||||||
|
import org.baeldung.web.model.Employee;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.*;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
public class EmployeeServiceUnitTest {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceUnitTest.class);
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
RestTemplate restTemplate;
|
||||||
|
|
||||||
|
@Spy
|
||||||
|
@InjectMocks
|
||||||
|
EmployeeService empService = new EmployeeService();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void initMocks() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenMockingIsDoneByMockito_whenGetIsCalled_shouldReturnMockedObject() throws Exception {
|
||||||
|
String id = "E001";
|
||||||
|
Employee emp = new Employee();
|
||||||
|
emp.setId(id);
|
||||||
|
emp.setName("Eric Simmons");
|
||||||
|
emp.setSalary(10000.00d);
|
||||||
|
Mockito
|
||||||
|
.when(restTemplate.getForEntity(EmployeeService.EMP_URL_PREFIX
|
||||||
|
+ EmployeeService.URL_SEP + id, Employee.class))
|
||||||
|
.thenReturn(new ResponseEntity<Employee>(emp, HttpStatus.OK));
|
||||||
|
|
||||||
|
EmployeeDto employeeDto = empService.getEmployee(id);
|
||||||
|
logger.info("Employee received as: {}", employeeDto);
|
||||||
|
Assert.assertEquals(emp.getName(), employeeDto.getName());
|
||||||
|
Assert.assertEquals(emp.getSalary(), employeeDto.getSalary());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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>
|
Loading…
Reference in New Issue