private void executeSetUpOperation() throws Exception{
final IDatabaseConnection connection = this.getConnection();
try{
DatabaseOperation.CLEAN_INSERT.execute(connection, this.getDataSet());
}finally{
connection.close();
}
}
private IDataSet getDataSet() throws IOException, DataSetException {
return new FlatXmlDataSet(new File("test/conf/dbunit-user-seed.xml"));
}
private IDatabaseConnection getConnection() throws ClassNotFoundException, SQLException {
final Class driverClass = Class.forName("org.gjt.mm.mysql.Driver");
final Connection jdbcConnection = DriverManager.
getConnection("jdbc:mysql://localhost/ccb01",
"9043", "43xli");
return new DatabaseConnection(jdbcConnection);
}
private void verifyPassword(String fileName) throws Exception{
final IDataSet expectedDataSet = new FlatXmlDataSet(
new File(fileName));
final ITable defJoinData = this.getConnection().
createQueryTable("TestResult",
"select user.username, user.password " +
"from user where user.username=\"jane\"");
final ITable defTable = expectedDataSet.getTable("user");
Assertion.assertEquals(defJoinData, defTable);
}
}
只多一个测试……
请注意这个测试用例没有测试边界用例,例如:如果两个口令字段(newPassword1 和 newPassword2())不匹配。谢天谢地,一旦设置好了,添加另一个测试用例并不难。在清单 12 中,验证了如果两个值不匹配,就生成 ActionError,用户 “jane” 口令在数据库中的值保持不变。
清单 12. 添加新测试
public void testExecuteWithErrors() throws Exception{
setRequestPathInfo("/changePasswordSubmit");
addRequestParameter("username","jane");
addRequestParameter("currentPassword","admin");
addRequestParameter("newPassword1","meme");
addRequestParameter("newPassword2","emem");
actionPerform();
verifyActionErrors(
new String[]{"error.changePassword.passwordsDontMatch"});
verifyPassword("test/conf/dbunit-expect-user-same.xml");
}
文章来源于领测软件测试网 https://www.ltesting.net/