I am facing below error while running junit for controller. I have already set content-type as Json, still error is same. Any suggestion what could be the issue ?
Error is
java.lang.AssertionError: expectation "expectNext({response=employee saved Successfully})" failed (expected: onNext({response=employee saved Successfully}); actual: onError(org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'application/octet-stream' not supported for bodyType=java.util.Map<java.lang.String, java.lang.String>))
Controller is
@Slf4j
@Controller
@RequiredArgsConstructor
public class EmployeeController {
private final EmployeeService employeeService;
@PostMapping(path ="/employees",produces = APPLICATION_JSON_VALUE)
public @ResponseBody Mono<Map<String, String>> saveEmployees(@RequestBody List<EmployeeDto> employeeDtos) {
log.info("Received request to save employees [{}]", employeeDtos);
return employeeService.saveEmployee(employeeDtos);
}
}
Service class as below:
@Service
@Slf4j
@RequiredArgsConstructor
public class EmployeeService {
private final WebClient webClient;
public Mono<Map<String, String>> saveEmployees(List<EmployeeDto> employeeDtos) {
return webClient
.post()
.uri("/create-employees")
.contentType(APPLICATION_JSON)
.bodyValue(employeeDtos)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<Map<String, String>>() {
})
.doOnError(e -> log.error("Failed to save employees {}: {}", employeeDtos, e));
Junit is as below:
@Slf4j
@SpringBootTest
class EmployeeServiceTest {
private static final WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
@Autowired
private ObjectMapper objectMapper;
@Autowired
private EmployeeService employeeService;
@Test
void shouldMakeAPostApiCallToSaveEmployee() throws JsonProcessingException {
var actualemployeeDtos = "....";
var randomEmployeeDto = ...;
wireMockServer.stubFor(post("/create-employees")
.withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON_VALUE))
.withHeader(ACCEPT, containing(APPLICATION_JSON_VALUE))
.withRequestBody(equalToJson(actualemployeeDtos))
.willReturn(aResponse()
.withStatus(OK.value())
.withBody("{\"response\": \"employee saved Successfully\"}")));
StepVerifier
.create(employeeService.saveEmployee(List.of(randomEmployeeDto)))
.expectNext(singletonMap("response", "employee saved Successfully"))
.verifyComplete();
}
}