Is there a way to check if the Catch part of the try / catch command is called in a test method when using JUnit?

For example, if I had the following class that I want to test:

public class SomeClass{
    public void someMethod() {
        try {
            //Some code, where comething could go wrong
        } catch (Exception err) {
            //Handling it amounts to logging the problem and trying to continue
        }
    }
 }

      

If I then test this method with JUnit, then if something goes wrong in the try clause and the catch code runs, then the test passes.

I want to make the test fail if the catch statement is executed.

I thought of several ways that I could try and write tests to get equivalent functionality, but there are reasons for each one that I don't want to approach it. It seems that trying to fail the test if any catch clause is reached is the cleanest way to do it, if possible.

Notes:

I know I can check some functions of the code and check if they are a specific value / have been run multiple times with Mockito. However, I want a solution where if there were changes to the test method that radically changed how it works (but not what is essentially being done), then I don't have to rewrite the test.

Unfortunately, just to make this work harder, I cannot make any changes to the source code. This position is beyond my control, so I have to work within these limits.


EDIT:

SomeClass is the class that I want to test. This is not a real JUnit test. I edited my original question to try and clarify this.

+3


source to share


5 answers


I have faced such problems before. I ended up mocking the logging subsystem (not simple, but not overly complex) and listened to "interesting" logging calls and then flagged the failures when it happened.



+4


source


Remember that you are testing the behavior of this function. Your test shouldn't care (not test) that the exception is caught, just what behavior you want.



This means that if you want to check if something is registered (or not). Then you should check that something is being logged. In several of my applications, I've made a distinction between debug protocol and important logmessage. We send the latter through a class (in our case, we used the facade design template). which we can mock (and thus test the calls). For most applications, most developer registrations do not need to be verified, so in these cases you should ignore this.

+2


source


I am not aware of any structure that allows it to be asserted that exceptions were handled and handled in a method and were not propagated. From the description of the problem, I would see two approaches using BDD:

  • Asserting the line of code inside the catch block is not called - the logger would be a good option as @Thirler suggests
  • Asserting that no exception constructor is called

Both have problems:

  • Not very clean as you want, because it is tightly coupled with the actual executable code.
  • Any exception handled in your code will fail.

You can try using aspectj to handle the code and flag that an exception was thrown when SomeClass.someMethod was executed .

0


source


In general

-1- Check that some function f () throws an exception

try {
  f();
  // notify test failure here
} catch (...) {
  // success
};

      

-2- Checking that he is not

try {
   f();
} catch(...) {
   // notify test failure here
}

      

0


source


you can use assert

what you can ask:

A statement is a statement in the JavaTM programming language that allows you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.

I don’t understand the details as I cannot explain it better than his own docs.

-1


source







All Articles