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 name; | ||||
|         private Double salary; | ||||
| 
 | ||||
|         public Employee(String id, String name) { | ||||
|                 this.id = id; | ||||
|                 this.name = name; | ||||
|         } | ||||
| 
 | ||||
|         public Employee() { | ||||
|         } | ||||
| 
 | ||||
|         public String getId() { | ||||
|                 return id; | ||||
| @ -25,29 +32,17 @@ public class Employee { | ||||
|                 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); | ||||
|                 return Objects.equals(id, employee.id); | ||||
|         } | ||||
| 
 | ||||
|         @Override public int hashCode() { | ||||
| 
 | ||||
|                 return Objects.hash(id, name, salary); | ||||
|                 return Objects.hash(id); | ||||
|         } | ||||
| } | ||||
|  | ||||
| @ -1,6 +1,5 @@ | ||||
| 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; | ||||
| @ -21,31 +20,10 @@ public class EmployeeService { | ||||
|     @Autowired | ||||
|     private RestTemplate restTemplate; | ||||
| 
 | ||||
|     public EmployeeDto getEmployee(String id) throws Exception { | ||||
|     public Employee getEmployee(String id) { | ||||
| 
 | ||||
|         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); | ||||
|         } | ||||
|         ResponseEntity<Employee> resp = restTemplate.getForEntity("http://localhost:8080/employee/" + id, | ||||
|           Employee.class); | ||||
|         return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1,9 +1,12 @@ | ||||
| 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.dto.EmployeeDto; | ||||
| import org.baeldung.web.model.Employee; | ||||
| import org.junit.Assert; | ||||
| 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.web.client.ExpectedCount; | ||||
| 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 com.fasterxml.jackson.databind.ObjectMapper; | ||||
| @ -45,33 +43,24 @@ public class EmployeeServiceMockRestServiceServerUnitTest { | ||||
|     private ObjectMapper mapper = new ObjectMapper(); | ||||
| 
 | ||||
|     @Before | ||||
|     public void initMocks() { | ||||
|     public void init() { | ||||
|         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); | ||||
|         Employee emp = new Employee("E001", "Eric Simmons"); | ||||
| 
 | ||||
|         String fullUri = new StringBuilder().append(EMP_URL_PREFIX).append(URL_SEP) | ||||
|           .append(id).toString(); | ||||
| 
 | ||||
|         mockServer.expect(ExpectedCount.once(), requestTo(new URI(fullUri))) | ||||
|           .andExpect(method(HttpMethod.GET)) | ||||
|           .andRespond(withStatus(HttpStatus.OK) | ||||
|             .contentType(MediaType.APPLICATION_JSON) | ||||
|             .body(mapper.writeValueAsString(emp))); | ||||
| 
 | ||||
|         EmployeeDto employeeDto = empService.getEmployee(id); | ||||
|         logger.info("Employee received as: {}", employeeDto); | ||||
|         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.getName(), employeeDto.getName()); | ||||
|         Assert.assertEquals(emp.getId(), employeeDto.getId()); | ||||
|         Assert.assertEquals(emp, employee); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
| @ -1,11 +1,13 @@ | ||||
| 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.mockito.InjectMocks; | ||||
| import org.mockito.Mock; | ||||
| import org.mockito.Mockito; | ||||
| import org.mockito.MockitoAnnotations; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| import org.springframework.http.HttpStatus; | ||||
| @ -29,20 +31,13 @@ public class EmployeeServiceUnitTest { | ||||
| 
 | ||||
|     @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)); | ||||
|         Employee emp = new Employee("E001", "Eric Simmons"); | ||||
|         Mockito.when(restTemplate.getForEntity("http://localhost:8080/employee/E001", Employee.class)) | ||||
|           .thenReturn(new ResponseEntity(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()); | ||||
|         Employee employee = empService.getEmployee("E001"); | ||||
| 
 | ||||
|         Assert.assertEquals(emp, employee); | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user