Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用另一个数据库进行测试_Java_Testing_Junit_Mocking - Fatal编程技术网

Java 使用另一个数据库进行测试

Java 使用另一个数据库进行测试,java,testing,junit,mocking,Java,Testing,Junit,Mocking,我有一个使用oracle的程序。 我想做一些关于谁做的查询的测试。 Db测试是HSQLDB public class UserDAODataAccess { private Connection connection; private void getConnection() throws MMException { connection = AutoExternalServices.getDatabase().newConnection(); }

我有一个使用oracle的程序。 我想做一些关于谁做的查询的测试。 Db测试是HSQLDB

public class UserDAODataAccess {
    private Connection connection;

    private void getConnection() throws MMException {
        connection = AutoExternalServices.getDatabase().newConnection();
    }


    List<User> getUser(){
        getConnection();
        ...
    }
}   
public类UserDAODataAccess{
专用连接;
私有void getConnection()引发MMException{
connection=AutoExternalServices.getDatabase().newConnection();
}
列表getUser(){
getConnection();
...
}
}   

在测试中,是否可以绕过当前的getConnection并获得另一个db的连接?

用测试连接替换
AutoExternalServices
中的
连接时遇到困难,应该会敲响警钟。通常,这些困难来自糟糕/次优的设计

在您的情况下,如果可以的话,我建议您提供
Connection
作为构造函数参数,并删除
getConnection()
方法。这样就可以很容易地为测试交换
连接

至于绕过
getConnection()
,有各种各样的“黑客”可以做到这一点,包括在系统属性上切换,在特殊测试类中重写方法


但是我不推荐它们中的任何一个。

您在应用程序中使用纯JDBC吗?还是使用持久性框架?如果是,是哪一个?AutoExternalServices.getDatabase()的代码是什么?连接设置这就是依赖项注入的整个点。