I want to make sure that the Spring Boot WebFlux jUnit test passes through onErrorResume()

Asked 2 years ago, Updated 2 years ago, 114 views

Thank you for your continuous support.

I would like to confirm that the Spring Boot WebFlux jUnit test passes through onErrorResume().
When Exception occurs, the jUnit test stops and the path cannot be verified.
Please let me know if there are any necessary items such as settings.


when Runtime Exception occurs in repository.hello() I would like to confirm that the onResumeError is handling exceptions (logs are printed).

public class HealthCheckHandlerTest{

    private HealthCheckHandler;

    @Mock
    private repository repository;

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
        // Generating a Test Instance
        handler = new HealthCheckHandler(repository);        
    }    

    @ Test
    Error in public void testHandleHealthMonitoring anomaly system 1_DB(){
        // preliminary preparation
        when(repository.hello()) .thenThrow(new RuntimeException("DB connection error"));

        // Running Tests
        Mono<ServerResponse>response=handler.handleHealthMonitoring (request);
        response.subscribe(r->r.statusCode());
    }
}
  • Tested Class
@Component
public class HealthCheckHandler {

    /**
     * repository
     */
    private final repository repository;

    /**
     * constructor
     *
     * @param repository
     */
    public HealthCheckHandler(
            Repository repository) {
        this.repository=repository;
    }

  /**
     * handler
     *
     * @param request
     * @return
     */
    public finalMono<ServerResponse>handleHealthMonitoring (finalServerRequest request) {
        // Try calling postgre for now to see if there are any errors.
        Mono<Map<String,Object>>postgreOpe=repository.hello();
        // Hogehoge is omitted this time.
        return Mono.zip (hogehoge, postgreOpe).flatMap(r->{
            return ServerResponse
                    .ok();
        }).timeout(Duration.ofSeconds(DURATION_TIME)) .onErrorResume(t->{
            logger.error("healthchecked for errors",t);
            return ServerResponse
                    .status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .contentType(MediaType.APPLICATION_JSON)
                    .bodyValue("Error\n");
        });
    }
}

At the end of the test code, an error stack of the "DB connection error" mock appears stacked when calling response.subscribe.The test is also considered a failure.The timing is right, but I would like you to continue processing...

Put java plugin and other sets into the vscode
* Key Plug-ins
* Maven for Java * Java Test Runner * Language Support for Java(TM) by Red Hat and so on.

spring-boot junit

2022-09-30 11:23

1 Answers

when(repository.hello()) .thenThrow(new RuntimeException("DB connection error"));

when(repository.hello()) .thenReturn(Mono.error(new RuntimeException("DB connection error"));


2022-09-30 11:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.