I want to use a feature file with cucumber jvm that will hold the data for the test.

  • I am testing a method that uses hibernate to load an object before processing.

    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;
    }
    

What do I do about the loading since I have to specify a dealId to load?

My understanding is that I need to "mock" the connection and the object resulting from the mocked connection.

I have looked at Jmock, mockito and dbunit respectively, but I don't understand what to do.

I would appreciate any input.

EDIT NOTES

I added more code, we are retrieving an object from the database. Next, we are looping through a list of objects related to the deal (many to one) and adding the amount of each loan to the deal amount. Finally, we set the total amount on the deal before returning the deal.

So how can I write a Junit test for this method considering I want to supply test information from a feature file?

In a "real" scenario this works, we load the deal in our application and add the loan amounts of each loan and set it onto the deal before returning it. But I don't understand how to write a JUnit for this test considering we have to load from the database inside this method.

有帮助吗?

解决方案

Refactor the logic into your Deal class and you shouldn't need to mock a database at all. Plus it's better OO design.

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;
   }
}

其他提示

I agree with the Garrett's answer that you should refactor your code. However, you have stated in your comments that refactoring "it's not an option for me". With this in mind an alternative solution is to use a database rather than trying to mock one.

There are a number of embedded in-memory databases that can be used as part of your unit tests (e.g. HSQLDB, H2, Apache Derby). You define your database schema in a file so the database is created on the fly. You can also pre-load your database tables with test data so your tests dont have to create it. In-memory database are very fast to load.

I would strongly recommend that you use a Java a framework like Spring which has convenient configuration for getting this type of setup running for you in just a few lines of XML. But, considering you can't refactor your code this probably isn't an option.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top