реализация:
Код: Выделить всё
public ResponseEntity createRecord(Object studentRecord) {
ResponseEntity res = null;
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_JSON);
header.setBasicAuth(username, password);
HttpEntity requestEntity = new HttpEntity(studentRecord, header);
try {
URI uri = UriComponentsBuilder.fromUriString("https://localhost:8080").path(/api/v1/createrecord).build().toURL().toURI();
res = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, Object.class);
} catch (Exception ex) {
e.printStackTrace();
}
return res;
}
Код: Выделить всё
@Test
public createRecordTest() {
when(restTemplate.exchange(
ArgumentMatchers.anyString(),
ArgumentMatchers.eq(HttpMethod.POST),
ArgumentMatchers.any(),
ArgumentMatchers.eq(Object.class)))
.thenReturn(new ResponseEntity(studentObject, HttpStatus.OK));
assertEquals(studentObject, studentService.createRecord(studentRecord).getBody());
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... ting-basic