easymock unexpected behaviour
I'm not sure what I'm doing wrong here. I had this error in my code, so I wrote a simple example to try to identify where the error lies.
I have a ClassA, that depends on two services ServiceA and ServiceB, I'm trying to test ClassA, and mocking ServiceA and ServiceB.
/**
* Last edited by: $Author: cg $
* on: $Date: 17 Jun 2011 11:36:25 $
* Filename: $RCSfile: ClassA.java,v $
* Revision: $Revision: $
*/
package easy;
import java.util.HashMap;
/**
*
* @version $Revision: $, $Date: 17 Jun 2011 11:36:25 $
*/
public class ClassA {
private ServiceA serviceA;
private ServiceB serviceB;
public ClassA(ServiceA a, ServiceB b) {
this.serviceA = a;
this.serviceB = b;
}
public String process(String p) {
HashMap<String,String> a = serviceA.getServiceA(p);
String ret = serviceB.getServiceB(a);
return ret;
}
}
interface ServiceA{
HashMap<String,String>getServiceA(String s);
}
interface ServiceB{
String getServiceB(HashMap<String,String> p);
}
My testing is the following:
/**
* Last edited by: $Author: cg $
* on: $Date: 17 Jun 2011 11:43:05 $
* Filename: $RCSfile: ClassATest.java,v $
* Revision: $Revision: $
*/
package easy;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import java.util.HashMap;
import junit.framework.Assert;
import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Test;
/**
*
* @version $Revision: $, $Date: 17 Jun 2011 11:43:05 $
*/
public class ClassATest {
@Test
public void testProcess() {
IMocksControl mockery = EasyMock.createControl();
mockery.resetToStrict();
mockery.checkOrder(true);
ServiceA servic开发者_运维问答eA = EasyMock.createMock("ServiceA",ServiceA.class);
ServiceB serviceB = EasyMock.createMock("ServiceB",ServiceB.class);
ClassA a = new ClassA(serviceA, serviceB);
String myParam = "My Test";
HashMap<String,String> retFromServiceA = new HashMap<String,String>();
retFromServiceA.put("my", "name");
expect(serviceA.getServiceA(eq(myParam))).andReturn(retFromServiceA);
expect(serviceB.getServiceB(retFromServiceA)).andReturn(myParam);
mockery.replay();
String actual = a.process(myParam);
mockery.verify();
Assert.assertEquals(myParam, actual);
}
}
the result is failing because the actual return is null.
I tried to debug into the code, and I realize that although in my expectations I say I expect ServiceA.getServiceA to return retFromServiceA, it's not. it's returning null.
Any ideas?
ServiceA and ServiceB were created without using the mockery control, so you should use:
EasyMock.replay(serviceA, serviceB);
prior to executing the process method
精彩评论