So, your test organization has been using Junit test suites for some time. Now you decide to switch to BDD with Cucumber. Do you need to re-write all your tests? Not at all!! We would show you how below:
Using Spring runner with Cucumber:
In Cucumber, you write your tests in a feature file using Gherkin syntax. The feature file steps are defined in a step definition files and the test itself is triggered by a runner file. Let’s assume you are testing a page called AboutApp and your existing Junit test suite name is AboutAppTestSuite.java.
The corresponding cucumber files are AboutApp.feature, AboutAppStepDefinitions.java and AboutAppTest.java respectively. In addition, we would also need a SpringIntegration class to be able to run cucumber with Spring. This class basically triggers our Junit test suite using Spring runner class TestautomationApplication.
Let’s look at the code snippets below:
AboutApp.feature:
@AboutApp Feature: AboutApp Req number: xxxx Background: Given application is installed and launched @AboutApp @Regression Scenario: client wants to verify about app screen elements When user opens the application Then verify user sees the following in app screen | Screen_Title | | App_Logo | | App_Name | | App_Version | | App_Images | Then close application and update HP QC test run
AboutAppStepDefinitions.java:
import io.cucumber.datatable.DataTable; import io.cucumber.java.en.And; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.java.en.When; import java.util.List; public class AboutAppStepDefinitions extends SpringIntegration { @Given("application is installed and launched") public void application_is_installed_and_launched() throws Exception { setUp(); } @When("user opens the application") public void user_opens_the_application() { navigateToScreen(); } @Then("verify user sees the following in app screen") public void verify_user_sees_the_following_in_app_screen(DataTable dt) { List<String> list = dt.asList(String.class); for (String str : list) { switch (str) { case "Screen_Title": testScenario1(); break; case "App_Logo": testScenario2(); break; case "App_Name": testScenario3(); break; case "App_Version": testScenario4(); break; default: testScenario5(); break; } } } @Then("close application and update HP QC test run") public void close_application_and_update_HP_QC_test_run() throws Exception { tearDown(); } }
AboutAppTest.java:
import io.cucumber.junit.Cucumber; import io.cucumber.junit.CucumberOptions; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions(features = "src/test/resources/features", glue = {"com.taf.testautomation.cucumber.aboutapp"}, monochrome = true, plugin = {"html:build/cucumber-html-report-normal", "json:build/cucumber.json", "com.taf.testautomation.cucumber.ExtentCucumberAdapter:"}, tags = {"@AboutApp"}) public class AboutAppTest { }
SpringIntegration.java:
import com.taf.testautomation.TestautomationApplication; import com.taf.testautomation.uitests.AboutAppTestSuite; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.jupiter.SpringExtension; @ExtendWith(SpringExtension.class) @SpringBootTest(classes = {TestautomationApplication.class}) @ContextConfiguration(classes = TestautomationApplication.class) public class SpringIntegration extends AboutAppTestSuite { }
The AboutApp feature file captures the verification points for our hypothetical app. I have used a datable to list the verification points. AboutAppStepDefinitions class defines these steps. But we already have a Junit test suite AboutAppTestSuite.java with test methods testScenario1() to testScenario5(). We invoke these methods from step definition using SpringIntegration class. So now, if you invoke the runner file, the test methods in AboutAppTestSuite gets invoked.