mockito - negativeTest for applicationEventPublisher

Rakshan

Mitglied
I have a runner which produces an event when the value is updated/changed.


[CODE lang="java" title="runner"]public void run(){

if(val!= cachedValue){
applicationEventPublisher.publishEvent(new DataEvent(this, val);
//only if a new value comes, then the event is fired
}

}[/CODE]


To verify I have a testcase, where the functionality is called twice with same inputs(For eg: When called for the first time, the value is not equal to cached value, hence event is fired. But when called again, I expect the event to not get fired)

Mockito Junit test:

Java:
public class Testing{

public UnderTest underTest;

@mockbean
ApplicationEventPublisher applicationEventPublisher;
@Captor
private ArgumentCaptor<DataEvent> eventArgumentCaptor,eventArgumentCaptor2;
@Test
public test1(){

        // some sample inputs

        underTest.run() // first run, i expect the event to fire
        verify(applicationEventPublisher).publishEvent(eventArgumentCaptor.capture()); // i get the value as expected here


        //now i run again
        underTest.run();
        verify(applicationEventPublisher, never()).publishEvent(eventArgumentCaptor2.capture());
        //fails here, the result show that the event is fired
        assertEquals(0, eventArgumentCaptor2.getAllValues().size());
    }

}


I am not understanding what is going wrong after the second run here. I expect to see no events(which is the case while debugging the runner), but does not work.

Any suggestions or inputs here please? ALso excuse my language, still learning Deutsch
 

LimDul

Top Contributor
The problem ist, verfiy doesn't check if if was called since the last time you called verify.
So a check for never will fail, because it was called. You should check in both verify checks for times(1) do ensure the number of calls hasn't gone up.
 

Rakshan

Mitglied
Java:
{
    underTest.run();
    verify(applicationEventPublisher,times(1)).publishEvent(eventArgumentCaptor.capture());
    //called twice
    underTest.run();   
 //   verifyNoMoreInteractions(applicationEventPublisher);   
    verify(applicationEventPublisher, times(1)).publishEvent(eventArgumentCaptor2.capture());   
    assertEquals(0, eventArgumentCaptor2.getAllValues().size());
    
}

But the assert statement fails here. Expected = 0, Actual =1
 

LimDul

Top Contributor
Because you expect one Argument - from the first call. What check with line 7 and 8 is not the result of line 5. It is the result of line 2 and 5 combined. And then there is one call to publishEvent and that argument is what you get.
 

Rakshan

Mitglied
Inspite of using the separate ArgumentCaptor after the second underTest.run(); ? I expect it to capture that there has been no event ..
Anyways could you suggest some idea to overcome this?
 

LimDul

Top Contributor
You could try Mockito.reset after the first verify. That should sovle your problems I think (I have never used it, so no guarantee)
 

Ähnliche Java Themen

Neue Themen


Oben