[英]How to test method that uses external package to interact with DB
I am trying to test a function, which is part of the business logic layer of a struts application. 我正在尝试测试一个功能,该功能是struts应用程序的业务逻辑层的一部分。 I am facing problem because the code is dependent on external functions in a organization wide used jar. 我遇到了问题,因为代码依赖于组织广泛使用的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 ;
}
Now the problem is testing the logic of filling up the object. 现在的问题是测试填充对象的逻辑。
The Junit4 test that I have written is: 我编写的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 );
}
The exception is thrown because "the external jar classes are loaded at the time of startup of struts application" as per the developers explanation. 抛出异常是因为按照开发人员的说明,“在struts应用程序启动时会加载外部jar类”。 I am new to Java and struts. 我是Java和strut的新手。 Is my approach wrong? 我的方法错了吗? Is there a way to "load" these external jar classes in the setUpBeforeClass()
in someway? 有办法以某种方式“加载”这些外部jar类到setUpBeforeClass()
中吗?
Please let me know if anything is unclear. 如果有任何不清楚的地方,请告诉我。
EDIT 2: Sorry, my question is unclear. 编辑2:对不起,我的问题尚不清楚。 I have these external jars in my classpath. 我的类路径中有这些外部jar。 It compiles fine, and it actually loads the external jar's classes. 它可以很好地编译,并且实际上可以加载外部jar的类。 The SQL queries are stored in an xml file. SQL查询存储在xml文件中。 These external jars have their own xml file with SQL statements as well. 这些外部jar也有自己的带有SQL语句的xml文件。 It is failing to load one of these two xml files. 无法加载这两个xml文件之一。
Further, even if they load correctly, I actually dont want to call the database. 此外,即使它们正确加载,我实际上也不想调用数据库。 Is there some way to mock these calls? 有什么方法可以模拟这些电话吗?
Is there a way to "load" these external jar classes in the setUpBeforeClass() in someway? 有办法以某种方式“加载”这些外部jar类到setUpBeforeClass()中吗?
You just need to include these jars in your class path. 您只需要在类路径中包含这些jar。 Classes are loaded at runtime automatically when you use them 使用类时,它们会在运行时自动加载
I am new to Java and struts. 我是Java和strut的新手。 Is my approach wrong? 我的方法错了吗?
There is nothing specific to Struts. 没有特定于Struts的东西。 Its plain core java that external classes you are using in your project must be included in your class path 您的项目路径中必须包含您项目中使用的外部类的纯核心Java
Is there some way to mock these calls? 有什么方法可以模拟这些电话吗?
Well if you mock these calls ,whats the use of writing the DB testcases which does test DB Interaction 好吧,如果您模拟这些调用,编写测试数据库交互的数据库测试用例有什么用?
I also suggest using IDE like eclipse as its very helpful for development like providing compilation issues upfront, formatting etc 我还建议使用像eclipse这样的IDE对开发非常有帮助,例如预先提供编译问题,格式化等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.