junit5-system-exit icon indicating copy to clipboard operation
junit5-system-exit copied to clipboard

Assertions after System.exit?

Open mihxil opened this issue 3 years ago • 4 comments

I was trying to make a test that also checks what's appearing on stdout, besides checking the exit code.

It's easy to just set System.setOut. I started out with just setting a custom securitymanager, but it seemed that using an extension to remove al that stuff would be a nice improvement.

Extending the test with @ExtendWith(SystemExitExtension.class) avoids indeed that the system.exit exits the complete testing using the securitymanager, but still, it silently aborts the currently running test. Which I can work around like so:

 SystemExitPreventedException throwable = (SystemExitPreventedException) catchThrowable(() -> {
              ... my code ...
            });
            assertThat(throwable.getStatusCode()).isEqualTo(0);

            assertThat(outContent.toString()).containsAnyOf("a", "B");

It works, but it is hardly convenient. It would help if the SystemExitPreventedException contains a useful message:

 assertThatThrownBy(() -> {
           ... my code ...
            }).isInstanceOf(SystemExitPreventedException.class).hasMessage("status 0");
            assertThat(outContent.toString()).containsAnyOf("a", "B");

Which is already a bit more readable.

I think I might also appreciate if it would be possible to not handle SystemExitPreventedException in handleTestExecutionException at all , and for example provide a custom utility to catch it nicely, which would leave me with this:

assertExitCode(() -> { my code }).isEqualTo(0)

or so .

If you happen to forget to wrap the code in something like that, the test will fail, but at least it won't silently skip assertions after it.

May be a variant of '@FailOnSystemExit', just '@ThrowOnSystemExit' or so would do, which then doesn't convert SystemExitPreventedException to fails or not, but just lets it go.

It may also be that I just failed to see a way to accomplish what I want.

mihxil avatar Nov 27 '21 18:11 mihxil

Hey @mihxil thanks for the report. I hadn't considered this use case of examining the stdout/stderr. Let me think about this for a bit before responding. FWIW SystemExitPreventedException does have a getStatusCode() method on it that you can use to manually check the code.

tginsberg avatar Dec 02 '21 21:12 tginsberg

I ended up working around it like so:

https://github.com/mihxil/json/tree/main/mihxil-json-core/src/test/java/org/meeuw/main

and actually just use the securitymanager of junit5-system-exit.

May be it's of some use.

mihxil avatar Dec 03 '21 07:12 mihxil

BTW, I noticed the getStatusCode, but having it in the message would have made it easier to check its value with an assertj's hasMessage check.

mihxil avatar Dec 03 '21 18:12 mihxil

BTW, I noticed the getStatusCode, but having it in the message would have made it easier to check its value with an assertj's hasMessage check.

I support this idea!

mjechow avatar Jul 08 '22 17:07 mjechow