Is there a way to make JUnit fail with any unchecked exception, even if caught

I use JUnit to write higher-level tests for legacy code that does not have unit tests.

Most of this code swallows many unchecked exceptions, such as NullPointerExceptions (for example, just printing a stack trace and returning zero). Therefore, a unit test can even go through a cascade of disasters at different points in the lower-level code.

Is there a way to skip a test on the first unchecked exception, even if they are swallowed?

The only alternative I can think of is to write a custom JUnit shell that redirects System.err and then parses the output for exceptions.

+6
java unit-testing junit swallowed-exceptions
source share
6 answers

If you run tests in your IDE debugger, you can configure the IDE to break when an exception is thrown.

+4
source share

In the absence of a concrete solution, my answer is pretty general:

Such code smells (for example, exceptions for unwinding) are best cleared step by step (class by class) when you encounter them when fixing an outdated system. Code quality tools (such as Findbugs, PMD, Checkstyle, or Sonar Quality Server) help you find these things.

The way to "catch" swallowed exceptions automatically is to use the AspectJ compiler. You can declare aspects to generate compile-time errors in your IDE if you violate certain code conventions. Alternatively, you can twist during the execution of the tested classes and let AspectJ reconstruct such exceptions so that they can be written by the JUnit runner.

+4
source share

I believe the exception is the standard class in the SDK libraries.

If you extracted it, changed it and placed it somewhere in your class path before the SDK, I think it should replace the one in the SDK (if you cannot return your "new" exception to the SDK bank)

In any case, your new exception may set a static value that can be read using the test environment.

It may not be the most elegant solution, but it does not require any "magic"

+1
source share

I would try to use AOP to fail. Something like this should work (note, I have not tested this, and you obviously need to have AspectJ Setup to use AOP annotations)

public class ClassUnderTest { private static Boolean exceptionThrown; @Before public void resetExceptionFlag() { ClassUnderTest.exceptionThrown = false; } @Test public void myTestMethod() { //.... // My Test Exception Code //.... assertFalse(ClassUnderTest.exceptionThrown); } @Aspect static class TestAspects { @Pointcut("handler(Exception)") public void intereceptAllExceptions(){} //This is Fully Qualified because of the conflict with the junit Before annotation above @org.aspectj.lang.annotation.Before("intereceptAllExceptions()") public void flagExceptionThrown() { ClassUnderTest.exceptionThrown = true; } 

}}

+1
source share

You could make fun of the Exceptions you want to catch, with a mocking structure that modifies the bytecode or AOP structure. I assume that you can change the constructor to throw a more fatal exception or set some flag in your test code.

0
source share

Perhaps you can insert the code you are trying to test. Most likely, you will have to use some of the "test patterns", such as powermock or jmockit, to manipulate the class manipulator in the JVM. But a sample of the tested class will help determine the necessary approach.

0
source share

All Articles