【发布时间】:2009-10-20 07:36:28
【问题描述】:
我有以下课程。它的构造函数中有连接到 SAP 的代码。我想模拟一个抽象方法(子类定义实现)。
public abstract class BapiExecutor {
...
public BapiExecutor(final SapConnectionInfo connectionInfo)
throws java.lang.Exception {
if (!validConnectorData()) {
throw new IllegalArgumentException(
"Does not have valid data to connect to SAP");
}
initializeState(connectionInfo);
}
public abstract Object execute() throws Exception ;
....
}
我要测试的单元是:
我想模拟对execute() 方法的调用。
private String invokeBapiToAddAssociation(Map associationMap,
SapConnectionInfo connectionInfo) {
EidCcBapiExecutor executor = null;
String bapiExecutionResult = null;
try {
executor = new EidCcBapiExecutor(connectionInfo, associationMap);
bapiExecutionResult = (String) executor.execute();
} catch (Exception e) {
e.printStackTrace();
throw new CcGenericException(
"Exception occurred while invoking the EID-CC Association BAPI executor!",
e);
}
return bapiExecutionResult;
}
任何支持模拟参数化构造函数的 Java 框架?
我只是想避免在构造函数中连接到 SAP。
【问题讨论】:
-
你对它说这样的话......“有这样的身体,我们看不到你是件好事”。 :-)
标签: java unit-testing mocking