bhochhi
bhochhi

Reputation: 140

How to verify exception thrown using StepVerifier in project reactor

    def expectError() {

        StepVerifier.create(readDB())
                    .expectError(RuntimeException.class)
                    .verify();
    }

     private Mono<String> readDB() {
//        try {
            return Mono.just(externalService.get())
                    .onErrorResume(throwable -> Mono.error(throwable));

//        } catch (Exception e) {
//            return Mono.error(e);
//        }
    }

unable to make it work if externalService.get throws Exception instead of return Mono.error. Is is always recommended to transform to Mono/Flow using try catch or is there any better way to verify such thrown exception?

Upvotes: 3

Views: 14207

Answers (1)

Simon Basl&#233;
Simon Basl&#233;

Reputation: 28301

Most of the time, if the user-provided code that throws an exception is provided as a lambda, exceptions can be translated to onError. But here you're directly throwing in the main thread, so that cannot happen

Upvotes: 2

Related Questions