Google

Nov 6, 2013

jBehave with ExamplesTable

This extends the previous tutorials
Step 1: The story file  which supplies a list of transactions as an input (i.e. via Given) and validates against a list of transactions that are supplied as shown below in Then.

Narrative: As a txns processor verify the number of txns returned

Scenario: verify transactions 

Given a list of transactions
txnCode|txndate|txnAmount
A123|10/07/2005|500.00
A124|10/25/2005|500.00
A125|10/28/2005|500.00
A126|10/29/2005|500.00
A127|10/15/2005|500.00
A128|10/12/2005|500.00

When processTxn method is fired with accountCd = 1234

Then I expect to receive 5 transactions
Then I expect transactions to have
TransactionType|TransactionSubType|OtherIncomeExpType
Income|InterestRevenue|NotApplicable
Income|InterestRevenue|NotApplicable
Expense|OtherIncome|NotApplicable
EXPENSE|InterestRevenue|NotApplicable
INCOME|NotApplicable|NotApplicable


Step 2: Map the story to step class TransactionProcessorStep as shown below. Pay attention to the ExamplesTable class provided by jBehave library.

//...

import org.drools.runtime.StatelessKnowledgeSession;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.model.ExamplesTable;
import org.joda.time.DateTime;
import org.mockito.Mockito;
import org.springframework.stereotype.Component;

@Component
public class TransactionProcessorStep
{

    //...
 //input
 List<Transaction> transactionsList = Collections.EMPTY_LIST;
 //output
 List<Transaction> result;

    @Given("a list of transactions $cashTransactionsTable")
    public void cashTxns(ExamplesTable cashTransactionsTable)
    {
        this.transactionsList = toTransactionsList(cashTransactionsTable);
    }
 
 @When("processTxn method is fired with accountCd = $accountCode")
    public void processTransaction(String accountCode)
    {
 
        try
        {
            //logic to process actual method and get the result
   result = ...
            
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
        
    }

    @Then("I expect to receive $count transactions")
    public void verify(int count)
    {
        try
        {
            MatcherAssert.assertThat(count, Matchers.equalTo(result.size()));
            
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    } 
 
 @Then("I expect transactions to have $transactionsTable")
    public void verify2(ExamplesTable transactionsTable)
    {
        try
        {
            
            int i = 0;
            for (Map<String, String> row : transactionsTable.getRows())
            {
                
                Transaction transaction = result.get(i);
                
                String txnType = row.get("TransactionType");
                MatcherAssert.assertThat(transaction.getTransactionType().name(),
                        Matchers.equalTo(txnType));
                
     String analysisCode = row.get("TransactionSubType");
                MatcherAssert.assertThat(transaction.getAnalysisCode(),
                        Matchers.equalTo(analysisCode));
    
                String otherIncomeExpType = row.get("OtherIncomeExpType");
                MatcherAssert.assertThat(transaction.getOtherIncomeAndExpensesSubType().name(),
                        Matchers.equalTo(otherIncomeExpType));
                 
                ++i;
                
            }
            
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
 
 private List<Transaction> toTransactionsList(ExamplesTable table)
    {
        List<Transaction> ctList = new ArrayList<Transaction>();
        for (Map<String, String> row : table.getRows())
        {
            
            //txnCode|txndate|txnAmount
            String txnCode = row.get("analysisCd");
            String txndate = row.get("txndate");
            String txnAmount = row.get("txnAmount");
   
            Transaction ct = new Transaction();
            ct.setAnalysisCd(txnCode);
            
            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
            try
            {
                ct.setTxnDt(sdf.parse(txndate));
            }
            catch (ParseException e)
            {
                throw new RuntimeException(e);
            }
            ct.setTxnAmount(new BigDecimal(txnAmount));
            
            ctList.add(ct);
        }
        return ctList;
    }
 

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home