BAEL-2221
This commit is contained in:
		
							parent
							
								
									da052c96d3
								
							
						
					
					
						commit
						dbf2721d25
					
				| @ -1,38 +0,0 @@ | |||||||
| 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 + '}'; |  | ||||||
|         } |  | ||||||
| } |  | ||||||
| @ -7,7 +7,14 @@ public class Employee { | |||||||
| 
 | 
 | ||||||
|         private String id; |         private String id; | ||||||
|         private String name; |         private String name; | ||||||
|         private Double salary; | 
 | ||||||
|  |         public Employee(String id, String name) { | ||||||
|  |                 this.id = id; | ||||||
|  |                 this.name = name; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         public Employee() { | ||||||
|  |         } | ||||||
| 
 | 
 | ||||||
|         public String getId() { |         public String getId() { | ||||||
|                 return id; |                 return id; | ||||||
| @ -25,29 +32,17 @@ public class Employee { | |||||||
|                 this.name = 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) { |         @Override public boolean equals(Object o) { | ||||||
|                 if (this == o) |                 if (this == o) | ||||||
|                         return true; |                         return true; | ||||||
|                 if (o == null || getClass() != o.getClass()) |                 if (o == null || getClass() != o.getClass()) | ||||||
|                         return false; |                         return false; | ||||||
|                 Employee employee = (Employee) o; |                 Employee employee = (Employee) o; | ||||||
|                 return Objects.equals(id, employee.id) && Objects.equals(name, employee.name) && Objects.equals(salary, employee.salary); |                 return Objects.equals(id, employee.id); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override public int hashCode() { |         @Override public int hashCode() { | ||||||
| 
 | 
 | ||||||
|                 return Objects.hash(id, name, salary); |                 return Objects.hash(id); | ||||||
|         } |         } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,6 +1,5 @@ | |||||||
| package org.baeldung.web.service; | package org.baeldung.web.service; | ||||||
| 
 | 
 | ||||||
| import org.baeldung.web.dto.EmployeeDto; |  | ||||||
| import org.baeldung.web.model.Employee; | import org.baeldung.web.model.Employee; | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
| @ -21,31 +20,10 @@ public class EmployeeService { | |||||||
|     @Autowired |     @Autowired | ||||||
|     private RestTemplate restTemplate; |     private RestTemplate restTemplate; | ||||||
| 
 | 
 | ||||||
|     public EmployeeDto getEmployee(String id) throws Exception { |     public Employee getEmployee(String id) { | ||||||
| 
 | 
 | ||||||
|         Employee emp = null; |         ResponseEntity<Employee> resp = restTemplate.getForEntity("http://localhost:8080/employee/" + id, | ||||||
|         try { |           Employee.class); | ||||||
| 
 |         return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null; | ||||||
|             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); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,9 +1,12 @@ | |||||||
| package org.baeldung.web.service; | 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 java.net.URI; | ||||||
| 
 | 
 | ||||||
| import org.baeldung.SpringTestConfig; | import org.baeldung.SpringTestConfig; | ||||||
| import org.baeldung.web.dto.EmployeeDto; |  | ||||||
| import org.baeldung.web.model.Employee; | import org.baeldung.web.model.Employee; | ||||||
| import org.junit.Assert; | import org.junit.Assert; | ||||||
| import org.junit.Before; | import org.junit.Before; | ||||||
| @ -19,11 +22,6 @@ import org.springframework.test.context.ContextConfiguration; | |||||||
| import org.springframework.test.context.junit4.SpringRunner; | import org.springframework.test.context.junit4.SpringRunner; | ||||||
| import org.springframework.test.web.client.ExpectedCount; | import org.springframework.test.web.client.ExpectedCount; | ||||||
| import org.springframework.test.web.client.MockRestServiceServer; | import org.springframework.test.web.client.MockRestServiceServer; | ||||||
| 
 |  | ||||||
| import static org.baeldung.web.service.EmployeeService.EMP_URL_PREFIX; |  | ||||||
| import static org.baeldung.web.service.EmployeeService.URL_SEP; |  | ||||||
| import static org.springframework.test.web.client.match.MockRestRequestMatchers.*; |  | ||||||
| import static org.springframework.test.web.client.response.MockRestResponseCreators.*; |  | ||||||
| import org.springframework.web.client.RestTemplate; | import org.springframework.web.client.RestTemplate; | ||||||
| 
 | 
 | ||||||
| import com.fasterxml.jackson.databind.ObjectMapper; | import com.fasterxml.jackson.databind.ObjectMapper; | ||||||
| @ -45,33 +43,24 @@ public class EmployeeServiceMockRestServiceServerUnitTest { | |||||||
|     private ObjectMapper mapper = new ObjectMapper(); |     private ObjectMapper mapper = new ObjectMapper(); | ||||||
| 
 | 
 | ||||||
|     @Before |     @Before | ||||||
|     public void initMocks() { |     public void init() { | ||||||
|         mockServer = MockRestServiceServer.createServer(restTemplate); |         mockServer = MockRestServiceServer.createServer(restTemplate); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenMockingIsDoneByMockRestServiceServer_whenGetIsCalled_shouldReturnMockedObject() throws Exception { |     public void givenMockingIsDoneByMockRestServiceServer_whenGetIsCalled_shouldReturnMockedObject() throws Exception { | ||||||
|         String id = "E001"; |         Employee emp = new Employee("E001", "Eric Simmons"); | ||||||
|         Employee emp = new Employee(); |  | ||||||
|         emp.setId(id); |  | ||||||
|         emp.setName("Eric Simmons"); |  | ||||||
|         emp.setSalary(10000.00d); |  | ||||||
| 
 | 
 | ||||||
|         String fullUri = new StringBuilder().append(EMP_URL_PREFIX).append(URL_SEP) |         mockServer.expect(ExpectedCount.once(), | ||||||
|           .append(id).toString(); |           requestTo(new URI("http://localhost:8080/employee/E001"))) | ||||||
| 
 |             .andExpect(method(HttpMethod.GET)) | ||||||
|         mockServer.expect(ExpectedCount.once(), requestTo(new URI(fullUri))) |             .andRespond(withStatus(HttpStatus.OK) | ||||||
|           .andExpect(method(HttpMethod.GET)) |               .contentType(MediaType.APPLICATION_JSON) | ||||||
|           .andRespond(withStatus(HttpStatus.OK) |               .body(mapper.writeValueAsString(emp))); | ||||||
|             .contentType(MediaType.APPLICATION_JSON) |  | ||||||
|             .body(mapper.writeValueAsString(emp))); |  | ||||||
| 
 |  | ||||||
|         EmployeeDto employeeDto = empService.getEmployee(id); |  | ||||||
|         logger.info("Employee received as: {}", employeeDto); |  | ||||||
| 
 | 
 | ||||||
|  |         Employee employee = empService.getEmployee("E001"); | ||||||
|         mockServer.verify(); |         mockServer.verify(); | ||||||
|         Assert.assertEquals(emp.getName(), employeeDto.getName()); |         Assert.assertEquals(emp, employee); | ||||||
|         Assert.assertEquals(emp.getId(), employeeDto.getId()); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,11 +1,13 @@ | |||||||
| package org.baeldung.web.service; | package org.baeldung.web.service; | ||||||
| 
 | 
 | ||||||
| import org.baeldung.web.dto.EmployeeDto; |  | ||||||
| import org.baeldung.web.model.Employee; | import org.baeldung.web.model.Employee; | ||||||
| import org.junit.Assert; | import org.junit.Assert; | ||||||
| import org.junit.Before; | import org.junit.Before; | ||||||
| import org.junit.Test; | import org.junit.Test; | ||||||
| import org.mockito.*; | import org.mockito.InjectMocks; | ||||||
|  | import org.mockito.Mock; | ||||||
|  | import org.mockito.Mockito; | ||||||
|  | import org.mockito.MockitoAnnotations; | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
| import org.springframework.http.HttpStatus; | import org.springframework.http.HttpStatus; | ||||||
| @ -29,20 +31,13 @@ public class EmployeeServiceUnitTest { | |||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void givenMockingIsDoneByMockito_whenGetIsCalled_shouldReturnMockedObject() throws Exception { |     public void givenMockingIsDoneByMockito_whenGetIsCalled_shouldReturnMockedObject() throws Exception { | ||||||
|         String id = "E001"; |         Employee emp = new Employee("E001", "Eric Simmons"); | ||||||
|         Employee emp = new Employee(); |         Mockito.when(restTemplate.getForEntity("http://localhost:8080/employee/E001", Employee.class)) | ||||||
|         emp.setId(id); |           .thenReturn(new ResponseEntity(emp, HttpStatus.OK)); | ||||||
|         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); |         Employee employee = empService.getEmployee("E001"); | ||||||
|         logger.info("Employee received as: {}", employeeDto); | 
 | ||||||
|         Assert.assertEquals(emp.getName(), employeeDto.getName()); |         Assert.assertEquals(emp, employee); | ||||||
|         Assert.assertEquals(emp.getSalary(), employeeDto.getSalary()); |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user