lawlorslaw
06/26/2023, 1:44 AM.doOnError *{}*
doesn't get triggered in a Unit Test. I'm using the Mono
from the projectreactor
project. It basically functions like a reactive stream. What am i missing?lawlorslaw
06/26/2023, 8:32 AMStepVerifier
and now I get into the doOnError{}
block. The longer explanation is below :
To test for exceptions in a Mono from Reactor Core, you can use the StepVerifier
class to verify the behavior of the Mono. Here’s an example of how you can test for exceptions:
import org.junit.jupiter.api.Test
import reactor.core.publisher.Mono
import reactor.test.StepVerifier
class MonoExceptionTest {
@Test
fun testMonoException() {
val mono: Mono<String> = Mono.error(RuntimeException("Test exception"))
StepVerifier.create(mono)
.expectError(RuntimeException::class.java)
.verify()
}
}
In this example, the Mono.error
method is used to create a Mono that emits an exception when subscribed to. The StepVerifier
is then used to verify the behavior of the Mono. The expectError
method is called to specify that an error is expected to be emitted by the Mono, and the expected exception type (RuntimeException
in this case) is provided as a parameter.
Finally, the verify
method is called to start the verification process. If the Mono emits the expected error, the test will pass; otherwise, it will fail.
You can customize the expected exception type by changing the parameter of the expectError
method to the specific exception class you want to test.
Note that using verify()
will wait for the Mono to complete or emit an error. If you want to assert more details about the exception, you can chain additional methods like assertErrorMessage
or assertErrorMatches
after the expectError
method to further refine your assertions.