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.
我必须使用JUnit测试用例测试一些api。实际上我有一些简单的JDBC数据库连接与API代码从MYSQL数据库中检索数据。但我需要一个内存数据库来测试代码的正确性。我正在使用maven。任何机构都可以给我适当的建议和步骤。如果可能的话,请提供一些示例JUnit代码来测试它。
Thanks, RK
谢谢,RK
3 个解决方案
#1
#2
1
Have a look at DBUnit - their how-to is at http://dbunit.sourceforge.net/howto.html
看看DBUnit - 他们的操作方法是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:
DBunit网站有很多例子。我所做的是以下内容:
set up the d/b in the @Before method, so each test gets a clean fixture. An example of the @Before method is:
在@Before方法中设置d / b,因此每个测试都得到一个干净的夹具。 @Before方法的一个例子是:
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:
loadHelperData()方法执行以下操作:
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.
load方法只需要一个表示数据库的xml文件。有关更多信息,请参阅DBUnit文档。
#3
0
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
为了测试这个测试它的最佳方法。我有些人提供了一些示例JUnit测试用例然后它会非常有用
#1
1
You could used HSQLDB which is in memory database 100% JDBC API compatible.
您可以使用与内存数据库100%JDBC API兼容的HSQLDB。
To make JSON String from object you could use Jacson. So from jdbc resultset make your object then JSON string.
要从对象制作JSON字符串,您可以使用Jacson。所以从jdbc结果集使你的对象然后JSON字符串。
#2
1
Have a look at DBUnit - their how-to is at http://dbunit.sourceforge.net/howto.html
看看DBUnit - 他们的操作方法是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:
DBunit网站有很多例子。我所做的是以下内容:
set up the d/b in the @Before method, so each test gets a clean fixture. An example of the @Before method is:
在@Before方法中设置d / b,因此每个测试都得到一个干净的夹具。 @Before方法的一个例子是:
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:
loadHelperData()方法执行以下操作:
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.
load方法只需要一个表示数据库的xml文件。有关更多信息,请参阅DBUnit文档。
#3
0
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
为了测试这个测试它的最佳方法。我有些人提供了一些示例JUnit测试用例然后它会非常有用