How to write a test case method having System.exit() using Junit 5?

311 views Asked by At

Scenario: Below negative scenario to be tested during Integration test. currently the test case getting failed due exit and not reaching to the test method.

example :

private void method1(int a){
try{
  if(a == 0){
    throw exception();
  }else{
     ---
  }
 }catch(exceptionclass e){
    System.exit(1);
 }
}
1

There are 1 answers

3
Ken Chan On

Sound like a bad smell to me that calling a method on a object can cause JVM to exist. Normally it should be done in the main method.

So I would refactor your codes such that your testing object will throw a kind of Exception to indicate that some kind of fatal error happens such that the main method can catch it and terminate the JVM.

Then you can simply test that if it will throw this Exception from your test case.