I've a transaction class with a validate method. There is a condition in the method that when satisfied will not be calling an alert notification method, which, on the other hand, will only be called when the check condition failed (if condition is false, call the alert and notify the user). I want to test with a JUnit, if that alert method get's called or not. Could someone advise how to proceed, as I'm new to JUnits.
Look at mocking libraries. In Mockito it will be like
Notifier mock = Mockito.mock(Notifier.class);
ClassUnderTest myClass = new ClassUnderTest(mock);
myClass.doSomething(-1);
Mockito.verify(mock).notification(Mockito.eq("Negative value passed!"));
myClass.doSomething(100);
Mockito.verifyNoMoreInteractions(mock);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With