From da052c96d30997706a49ebca75e1cb05b02136b7 Mon Sep 17 00:00:00 2001 From: Swapan Pramanick Date: Wed, 17 Oct 2018 18:11:30 +0530 Subject: [PATCH] BAEL-2221 --- ...eServiceMockRestServiceServerUnitTest.java | 33 +++++++++++-------- .../web/service/EmployeeServiceUnitTest.java | 7 ++-- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/spring-resttemplate/src/test/java/org/baeldung/web/service/EmployeeServiceMockRestServiceServerUnitTest.java b/spring-resttemplate/src/test/java/org/baeldung/web/service/EmployeeServiceMockRestServiceServerUnitTest.java index 7c2f535fae..f04b0fbc2b 100644 --- a/spring-resttemplate/src/test/java/org/baeldung/web/service/EmployeeServiceMockRestServiceServerUnitTest.java +++ b/spring-resttemplate/src/test/java/org/baeldung/web/service/EmployeeServiceMockRestServiceServerUnitTest.java @@ -17,9 +17,13 @@ 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.test.web.client.match.MockRestRequestMatchers; -import org.springframework.test.web.client.response.MockRestResponseCreators; + +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; @@ -31,14 +35,14 @@ public class EmployeeServiceMockRestServiceServerUnitTest { private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceMockRestServiceServerUnitTest.class); @Autowired - EmployeeService empService; + private EmployeeService empService; @Autowired - RestTemplate restTemplate; + private RestTemplate restTemplate; - MockRestServiceServer mockServer; + private MockRestServiceServer mockServer; - ObjectMapper mapper = new ObjectMapper(); + private ObjectMapper mapper = new ObjectMapper(); @Before public void initMocks() { @@ -52,17 +56,20 @@ public class EmployeeServiceMockRestServiceServerUnitTest { 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))); + 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.verify(); Assert.assertEquals(emp.getName(), employeeDto.getName()); Assert.assertEquals(emp.getId(), employeeDto.getId()); } diff --git a/spring-resttemplate/src/test/java/org/baeldung/web/service/EmployeeServiceUnitTest.java b/spring-resttemplate/src/test/java/org/baeldung/web/service/EmployeeServiceUnitTest.java index aa323dd685..ac714bf6db 100644 --- a/spring-resttemplate/src/test/java/org/baeldung/web/service/EmployeeServiceUnitTest.java +++ b/spring-resttemplate/src/test/java/org/baeldung/web/service/EmployeeServiceUnitTest.java @@ -17,14 +17,13 @@ public class EmployeeServiceUnitTest { private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceUnitTest.class); @Mock - RestTemplate restTemplate; + private RestTemplate restTemplate; - @Spy @InjectMocks - EmployeeService empService = new EmployeeService(); + private EmployeeService empService = new EmployeeService(); @Before - public void initMocks() { + public void setup() { MockitoAnnotations.initMocks(this); }