Skip to content

Commit dbf2721

Browse files
author
Swapan Pramanick
committed
BAEL-2221
1 parent da052c9 commit dbf2721

5 files changed

Lines changed: 39 additions & 120 deletions

File tree

‎spring-resttemplate/src/main/java/org/baeldung/web/dto/EmployeeDto.java‎

Lines changed: 0 additions & 38 deletions
This file was deleted.

‎spring-resttemplate/src/main/java/org/baeldung/web/model/Employee.java‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@ public class Employee {
77

88
private String id;
99
private String name;
10-
private Double salary;
10+
11+
public Employee(String id, String name) {
12+
this.id = id;
13+
this.name = name;
14+
}
15+
16+
public Employee() {
17+
}
1118

1219
public String getId() {
1320
return id;
@@ -25,29 +32,17 @@ public void setName(String name) {
2532
this.name = name;
2633
}
2734

28-
public Double getSalary() {
29-
return salary;
30-
}
31-
32-
public void setSalary(Double salary) {
33-
this.salary = salary;
34-
}
35-
36-
@Override public String toString() {
37-
return "Employee{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", salary=" + salary + '}';
38-
}
39-
4035
@Override public boolean equals(Object o) {
4136
if (this == o)
4237
return true;
4338
if (o == null || getClass() != o.getClass())
4439
return false;
4540
Employee employee = (Employee) o;
46-
return Objects.equals(id, employee.id) && Objects.equals(name, employee.name) && Objects.equals(salary, employee.salary);
41+
return Objects.equals(id, employee.id);
4742
}
4843

4944
@Override public int hashCode() {
5045

51-
return Objects.hash(id, name, salary);
46+
return Objects.hash(id);
5247
}
5348
}
Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.baeldung.web.service;
22

3-
import org.baeldung.web.dto.EmployeeDto;
43
import org.baeldung.web.model.Employee;
54
import org.slf4j.Logger;
65
import org.slf4j.LoggerFactory;
@@ -21,31 +20,10 @@ public class EmployeeService {
2120
@Autowired
2221
private RestTemplate restTemplate;
2322

24-
public EmployeeDto getEmployee(String id) throws Exception {
23+
public Employee getEmployee(String id) {
2524

26-
Employee emp = null;
27-
try {
28-
29-
ResponseEntity<Employee> resp = restTemplate.getForEntity(EMP_URL_PREFIX
30-
+ URL_SEP + id, Employee.class);
31-
32-
if (resp == null || resp.getStatusCode() != HttpStatus.OK
33-
|| resp.getBody() == null) {
34-
35-
throw new Exception("Employee details could not be fetched.");
36-
}
37-
38-
emp = resp.getBody();
39-
40-
EmployeeDto dto = new EmployeeDto();
41-
dto.setId(emp.getId());
42-
dto.setName(emp.getName());
43-
dto.setSalary(emp.getSalary());
44-
return dto;
45-
46-
} catch (Exception e) {
47-
logger.error("Error occurred while fetching employee details", e);
48-
throw new Exception("Error occurred while fetching employee details", e);
49-
}
25+
ResponseEntity<Employee> resp = restTemplate.getForEntity("http://localhost:8080/employee/" + id,
26+
Employee.class);
27+
return resp.getStatusCode() == HttpStatus.OK ? resp.getBody() : null;
5028
}
5129
}
Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package org.baeldung.web.service;
22

3+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
4+
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
5+
import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
6+
37
import java.net.URI;
48

59
import org.baeldung.SpringTestConfig;
6-
import org.baeldung.web.dto.EmployeeDto;
710
import org.baeldung.web.model.Employee;
811
import org.junit.Assert;
912
import org.junit.Before;
@@ -19,11 +22,6 @@
1922
import org.springframework.test.context.junit4.SpringRunner;
2023
import org.springframework.test.web.client.ExpectedCount;
2124
import org.springframework.test.web.client.MockRestServiceServer;
22-
23-
import static org.baeldung.web.service.EmployeeService.EMP_URL_PREFIX;
24-
import static org.baeldung.web.service.EmployeeService.URL_SEP;
25-
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
26-
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;
2725
import org.springframework.web.client.RestTemplate;
2826

2927
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -45,33 +43,24 @@ public class EmployeeServiceMockRestServiceServerUnitTest {
4543
private ObjectMapper mapper = new ObjectMapper();
4644

4745
@Before
48-
public void initMocks() {
46+
public void init() {
4947
mockServer = MockRestServiceServer.createServer(restTemplate);
5048
}
5149

5250
@Test
5351
public void givenMockingIsDoneByMockRestServiceServer_whenGetIsCalled_shouldReturnMockedObject() throws Exception {
54-
String id = "E001";
55-
Employee emp = new Employee();
56-
emp.setId(id);
57-
emp.setName("Eric Simmons");
58-
emp.setSalary(10000.00d);
59-
60-
String fullUri = new StringBuilder().append(EMP_URL_PREFIX).append(URL_SEP)
61-
.append(id).toString();
62-
63-
mockServer.expect(ExpectedCount.once(), requestTo(new URI(fullUri)))
64-
.andExpect(method(HttpMethod.GET))
65-
.andRespond(withStatus(HttpStatus.OK)
66-
.contentType(MediaType.APPLICATION_JSON)
67-
.body(mapper.writeValueAsString(emp)));
52+
Employee emp = new Employee("E001", "Eric Simmons");
6853

69-
EmployeeDto employeeDto = empService.getEmployee(id);
70-
logger.info("Employee received as: {}", employeeDto);
54+
mockServer.expect(ExpectedCount.once(),
55+
requestTo(new URI("http://localhost:8080/employee/E001")))
56+
.andExpect(method(HttpMethod.GET))
57+
.andRespond(withStatus(HttpStatus.OK)
58+
.contentType(MediaType.APPLICATION_JSON)
59+
.body(mapper.writeValueAsString(emp)));
7160

61+
Employee employee = empService.getEmployee("E001");
7262
mockServer.verify();
73-
Assert.assertEquals(emp.getName(), employeeDto.getName());
74-
Assert.assertEquals(emp.getId(), employeeDto.getId());
63+
Assert.assertEquals(emp, employee);
7564
}
7665

7766
}
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package org.baeldung.web.service;
22

3-
import org.baeldung.web.dto.EmployeeDto;
43
import org.baeldung.web.model.Employee;
54
import org.junit.Assert;
65
import org.junit.Before;
76
import org.junit.Test;
8-
import org.mockito.*;
7+
import org.mockito.InjectMocks;
8+
import org.mockito.Mock;
9+
import org.mockito.Mockito;
10+
import org.mockito.MockitoAnnotations;
911
import org.slf4j.Logger;
1012
import org.slf4j.LoggerFactory;
1113
import org.springframework.http.HttpStatus;
@@ -29,20 +31,13 @@ public void setup() {
2931

3032
@Test
3133
public void givenMockingIsDoneByMockito_whenGetIsCalled_shouldReturnMockedObject() throws Exception {
32-
String id = "E001";
33-
Employee emp = new Employee();
34-
emp.setId(id);
35-
emp.setName("Eric Simmons");
36-
emp.setSalary(10000.00d);
37-
Mockito
38-
.when(restTemplate.getForEntity(EmployeeService.EMP_URL_PREFIX
39-
+ EmployeeService.URL_SEP + id, Employee.class))
40-
.thenReturn(new ResponseEntity<Employee>(emp, HttpStatus.OK));
41-
42-
EmployeeDto employeeDto = empService.getEmployee(id);
43-
logger.info("Employee received as: {}", employeeDto);
44-
Assert.assertEquals(emp.getName(), employeeDto.getName());
45-
Assert.assertEquals(emp.getSalary(), employeeDto.getSalary());
34+
Employee emp = new Employee("E001", "Eric Simmons");
35+
Mockito.when(restTemplate.getForEntity("http://localhost:8080/employee/E001", Employee.class))
36+
.thenReturn(new ResponseEntity(emp, HttpStatus.OK));
37+
38+
Employee employee = empService.getEmployee("E001");
39+
40+
Assert.assertEquals(emp, employee);
4641
}
4742

4843
}

0 commit comments

Comments
 (0)