I have written a Spring Function and I am able to run junit to validate function. Howerver, if I am try to run junit to test same function as Http endpoint, below error is thrown.
java.lang.AssertionError: Status expected:<200 OK> but was:<404 NOT_FOUND>
Function Bean looks as below:
@Slf4j
@Configuration
@RequiredArgsConstructor
public class RetryFunction {
private final RetryExecutor retryExecutor;
@Bean
public Function<Message<Request>, Message<Response>> executeRetryJob() {
return request -> {
log.info("Received [request : {}]",request);
retryExecutor.executeJob(request.getPayload());
var response = Response.builder()
.message(request.getPayload().getName().concat("submitted successfully."))
.build();
return MessageBuilder.withPayload(response).build();
};
}
}
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Request {
private String name;
}
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Response {
private String message;
}
Junit code which is failing looks like below:
@WebFluxTest
@ContextConfiguration(classes = RetryFunction.class)
class JobExecutionFunctionTest {
@MockBean
private RetryExecutor retryExecutor;
@Autowired
private WebTestClient webTestClient;
@Test
void shouldReturnSuccessResponseWithHttpStatus200() {
var randomRequest = Request.builder().name(randomAlphanumericString(10)).build();
var requestMessage = MessageBuilder.withPayload(randomRequest).build();
var randomResponse = Response.builder()
.response(randomRequest.getName().concat("submitted successfully."))
.build();
var expectedResponseMessage = MessageBuilder.withPayload(randomResponse).build();
doNothing().when(retryExecutor).executeJob(randomRequest);
webTestClient.post()
.uri("/executeRetryJob")
.body(Flux.just(requestMessage),Message.class)
.exchange()
.expectStatus().isOk()
.expectBody(Message.class).isEqualTo(expectedResponseMessage);;
}
}
On running above junit, below error is thrown
java.lang.AssertionError: Status expected:<200 OK> but was:<404 NOT_FOUND>
> POST /executeRetryJob
> WebTestClient-Request-Id: [1]
> Content-Type: [application/json]
[{"payload":{"job_name":"os2zazNuIm"},"headers":{"id":"69f63243-0c6f-c911-4b77-ee39cadf3f42","timestamp":1609417585159}}]
< 404 NOT_FOUND Not Found
< Content-Type: [application/json]
< Content-Length: [130]
{"timestamp":"2020-12-31T12:26:25.461+00:00","path":"/executeRetryJob","status":404,"error":"Not Found","requestId":"31d47bd8"}
Any suggestion why this error is coming?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…