How to pass variable values between steps in Cucumber Java? - cucumber

I have a variable and I want to pass this variable across all the steps.
Anyone can suggest with an code snippet example please on how to pass a variable value between the steps please.
Any help will be highly appreciated.

In Cucumber for Java (cucumber-jvm) the intended way of sharing data between steps is to use a dependency integration (DI) container - several of which have been integrated with Cucumber.
The method in which you use DI varies slightly from container to container, but here's an example using PicoContainer:
// MySharedData.java
public class MySharedData {
public String stringData;
}
// SomeStepDefs.java
public class SomeStepDefs {
private MySharedData sharedData;
public SomeStepDefs(MySharedData sharedData) {
this.sharedData = sharedData;
}
// StepDefs omitted
}
// MoreStepDefs.java
public class MoreStepDefs {
private MySharedData sharedData;
public MoreStepDefs(MySharedData sharedData) {
this.sharedData = sharedData;
}
// StepDefs omitted
}
The DI container will ensure that a single instance of MySharedData is created for each scenario and is passed to every step definition class that requires it. The benefit of this approach is that Cucumber ensures that no shared state leaks between scenarios, because the injected dependency is created afresh for each scenario.
The example above uses constructor injection (so the injected dependency is specified by a constructor parameter) but other DI containers also support other injection mechanisms, such as Spring's #Autowired.
To get Cucumber to use DI you'll need to choose one (and only one) of the DI integrations and include it on your classpath (or in your POM). The choice is between:
PicoContainer (cucumber-picocontainer.jar)
Guice (cucumber-guice.jar)
Weld (cucumber-weld.jar)
Spring (cucumber-spring.jar)
OpenEJB (cucumber-openejb.jar)
You'll also need to install the selected DI container itself, because the Cucumber jars only provide the integration between Cucumber and the DI container.

private static String myName = null;
#Given("I have a cucumber step")
public void i_have_a_cucumber_step() throws Throwable {
myName = "Stackoverflow"
}
#Given("^I have (\\d+) (.*) in my basket$")
public void i_have_in_my_basket(int number, String veg) throws Throwable {
System.out.println(myName));
}

Related

How to write cucumber acceptance for Java Library

I am in newbie in Cucumber and want to write acceptance test using it.
Important thing here is, I am developing library here so I don't have any http endpoint or any interface.
This library has java methods and further these methods will be consumed by client.
I want to write acceptance for library first.
Following is service which will be consumed by client and want to write ac for this.
How can I do that?
public class HelloWorld{
public String helloWorld(){
return "Hello World";
}
}
I do not think that Cucumber is a good tool for writing tests like that because Cucumber is a sort of a tool that integrates less technical people and more technical people. Better to use pure Java for your need.
However if you want to have some theory you can start from writing a gherkin script:
Feature: Lib testing
Scenario: Test HelloWorld
Given an object of click.webelement.cucumber.lib.HelloWorld class
And calling helloWorld method
Then method returns: Hello World
Then you need to have step definition:
public class StepDefs {
#Inject
ObjMethContainer container;
#Given("an object of {} class")
public void given(String clazz) throws Exception {
container.o = Class
.forName(clazz)
.getConstructor()
.newInstance(new Object[]{});
}
#When("calling {} method")
public void when(String method) throws Exception {
container.m = container.o.getClass().getDeclaredMethod(method);
}
#Then("method returns: {}")
public void then(String val) throws Exception {
assert val.equals(container.m.invoke(container.o));
}
}
N.B. - To share state between steps you need to apply some DI (in my example I use Guice). Where ObjMethContainer is simply:
#ScenarioScoped
public class ObjMethContainer {
public Object o;
public Method m;
}
In my example I generalized exceptions to Exception to save space.

How to configure environment variable for particular cucumber feature file in eclipse?

I am using Cucumber in Java with JUnit and building the project with Maven. I want to use environment variable in the project that should access my entire project. Is there any way to do it?
Your problem title says for a particular cucumber feature file and description of your question says in your whole projects the environment variable should be accessible.
Can you confirm what you want for a particular feature file or for whole project?
In case if you want your environment variables to be accessed in whole project with static value than you can do following things:
Create Single ton class and getter and setter methods to access those environment variables as below
public class GlobalClass {
private static GlobalClass ourInstance;
private HashMap<String, Object> sessionState = new HashMap<>();
public static GlobalClass getInstance() {
if (ourInstance == null) {
return ourInstance = new ThisRun();
}
return ourInstance;
}
public void add(String key, Object value) {
sessionState.put(key, value);
}
public String getAsString(String key) {
return sessionState.get(key).toString();
}
}
so you can add the key and values in this single ton class and can access anywhere in the project

How can I run code in JUnit before Spring starts?

How can I run code in my #RunWith(SpringRunner.class) #SpringBootTest(classes = {...}) JUnit test before Spring starts?
This question has been asked several times (e.g. 1, 2) but was always "solved" by some configuration recommendation or other, never with a universal answer. Kindly don't question what I am about to do in that code but simply suggest a clean way to do it.
Tried so far and failed:
Extend SpringJUnit4ClassRunner to get a class whose constructor can run custom code before initializing Spring. Failed because super(testClass) must be called first thing and already does a whole lot of things that get in the way.
Extend Runner to get a class that delegates to SpringRunner instead of inheriting it. This class could run custom code in its constructor before actually instantiating the SpringRunner. However, this setup fails with obscure error messages like java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig. "Obscure" because my test has no web config and thus shouldn't meddle with sessions and cookies.
Adding an ApplicationContextInitializer that is triggered before Spring loads its context. These things are easy to add to the actual #SpringApplication, but hard to add in Junit. They are also quite late in the process, and a lot of Spring has already started.
One way to do it is to leave out SpringRunner and use the equivalent combination of SpringClassRule and SpringMethodRule instead. Then you can wrap the SpringClassRule and do your stuff before it kicks in:
public class SomeSpringTest {
#ClassRule
public static final TestRule TestRule = new TestRule() {
private final SpringClassRule springClassRule =
new SpringClassRule();
#Override
public Statement apply(Statement statement, Description description) {
System.out.println("Before everything Spring does");
return springClassRule.apply(statement, description);
}
};
#Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
#Test
public void test() {
// ...
}
}
(Tested with 5.1.4.RELEASE Spring verison)
I don't think you can get more "before" than that. As for other options you could also check out #BootstrapWith and #TestExecutionListeners annotations.
Complementing jannis' comment on the question, the option to create an alternative JUnit runner and let it delegate to the SpringRunner does work:
public class AlternativeSpringRunner extends Runner {
private SpringRunner springRunner;
public AlternativeSpringRunner(Class testClass) {
doSomethingBeforeSpringStarts();
springRunner = new SpringRunner(testClass);
}
private doSomethingBeforeSpringStarts() {
// whatever
}
public Description getDescription() {
return springRunner.getDescription();
}
public void run(RunNotifier notifier) {
springRunner.run(notifier);
}
}
Being based on spring-test 4.3.9.RELEASE, I had to override spring-core and spring-tx, plus javax.servlet's servlet-api with higher versions to make this work.

Mockito implemetation for formhandlers in ATG

I am new to Mockito as a concept. Can you please help me understand using Mockito for formhandlers in ATG. Some examples will be appreciated.
There is a good answer (related to ATG) for other similar question: using-mockito-for-writing-atg-test-case. Please review if it includes what you need.
Many of ATG-specific components (and form handlers particularly) are known to be "less testable" (in comparison to components developed using TDD/BDD approach), b/c design of OOTB components (including reference application) doesn't always adhere to the principle of having "Low Coupling and High Cohesion"
But still the generic approach is applicable for writing unit-tests for all ATG components.
Below is a framework we've used for testing ATG FormHandlers with Mockito. Obviously you'll need to put in all the proper bits of the test but this should get you started.
public class AcmeFormHandlerTest {
#Spy #InjectMocks private AcmeFormHandler testObj;
#Mock private Validator<AcmeInterface> acmeValidatorMock;
#Mock private DynamoHttpServletRequest requestMock;
#Mock private DynamoHttpServletResponse responseMock;
private static final String ERROR1_KEY = "error1";
private static final String ERROR1_VALUE = "error1value";
#BeforeMethod(groups = { "unit" })
public void setUp() throws Exception {
testObj = new AcmeFormHandler();
initMocks(this);
}
//Test the happy path scenario
#Test(groups = { "unit" })
public void testWithValidData() throws Exception {
testObj.handleUpdate(requestMock, responseMock);
//Assume your formhandler calls a helper method, then ensure the helper method is called once. You verify the working of your helper method as you would do any Unit test
Mockito.verify(testObj).update(Matchers.refEq(requestMock), Matchers.refEq(responseMock), Mockito.anyString(), (AcmeBean) Mockito.anyObject());
}
//Test a validation exception
#Test(groups = { "unit" })
public void testWithInvalidData() throws Exception {
Map<String, String> validationMessages = new HashMap<String, String>();
validationMessages.put(ERROR1_KEY, ERROR1_VALUE);
when(acmeValidatorMock.validate((AcmeInterface) Mockito.any())).thenReturn(validationMessages);
testObj.handleUpdate(requestMock, responseMock);
assertEquals(1, testObj.getFormExceptions().size());
DropletFormException exception = (DropletFormException) testObj.getFormExceptions().get(0);
Assert.assertEquals(exception.getMessage(), ERROR1_VALUE);
}
//Test a runtime exception
#Test(groups = { "unit" })
public void testWithRunProcessException() throws Exception {
doThrow(new RunProcessException("")).when(testObj).update(Matchers.refEq(requestMock), Matchers.refEq(responseMock), Mockito.anyString(), (AcmeBean) Mockito.anyObject());
testObj.handleAddGiftCardToCart(requestMock, responseMock);
assertEquals(1, testObj.getFormExceptions().size());
DropletFormException exception = (DropletFormException) testObj.getFormExceptions().get(0);
Assert.assertEquals(exception.getMessage(), GENERAL_ERROR_KEY);
}
}
Obviously the above is just a framework that fit in nicely with the way in which we developed our FormHandlers. You can also add validation for redirects and stuff like that if you choose:
Mockito.verify(responseMock, Mockito.times(1)).sendLocalRedirect(SUCCESS_URL, requestMock);
Ultimately the caveats of testing other people's code still applies.
Here's what I do when I unit test a form handler (at least until I manage to release a major update for AtgDust). Note that I don't use wildcard imports, so I'm not sure if this causes any namespace conflicts.
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.initMocks;
import org.junit.*;
import static org.junit.Assert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import atg.servlet.*;
import some.form.handler.FormHandler;
#RunWith(JUnit4.class)
public class FormHandlerTest {
#Mock DynamoHttpServletRequest request;
#Mock DynamoHttpServletResponse response;
FormHandler handler;
#Before
public void setup() {
initMocks(this);
handler = new FormHandler();
}
#Test
public void testSubmitHandlerRedirects() {
handler.handleSubmit(request, response);
verify(response).sendLocalRedirect(eq("/success.jsp"), eq(request));
assertThat(handler.getFormError(), is(false));
}
}
The basic idea is to set up custom behavior for mocks/stubs using when() on the mock object method invocation to return some test value or throw an exception, then verify() mock objects were invoked an exact number of times (in the default case, once), and do any assertions on data that's been changed in the form handler. Essentially, you'll want to use when() to emulate any sort of method calls that need to return other mock objects. When do you need to do this? The easiest way to tell is when you get NPEs or other runtime exceptions due to working with nulls, zeros, empty strings, etc.
In an integration test, ideally, you'd be able to use a sort of in-between mock/test servlet that pretends to work like a full application server that performs minimal request/session/global scope management. This is a good use for Arquillian as far as I know, but I haven't gotten around to trying that out yet.

Unable to get default constructor for Integration class ninject

I'm new on using ninject and Dependency Injection, and have a problem using it.
I try to using Ninject on my class libray, and building an integration tests.
now, I see in many example that, for using ninject is just specified the DI Module like this:
Public Class DIModule : NinjectModule
public override void Load()
{
Bind<IUSAServices>().To<USAServices>();
}
And then on my test class, I try to call my dependency is like this:
[TestClass]
public class USAIntegrationTests
{
private readonly IUSAServices _usaService;
public USAIntegrationTests(IUSAServices usaServices)
{
_usaService = usaServices;
}
[TestMethod]
public void ValidateUserTests()
{
Assert.IsTrue(_usaService.ValidateUser("username1", "password1"));
}
}
And Getting this error:
Unable to get default constructor for class USATests.IntegrationTests.USAIntegrationTests.
However I read the documentation and tried like this:
[TestClass]
public class USAIntegrationTests
{
private readonly IUSAServices _usaService;
public USAIntegrationTests()
{
using (IKernel kernel = new StandardKernel(new DIModule()))
{
_usaService = kernel.Get<IUSAServices>();
}
}
[TestMethod]
public void ValidateUserTests()
{
Assert.IsTrue(_usaService.ValidateUser("mantab", "banget"));
}
}
The test is works properly.
My question is, why I getting that error? is that some way to get around it?
Thanks in advance.
Unit test frameworks require your test classes to have a default constructor. You usually can't integrate DI containers with them. Instead of using constructor injection, you will have to call the container directly from your code, although for unit tests you should typically not have a container at all (for integration tests however, this is okay).
You can add a paramterless constructor for the class. It worked for me.

Resources