使用 JUnit4 延迟数据库访问?
Delay in DB access with JUnit4?
我正在开发一个需要 JUnit 版本 4 的 Java 控制台应用程序。我想要一个测试来检查用户对象中的布尔字段是否成功地从 0 更新为 1,并且然后再回来。
public void approveTest() throws BankException {
// get the test user to work on
User user = udi.accessUserObject("UNITtestApprove");
// find their user id (it is 590)
String user_id = user.getUser_id();
//approve the account, test that the boolean/number set to 1
udi.approve(user_id);
assert(user.getApproved() == 1);
//remove approval, test that the boolean/number reset to 0
udi.removeApproval(user_id);
assert(user.getApproved() == 0);
}
此测试失败。如果我把它分成两个测试,一个通过,另一个失败,然后相反。似乎我的 getter 没有从我的数据库中获取新的、更新的值,但在测试完成后,该值肯定会更新。当我通过访问我的 DAO 层在我的应用程序中使用这两种方法时,我确信这两种方法都有效。
我正在使用 Spring、Oracle SQL Developer 和 AWS 数据库。谁能帮我找出问题所在,是订单问题还是某种时间问题?
您正在执行 udi.approve(user_id)
,但您没有在检查其值之前从数据库中获取最新版本。相反,您是在更新之前获得的 User
对象上断言。我认为您需要更多类似的东西:
public void approveTest() throws BankException {
// get the test user to work on
final String userName = "UNITtestApprove";
final User user = getUserByName(userName);
// find their user id (it is 590)
String userId = user.getUser_id();
// approve the account, test that the boolean/number set to 1
udi.approve(userId);
assertEquals(1, getUserByName(userName).getApproved());
// remove approval, test that the boolean/number reset to 0
udi.removeApproval(userId);
assertEquals(0, getUserByName(userName).getApproved());
}
public User getUserByName(final String userName) {
return udi.accessUserObject(userName);
}
我正在开发一个需要 JUnit 版本 4 的 Java 控制台应用程序。我想要一个测试来检查用户对象中的布尔字段是否成功地从 0 更新为 1,并且然后再回来。
public void approveTest() throws BankException {
// get the test user to work on
User user = udi.accessUserObject("UNITtestApprove");
// find their user id (it is 590)
String user_id = user.getUser_id();
//approve the account, test that the boolean/number set to 1
udi.approve(user_id);
assert(user.getApproved() == 1);
//remove approval, test that the boolean/number reset to 0
udi.removeApproval(user_id);
assert(user.getApproved() == 0);
}
此测试失败。如果我把它分成两个测试,一个通过,另一个失败,然后相反。似乎我的 getter 没有从我的数据库中获取新的、更新的值,但在测试完成后,该值肯定会更新。当我通过访问我的 DAO 层在我的应用程序中使用这两种方法时,我确信这两种方法都有效。
我正在使用 Spring、Oracle SQL Developer 和 AWS 数据库。谁能帮我找出问题所在,是订单问题还是某种时间问题?
您正在执行 udi.approve(user_id)
,但您没有在检查其值之前从数据库中获取最新版本。相反,您是在更新之前获得的 User
对象上断言。我认为您需要更多类似的东西:
public void approveTest() throws BankException {
// get the test user to work on
final String userName = "UNITtestApprove";
final User user = getUserByName(userName);
// find their user id (it is 590)
String userId = user.getUser_id();
// approve the account, test that the boolean/number set to 1
udi.approve(userId);
assertEquals(1, getUserByName(userName).getApproved());
// remove approval, test that the boolean/number reset to 0
udi.removeApproval(userId);
assertEquals(0, getUserByName(userName).getApproved());
}
public User getUserByName(final String userName) {
return udi.accessUserObject(userName);
}