HSQLDB: Switching between testing database and production database

165 views Asked by At

I am not getting how to switch the database connection from the actual database to my testing database.I have been calling the function defined in the DAO class from my testing class- So, in test class i have-

userDAO = new UserDAO(); userDAO.conn= databaseTester.getConnection().getConnection();

and in userDAO class i have-

public UserDemoProfileDTO getUserByOTP(String deviceUUID) {
    conn = DBConnector.getPooledConnection(); // here creating connection with actual database
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    UserDemoProfileDTO userDto = new UserDemoProfileDTO();
    try { ...

Problem is how to ensure that when the function is called from the test class it doesn't connect to the actual database (as it is getting connected inside the function) instead gets connected to testing database? I don't know, there must be some function or something to switch between. As I am new to development world, please reply in a basic way. Thank you.

1

There are 1 answers

0
NickJ On

A simple way to specify database connection information is in a .properties file, and you can use different .properties files for test and production, so your Unit tests use a different database.

Some Q and A here: Does JUnit support properties files for tests?