【发布时间】:2013-05-05 04:24:11
【问题描述】:
我正在尝试测试一个函数,它是 struts 应用程序的业务逻辑层的一部分。我遇到了问题,因为代码依赖于组织范围内使用的 jar 中的外部函数。
public CustomObject getCustomObject(String id){
CustomObject customObject = new CustomObject();
QueryObject sql = createSqlStatement(id); // EXTERNAL jar
Result result = execute(sql); // EXTERNAL jar
ArrayList list = result.getResulList(); // EXTERNAL jar
// Logic to use the list object to fill the customObject
// I can see an error here, that could have been
// caught in unit test
return customObject ;
}
现在的问题是测试填充对象的逻辑。
我写的Junit4测试是:
@Test
public void testCustomObject() {
CustomObject customObjectwActual = new CustomObject();
CustomObject customObjectExpected = new CustomObject();
// set properties of customObjectExpected here
customObjectwActual = getCustomObject(id); // Exception here
assertEquals(customObjectExpected , customObjectwActual );
}
根据开发人员的解释,抛出异常是因为“在启动 struts 应用程序时加载了外部 jar 类”。我是 Java 和 struts 的新手。我的方法错了吗?有没有办法以某种方式在setUpBeforeClass() 中“加载”这些外部 jar 类?
如果有什么不清楚的地方请告诉我。
编辑 2:
抱歉,我的问题不清楚。我的类路径中有这些外部 jar。它编译得很好,它实际上加载了外部 jar 的类。 SQL 查询存储在一个 xml 文件中。这些外部 jar 也有自己的带有 SQL 语句的 xml 文件。无法加载这两个 xml 文件之一。
此外,即使它们正确加载,我实际上也不想调用数据库。有什么方法可以模拟这些调用吗?
【问题讨论】:
-
你需要在你的类库中有这些 jar 并导入你的代码所需的类
-
“但是我他们无法加载自己的依赖项”是什么意思?
标签: java unit-testing junit mocking