I am trying to mock the method which is used in same class but it is giving me exception.
The expectation is that newId() mock return String id which is added to List idList and mocked to insert(idList) method.
Test class is :
public final JUnitRuleMockery mock= new ClassMockery();
private CheckImpl check;
private IdDao daoMock;
private CheckImpl checkImpl;
@Before
public void setUp()
{
check= new CheckImpl ();
daoMock = mock.mock(IdDao.class);
check.IdDao = daoMock;
checkImpl = mock.mock(CheckImpl.class);
}
@Test
public void mainLogic()
{
List<Id> id = // List is created here
List<Id> idList = // another list is created here.
mock.checking(new ExtendedExpectations()
{
{
oneOf(checkImpl).newId();
will(returnValue(empId));
oneOf(daoMock).insert(idList);
will(returnValue(new int[] { 1 }));
The oneOf checkImpl is never mocked in actual class. IdDao is passed as @Resource in CheckImpl class.
Here is CheckImpl class. This is not the complete class but it tell what i am looking for.
public class CheckImpl
{
@Resource
IdDao idDao;
public String newId()
{
return String.valueOf(id);
}
public List MainLogic(List){
String s = newId();
listInsert.add(s);
idDao.insert(listInsert);
}
The error is below,
java.lang.AssertionError: unexpected invocation:
idDao.insert(<[com.Id@24a35978]>)
expectations:
expected once, already invoked 1 time: idDao.find(<3>); returns
<[com.Id@5204062d, com.Id@4fcd19b3]>
expected once, never invoked: checkImpl.newId(); returns "123"
expected once, never invoked: idDao.insert(<[com.Id@24a35978]>); returns
[<1>]
parameter 0 did not match: <[com.Id@24a35978]>, because was
<[com.Id@24a35978]>
what happened before this:
idDao.find(<3>)
at org.jmock.api.ExpectationError.unexpected(ExpectationError.java:23)
at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:85)
at org.jmock.Mockery.dispatch(Mockery.java:244)
at org.jmock.Mockery.access$100(Mockery.java:29)
at org.jmock.Mockery$MockObject.invoke(Mockery.java:284)
at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
at org.jmock.internal.SingleThreadedPolicy$1.invoke(SingleThreadedPolicy.java:21)
at org.jmock.lib.legacy.ClassImposteriser$4.invoke(ClassImposteriser.java:136)