Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit to test if the method call is made

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.

like image 608
JaveDeveloper Avatar asked Aug 30 '25 16:08

JaveDeveloper


1 Answers

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);
like image 149
vrudkovsk Avatar answered Sep 02 '25 05:09

vrudkovsk