Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have to test some api using JUnit test cases. Actually I have some simple JDBC database connection with API code to retrieve data from MYSQL database. But I need one inmemory databases to test the correctness of code. I am using maven. Can any body give me proper suggestion with steps. If possible please give some sample JUnit code to test that.

Thanks, RK

share|improve this question
    
If your SQL code is generic and could be run across other databases, you can checkout the derby-maven-plugin and my answer to this SO post: stackoverflow.com/questions/14731178/…. –  carlspring Feb 20 at 17:54
add comment

3 Answers

I like to add some code here:

    public JSONObject getResultById(int id) {
    Connection conn = null;
    Statement stmt = null;
    String sqlQuery = "select * from " + table + " where id='"
            + id + "'";
    try {
        //get the connection
        conn = DBConnection.getConnection();
        stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(sqlQuery);
        //return result set in JSON format
        return getJsonString(rs);

    } catch (Exception ex) {
        return null;
    } finally {
        try {
            if (conn != null)
                conn.close();
            if (stmt != null)
                stmt.close();
        } catch (Exception ex) {
            return null;
        }
    }
}

To test this what will be best approach to test it. I some one give some sample JUnit test case then it will be very help full

share|improve this answer
add comment

You could used HSQLDB which is in memory database 100% JDBC API compatible.

To make JSON String from object you could use Jacson. So from jdbc resultset make your object then JSON string.

share|improve this answer
    
public JSONObject getResultById(int id) { Connection conn = null; Statement stmt = null; String sqlQuery = "select * from " + table + " where id='" + id + "'"; try { //get the connection conn = DBConnection.getConnection(); stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sqlQuery); //return result set in JSON format return getJsonString(rs); } catch (Exception ex) { return null; } finally { try { if (conn != null) conn.close(); if (stmt != null) stmt.close(); } catch (Exception ex) { return null; } } } –  user1321939 Aug 20 '13 at 9:08
    
@user1321939, I did not get your comment. any problem? –  sha Aug 20 '13 at 9:11
    
@user1321939, Check updated answer –  sha Aug 20 '13 at 9:15
add comment

Have a look at DBUnit - their how-to is at http://dbunit.sourceforge.net/howto.html

It does exactly what you're looking for.

The DBunit site has lots of examples. What I do is the following:

set up the d/b in the @Before method, so each test gets a clean fixture. An example of the @Before method is:

Connection conn = dataSource.getConnection();
        try {
            IDatabaseConnection connection = new DatabaseConnection(conn);
            DatabaseConfig config = connection.getConfig();
            config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
                    new HsqldbDataTypeFactory());
            DatabaseOperation.CLEAN_INSERT.execute(connection, loadDutyData());
            DatabaseOperation.CLEAN_INSERT
                    .execute(connection, loadHelperData());
        } finally {
            DataSourceUtils.releaseConnection(conn, dataSource);
        }

where the loadHelperData() method does the folllowing:

DataFileLoader loader = new FlatXmlDataFileLoader();
        IDataSet ds = loader.load(TEST_DATA_FILE_HELPER);
        return ds;

The load method simply takes an xml file representing the database. See the DBUnit documentation for much more information.

share|improve this answer
    
I like to give some sample java code here: –  user1321939 Aug 20 '13 at 9:07
    
Updated the answer with an example –  TrueDub Aug 20 '13 at 12:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.