0

i'm writing a cucumber test and i come up with some difficulty:

I have a step which creates dto and saves it using save client which returns dto back again i would need to use that returned dto for other step but don't know how to make it.

Here's how it looks in code :

commonExpenseCreationSteps.java

@Given("^new \"([^\"]*)\" expense with type \"([^"]*)\"$")
public ExpenseDTO newExpense(String description, String expenseType) throws Throwable {
    ExpenseDTO expenseDTO = new ExpenseDTO();
    expenseDTO.setDefaultPurpose(description);
    expenseDTO.setExpenseType(expenseType);
    return expenseSaveClient.save(expenseDTO);
}

expenseTransactionsSendSteps.java

@Given("^send expense for Approval$")
public void sendExpenseForApproval() throws InterruptedException {
    expenseTransactionSendClient.sendToApproval(expenseDTO);
}

How it would be possible to store value returned by one Step and use it in other one in this case i return ExpenseDTO in newExpense method but i need to use it in sendExpenseForApproval but don't know how to do it !?

Dadonis
  • 77
  • 2
  • 7
  • http://stackoverflow.com/questions/34449948/how-to-pass-variable-values-between-steps-in-cucumber-java check this out – Amit Malik Apr 05 '17 at 09:16

2 Answers2

0

Create expenseDTO object outside of your glue code, probably in your stepdef class constructor.

ExpenseDTO expenseDTO = new ExpenseDTO();
MamathaMacherla
  • 1,106
  • 7
  • 8
0

The way to share state between steps in the same class is to use instance variables. Set the value in one step and use that value in a later step.

The way to share state between steps with two or more step classes is to use dependency injection.

I wrote a blog post that describes how it can be done using PicoContainer.

Thomas Sundberg
  • 4,098
  • 3
  • 18
  • 25