2
我不知道我在做什麼錯在這裏。我在代碼中出現了這個錯誤,所以我編寫了一個簡單的例子來試圖找出錯誤所在。easymock意外的行爲
我有一個ClassA,這取決於兩個服務ServiceA和ServiceB,我試圖測試ClassA,並嘲笑ServiceA和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);
}
我的測試如下:
/**
* 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 serviceA = 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);
}
}
結果是失敗,因爲實際收益是零。
我試圖調試到代碼中,我認識到,雖然在我的期望我說我希望ServiceA.getServiceA返回retFromServiceA,事實並非如此。它返回null。
任何想法?