gpt4 book ai didi

java - easymock 意外行为

转载 作者:行者123 更新时间:2023-11-30 09:48:09 25 4
gpt4 key购买 nike

我不确定我在这里做错了什么。我的代码中有这个错误,所以我写了一个简单的例子来尝试找出错误所在。

我有一个 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

有什么想法吗?

最佳答案

ServiceS 和 ServiceS 是在没有使用模拟控件的情况下创建的,因此您应该使用:

EasyMock.replay(serviceA, serviceB);

在执行流程方法之前

关于java - easymock 意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6384904/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com