lundi 26 octobre 2020

public void method throws Exception if - JUNIT test: Exception should not be thrown

I have a certain public void method which throws an Exception if a condition is fulfilled. In my case the method looks like this:

public void toBeTestedMethod(Testobject testObject) throws CertainException {
if (testObject.getStatus().getAllowsEdit()){
throw ...}
}

getStatus() is a method which returns a certain Status and getAllowsEdit() is a method which returns a boolean value and nullable = true. For the two methods there also exist set-methods.

I would now like to test the method toBeTestedMethod. The target is that the method does not throw an exception but gets executed successfully.

That means I would like to write a JUNIT-test which tests the following:

public void testToBeTestedMethod_success throws Exception{

// Enter test code here

}

In my opinion, I have to modify the condition in the if-statement in order to get the expected result, correct?

Note: I did not write the method and the other code. Nevertheless, my task is to test the code via JUNIT. I tried some code, but everytime I get the error that the Excpetion was thrown.

Even if you cannot solve this problem, I would be glad to get some hints where I should look for the problem why my test does not do what I want the test to do.

Aucun commentaire:

Enregistrer un commentaire