简体   繁体   中英

Mockito: verify that a method was not called with specific parameter type

I wanted to test with Mockito that a method was not called with a specific parameter type with this simplified test:

@Test
public void testEm() {
    EntityManager emMock = Mockito.mock(EntityManager.class);
    emMock.persist("test");

    Mockito.verify(emMock, Mockito.never()).persist(Matchers.any(Integer.class));

}

Surprisingly this test failed with the following output:

org.mockito.exceptions.verification.NeverWantedButInvoked: 
entityManager.persist(<any>);
Never wanted here:
-> at com.sg.EmTest.testEm(EmTest.java:21)
But invoked here:
-> at com.sg.EmTest.testEm(EmTest.java:19)

I expected this test to fail only when the persist method is called with an Integer parameter, but it fails with String as well.

Why doesn't it work and how could I test it?

Thank You.

You can try to use not matcher http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/Matchers.html#not(org.hamcrest.Matcher)

Matchers.not(Matchers.any(Integer.class))

The persist() method takes an argument as Object and when you select any(Integer.class) will be converted to any object, so it will fail because you call it using String .

The best method here to specify the argument using eq() , like this

Mockito.verify(emMock, Mockito.never()).persist(Matchers.eq(Integer.valueOf(1)));

Or use ArgumentCaptor to capture the arguments and assert them

@Test
public void testEm() {
    // Arrange
    EntityManager emMock = Mockito.mock(EntityManager.class);
    ArgumentCaptor<Object> captor =  ArgumentCaptor.forClass(Object.class);  

    // Act
    emMock.persist("test");

    // Assert
    verify(emMock).persit(captor.catpure());
    Object val = captor.getValue();
    Assert....
}

Oracle doc#persist()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM