Bajji
Bajji

Reputation: 2393

Approach for enforcing a statement is present inside if condition through tests

I have a java project and I am looking for testing frameworks which allows me to enforce a constraint -

A specific method call on one object can only be called only after invoking another method on another object:

For example

if (validator.someMandatoryCheck()) {
    myObject.performOperation();
}

I want to ensure that in my codebase, everywhere myObject.performOperation() is called, validator.someMandatoryCheck() has to be called first.

Is there a testing framework that would allow me to achieve this ?

Upvotes: 0

Views: 18

Answers (1)

Hemant Singh
Hemant Singh

Reputation: 1608

To test your code snippet, you can use Mockito framework.

Mockito.when(validator.someMandatoryCheck()).thenReturns(true);
Mockito.verify(myObject).performOperation();

But myObject should be a mock object. For more reference on verify method: http://www.baeldung.com/mockito-verify

Upvotes: 0

Related Questions