简体   繁体   English

如何测试使用外部程序包与数据库交互的方法

[英]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.

相关问题 JUnit-如何对测试方法进行单元测试,该方法读取目录中的文件并使用外部库 - JUnit - How to unit test method that reads files in a directory and uses external libraries 如何测试内部创建和使用ServerSocket的方法 - How to test method that internally creates and uses a ServerSocket 如何测试(模拟)使用外部API的对象(Jama Software) - How to test(mock) object that uses external API (Jama Software) 如何测试使用其他方法的方法-Mockito,Java,Junit - How to test method which uses other method - Mockito, java, junit 如何对使用文件锁的java方法进行单元测试? - How to unit test my java method which uses file locks? 如何测试使用实用程序调用并需要返回其输入的方法? - How to test method that uses Utility calls and need to return its input? 如何对使用javafx文件选择器的方法进行单元测试 - How to unit test a method that uses javafx file chooser 如何对使用Scanner和InputStream的方法进行单元测试? - How can I unit test a method that uses a Scanner and an InputStream? 如何为使用私有方法的类编写测试用例? - How to write Test case for a Class that uses a private method? 需要使用外部方法的构造函数的值 - need value from constructor that uses external method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM