0

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)
Rahul
  • 95
  • 1
  • 3
  • 14
  • What's the exception? Add it to the question. – M. Prokhorov Mar 23 '18 at 17:16
  • The error which is throws is below, – Rahul Mar 23 '18 at 19:47
  • So, the exception at least suggests that you might have a problem with implementation of `equals` in `com.Id` class. Other than that, I find the code in your test extremely hard to follow. For example, why do you need two instances of `CheckImpl`? – M. Prokhorov Mar 25 '18 at 10:06
  • Basically in my method MainLogic(List) it is calling newId() method. I want to mock using jMock newId() when writing test code for MainLogic(List) method. I am unable to mock newId() method inside mainLogic() method hence the equals error in com.Id is coming up. Id class is pojo which is setting the string which is retrieved from newId(). – Rahul Mar 25 '18 at 10:32
  • Except it's not calling that. Most likely precisely because you have two instances of the same class. Tear that down and start with first and simplest thing that works, then build up from there. – M. Prokhorov Mar 25 '18 at 10:34
  • yes, i tried that also i initally. Only check object was there but then below code is giving error. Error is "check" in not mocked object. oneOf(check).newId(); will(returnValue(empId)); ** How should i create mock for method newId() so it returnsValue empId .. – Rahul Mar 25 '18 at 11:34
  • And it was completely justified in throwing that exception, because `check` is indeed not a mocked object. It should be. Everything you have mock-expectations for should be a mock object. – M. Prokhorov Mar 25 '18 at 12:30
  • yes, so how to mock the method. – Rahul Mar 25 '18 at 16:39
  • Same way you're already doing, but using proper instances that you'll actually use in tests. – M. Prokhorov Mar 26 '18 at 12:22

0 Answers0