Testing Model class equals() method using Mockito

1,836 views
Skip to first unread message

Ashok Shanmugam

unread,
Sep 11, 2015, 4:50:11 AM9/11/15
to mockito
How to test Model class equals() method. Getting following issue.

  <<< FAILURE! - in EmployeeTest
    testEquals(EmployeeTest)  Time elapsed: <<< ERROR!
    org.mockito.exceptions.misusing.UnfinishedVerificationException: 
    Missing method call for verify(mock) here:
    -> at EmployeeTest.testEquals(EmployeeTest.java:20)

    Example of correct verification:
        verify(mock).doSomething()

    Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.
    Those methods *cannot* be stubbed/verified.
    Mocking methods declared on non-public parent classes is not supported.

Sample test method. Using Mockito.verify() for testing equals method.  



 private static Employee mockedEmployee;
    private static Employee employee;

    @BeforeClass
    public static void setup() {
        mockedEmployee = mock(Employee.class);
        employee = new Employee("first", "last");

       when(mockedEmployee .getFirstName()).thenReturn(Employee.getFirstName());

    }        

    @Test
    public void testEquals() {
        Mockito.verify(mockedEmployee).equals(employee);
    }

KARR, DAVID

unread,
Sep 11, 2015, 11:50:40 PM9/11/15
to moc...@googlegroups.com

I see a few issues here.  First of all, you’re apparently wondering why Mockito is complaining about your “verify().equals()” call.  The error message that that you provided here should have explained that: “you verify either of: final/private/equals()/hashCode() methods”.  This doesn’t solve your problem, but outside of refactoring your “equals()” method or simply removing the test, there’s no alternative I’m aware of.

 

I hope you were intending to write more in the “testEquals” method than that verification.  It doesn’t make sense by itself, even if it worked.

 

It’s not a good idea to initialize your mocks in a “@BeforeClass” method.  Initialize them as normal instance variables, or perhaps in a “@Before” method.  Each test method runs with a fresh instance of the test class, it’s best to not do any “@BeforeClass” work unless it’s absolutely necessary.

 

It’s really odd to get the data you’re using to verify against from another instance of the same class under test.  Not a good idea.

 

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/d8f77f8b-5114-4d9a-afc9-a19ecdc05ea8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brice Dutheil

unread,
Sep 12, 2015, 8:09:44 AM9/12/15
to moc...@googlegroups.com

Hi Ashok,

David is right, there’s some issues in the code snippet here. As he rightly pointed out the cause is in the message

... final/private/equals()/hashCode() methods.
    Those methods *cannot* be stubbed/verified.

equals method stubbing is not supported, you can see more in the FAQ wiki : https://github.com/mockito/mockito/wiki/FAQ

While all point of David are correct, I’d like to add that this code is mocking value type, one should not mock value object, instead create factory or builders to create concrete object to test actual behavior. Mocks are better suited for test interactions between different objects that collaborate together.

Here’s a page here that gives more tips to write good tests : https://github.com/mockito/mockito/wiki/How-to-write-good-tests

Hope that helps.
— Brice

Reply all
Reply to author
Forward
0 new messages