I would like to suggest as a feature request, a convenient JUnit5 extensions that'd allow capturing testing exceptions on the code.
This would be especially useful when annotating the test class level, on application runs that happen before the @Test
(through ApplicationRunner
/CommandLineRunner
and the like). It is a good complement to @SpringBootTest.args
and OutputCaptureExtension
Comment From: wilkinsona
Thanks for the suggestion, but I'm not sure how this would work. Generally speaking, there isn't a one-to-one mapping between application context creation and test class creation due to caching of the application context. When application context creation fails, every test in the class that required the context will fail. What purpose would be served by an extension that can capture the exception and how would you expect it to be called?
Comment From: nightswimmings
Hi Andy, I am not sure of the shape, to be honest, because your point makes a lot of sense but perhaps something close to or allowing to test like:
@SpringBootTest(args="--exception-inducing-param")
@ExtendWith(ExceptionCaptureExtension.class)
public class ApplicationRunTest {
@Test
public void whenExceptionInducingParamExceptionIsThrown(ExceptionCapture exceptions){
assertThat(exceptionCapture).isEqualTo(IllegalArgumentException.class)....
}
}
Comment From: wilkinsona
Thanks for the clarification. Unfortunately, as I tried to explain above, that won't work as the @Test
methods won't be called if refresh of the application context fails. I don't think there's anything we can do here so I'm going to close this one. Thanks anyway for the suggestion.
If you're interested in testing failure scenarios, I think you'd be better served by using ApplicationContextRunner
rather than @SpringBootTest
. In addition to the documentation, there are numerous usages of it in Spring Boot's own tests that you may want to look at.
Comment From: nightswimmings
Hi Andy, I understood your point, I was trying to explain it from the side of the need, so perhaps some of you come up with a clever (or feasible) approach, idk, like there could be some capturing logic by the extension that it does not make the context fail or something, but I understand your rational