JUnit Easymock Unexpected method call

Posted by Lancelot on Stack Overflow See other posts from Stack Overflow or by Lancelot
Published on 2010-01-21T23:30:08Z Indexed on 2010/05/31 1:22 UTC
Read the original article Hit count: 1489

Filed under:
|
|
|
|

Hi,

I'm trying to setup a test in JUnit w/ EasyMock and I'm running into a small issue that I can't seem to wrap my head around. I was hoping someone here could help.

Here is a simplified version of the method I'm trying to test:

public void myMethod() {
    //Some code executing here
    Obj myObj = this.service.getObj(param);
    if (myObj.getExtId() != null) {
      OtherObj otherObj = new OtherObj();
      otherObj.setId(myObj.getExtId());
      this.dao.insert(otherObj);
    }
    //Some code executing there
}

Ok so using EasyMock I've mocked the service.getObj(myObj) call and that works fine.

My problem comes when JUnit hits the dao.insert(otherObj) call. EasyMock throws a "Unexpected Method Call" on it.

I wouldn't mind mocking that dao in my test and using expectLastCall().once(); on it, but that assumes that I have a handle on the "otherObj" that's passed as a parameter at insert time... Which of course I don't since it's conditionally created within the context of the method being tested.

Anyone has ever had to deal with that and somehow solved it?

Thanks.

© Stack Overflow or respective owner

Related posts about junit

Related posts about method