我想使用带有 Cucumber jvm 的功能文件来保存测试数据。
我正在测试一种在处理之前使用 hibernate 加载对象的方法。
public Deal getDealById(Long dealId) { deal = template.get(Deal.class, dealId); BigDecimal totalAmount = new BigDecimal(); //loop through all of the loans related to this deal to add up a value for (Loan tempLoan: deal.loanList) { //add amount from each loan together BigDecimal totalAmount = totalAmount + tempLoan.amount; } //set the total amount value on the deal object deal.setTotalAmount(totalAmount); return deal; }
由于我必须指定要加载的 dealId,我该如何处理加载?
我的理解是,我需要“模拟”连接以及模拟连接产生的对象。
我分别查看了Jmock、mockito和dbunit,但我不明白该怎么做。
如果有任何意见,我将不胜感激。
编辑注释
我添加了更多代码,我们正在从数据库中检索一个对象。接下来,我们循环遍历与交易相关的对象列表(多对一),并将每笔贷款的金额添加到交易金额中。最后,我们在返回交易之前设置交易的总金额。
那么考虑到我想从功能文件提供测试信息,我该如何为此方法编写 Junit 测试呢?
在“真实”场景中,这是可行的,我们将交易加载到应用程序中,添加每笔贷款的贷款金额,并将其设置到交易中,然后返回。但考虑到我们必须从该方法内的数据库加载,我不明白如何为此测试编写 JUnit。
最佳答案
将逻辑重构到您的 Deal
类中,您根本不需要模拟数据库。而且它是更好的 OO 设计。
public Deal getDealById(Long dealId) {
return template.get(Deal.class, dealId);
}
public class Deal {
public BigDecimal recalculateTotalLoanAmount() {
BigDecimal totalAmount = new BigDecimal();
for (Loan tempLoan: deal.loanList)
totalAmount = totalAmount + tempLoan.amount;
setTotalAmount(totalAmount);
return totalAmount;
}
}
关于java - 使用 Cucumber JVM/JUnit 加载数据库该怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11849338/