-2

How do I test the Model class' equals() method. I keep experiencing the 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.  
Craig
  • 2,286
  • 3
  • 24
  • 37
spider
  • 29
  • 2
  • 6

3 Answers3

2

If you want to test your equals method, I recommend you to use a library like Equalsverifier and not using a mock.

Steve C
  • 1,034
  • 10
  • 12
  • Thanks for your reply. I do not want to use another library for equals. Is there a way to test equals() using Mockito? – spider Sep 11 '15 at 06:01
  • 1
    You should use JUnit and not mockito. You don't need a mock. Employee employee1 = new Employee("first", "last"); Employee employee2 = new Employee("first", "last"); AssertEquals(employee1, employee2). – Steve C Sep 11 '15 at 08:53
0

Mockito.verify() gives above issue.

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);
}
Craig
  • 2,286
  • 3
  • 24
  • 37
spider
  • 29
  • 2
  • 6
0

As it says in the error message

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

so you will have to create real test object (not a mock) and then verify the equals() by comparing it with a real expected object.

Alternatively, you could instead verify other methods on the mock (i.e. verify that the last name was correctly set verify(employee).setLastName("ExpectedLastName")).

Craig
  • 2,286
  • 3
  • 24
  • 37