BAEL-2221

This commit is contained in:
Swapan Pramanick 2018-10-17 18:11:30 +05:30
parent 9713832dc6
commit da052c96d3
2 changed files with 23 additions and 17 deletions

View File

@ -17,9 +17,13 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration; 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.MockRestServiceServer; 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 org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
@ -31,14 +35,14 @@ public class EmployeeServiceMockRestServiceServerUnitTest {
private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceMockRestServiceServerUnitTest.class); private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceMockRestServiceServerUnitTest.class);
@Autowired @Autowired
EmployeeService empService; private EmployeeService empService;
@Autowired @Autowired
RestTemplate restTemplate; private RestTemplate restTemplate;
MockRestServiceServer mockServer; private MockRestServiceServer mockServer;
ObjectMapper mapper = new ObjectMapper(); private ObjectMapper mapper = new ObjectMapper();
@Before @Before
public void initMocks() { public void initMocks() {
@ -52,17 +56,20 @@ public class EmployeeServiceMockRestServiceServerUnitTest {
emp.setId(id); emp.setId(id);
emp.setName("Eric Simmons"); emp.setName("Eric Simmons");
emp.setSalary(10000.00d); emp.setSalary(10000.00d);
//employeeDao.create(emp);
mockServer.expect(MockRestRequestMatchers.requestTo(new URI(EmployeeService.EMP_URL_PREFIX String fullUri = new StringBuilder().append(EMP_URL_PREFIX).append(URL_SEP)
+ EmployeeService.URL_SEP + id))) .append(id).toString();
.andExpect(MockRestRequestMatchers.method(HttpMethod.GET))
.andRespond(MockRestResponseCreators.withStatus(HttpStatus.OK) mockServer.expect(ExpectedCount.once(), requestTo(new URI(fullUri)))
.contentType(MediaType.APPLICATION_JSON) .andExpect(method(HttpMethod.GET))
.body(mapper.writeValueAsString(emp))); .andRespond(withStatus(HttpStatus.OK)
.contentType(MediaType.APPLICATION_JSON)
.body(mapper.writeValueAsString(emp)));
EmployeeDto employeeDto = empService.getEmployee(id); EmployeeDto employeeDto = empService.getEmployee(id);
logger.info("Employee received as: {}", employeeDto); logger.info("Employee received as: {}", employeeDto);
mockServer.verify();
Assert.assertEquals(emp.getName(), employeeDto.getName()); Assert.assertEquals(emp.getName(), employeeDto.getName());
Assert.assertEquals(emp.getId(), employeeDto.getId()); Assert.assertEquals(emp.getId(), employeeDto.getId());
} }

View File

@ -17,14 +17,13 @@ public class EmployeeServiceUnitTest {
private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceUnitTest.class); private static final Logger logger = LoggerFactory.getLogger(EmployeeServiceUnitTest.class);
@Mock @Mock
RestTemplate restTemplate; private RestTemplate restTemplate;
@Spy
@InjectMocks @InjectMocks
EmployeeService empService = new EmployeeService(); private EmployeeService empService = new EmployeeService();
@Before @Before
public void initMocks() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
} }