Though it's not clear why at this point, it seems like Mockery is checking a different instance of mo compared to the one you're defining the expectations to. Try inserting context.mock(Mo.class)
in the same local scope as the test case (or in an @Before method) and see if that fixes things.
jMock "unexpected invocation" for a method that I have a oneOf(instance).method() expected
Вопрос
I can't seem to figure out what I'm doing wrong here:
This is the method I'm testing:
public List<Mo> filterDuplicatesByName(List<Mo> dbMos) {
List<String> names = Lists.newArrayList();
for(Mo mo : dbMos) {
try {
String name = mo.getName();
if(names.contains(name)) {
dbMos.remove(mo);
} else {
names.add(name);
}
} catch (DataLayerException ex) {
dbMos.remove(mo);
}
}
return dbMos;
}
And this is my test class:
package com.rondavu.wt.service.recommendations;
import com.google.common.collect.Lists;
import com.rondavu.data.api.Mo;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
public class RecommendationsUtilsTest {
Mockery context = new Mockery();
RecommendationsUtils recommendationsUtils = new RecommendationsUtils();
final Mo mo = context.mock(Mo.class);
@Test
public void testFilterDuplicatesByName_oneMo() throws DataLayerException {
List<Mo> input = Lists.newArrayList(mo);
List<Mo> expected = Lists.newArrayList(mo);
context.checking(new Expectations() {{
oneOf (mo).getName(); will(returnValue("Mo 1"));
}});
List<Mo> actual = recommendationsUtils.filterDuplicatesByName(input);
context.assertIsSatisfied();
assertEquals(expected, actual);
}
}
When I run the test I get this output:
unexpected invocation: mo.getName()
no expectations specified: did you...
- forget to start an expectation with a cardinality clause?
- call a mocked method to specify the parameter of an expectation?
what happened before this: nothing!
[stack trace]
I'm pretty new to jMock, and Java is not my strongest language in general but I thought that my oneOf (mo).getName()
would make it expect that invocation. What am I doing wrong here?
Решение
Не связан с StackOverflow