Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyMock Unexpected method call expected: 1, actual: 2 java.lang.AssertionError:

Tags:

java

easymock

Trying to test a method which takes a list of objects and returns a sorted list of objects. Sorting happens on the basis of putting the first element on the list with empty string value. The test fails with the following error:

java.lang.AssertionError: 
Unexpected method call LoggerConfig.getName():
LoggerConfig.getName(): expected: 1, actual: 2

The issue here is Expecting an Explicit Number of Calls. Here it seems the method is called too often, which throws an exception that the method has been called too many times. The failure occurs immediately at the first method call exceeding the limit(taken from EasyMock guide). Question is how to fix it in this context? Where am I doing it wrong?

EasyMock code:

public class SorterTest {
private Sorter tested;
LoggerConfig item1;
LoggerConfig item2;
LoggerConfig item3;
List<LoggerConfig> sortedList;

@Before
public void setUp() {
    tested = new Sorter();
}

private List<LoggerConfig> makeUnsortedList() {
    item1 = EasyMock.mock(LoggerConfig.class);
    item2 = EasyMock.mock(LoggerConfig.class);
    item3 = EasyMock.mock(LoggerConfig.class);

    EasyMock.expect(item1.getName()).andReturn("com.core");
    EasyMock.expect(item2.getName()).andReturn("");
    EasyMock.expect(item3.getName()).andReturn("com.core.FOO");

    List<LoggerConfig> unsortedList = new ArrayList<>();
    unsortedList.add(item1);
    unsortedList.add(item2);
    unsortedList.add(item3);


    return unsortedList;
}

@Test
public void testSort() {

    List<LoggerConfig> unsortedList = makeUnsortedList();
    EasyMock.replay(item1,item2,item3);

    List<LoggerConfig> sortedList = tested.sort(unsortedList);

    assertTrue(sortedList.get(0).getName().isEmpty());
    assertTrue(sortedList.get(1).equals("com.core") || sortedList.get(1).equals("com.fwk.core.EBCTestClass"));
    assertTrue(sortedList.get(2).equals("com.core") || sortedList.get(2).equals("com.core.FOO"));

}

}

Testing the following method:

class Sorter {

  List<LoggerConfig> sort(List<LoggerConfig> unSortedList) {

    List<LoggerConfig> sortedList = new ArrayList<>(unSortedList);

    Collections.sort(sortedList, new Comparator<LoggerConfig>() {
        @Override
        public int compare(LoggerConfig o1, LoggerConfig o2) {
            return (o1.getName().compareTo(o2.getName()));
        }
    });

    return sortedList;
  }
}  
like image 833
PineCone Avatar asked May 11 '17 07:05

PineCone


1 Answers

It's calling getName() as part of the sort (possibly multiple times depending on the algorithm) and then again in your verification. Since you're not really interested in how many times it's called (as it's not part of your tested contract), remove the default restriction. To do that, replace andReturn with andStubReturn:

EasyMock.expect(item1.getName()).andStubReturn("com.core");
EasyMock.expect(item2.getName()).andStubReturn("");
EasyMock.expect(item3.getName()).andStubReturn("com.core.FOO");
like image 190
Yosef Weiner Avatar answered Nov 02 '22 04:11

Yosef Weiner