-
Notifications
You must be signed in to change notification settings - Fork 18
Guide to write cucumber test for PHEE
Cucumber is a test writing framework which is used to achieve the idea of BDD(Behaviour Driven Development). To know more about BDD and why it is considered read this article.
- Ghrekin feature file Its is a human readable domain specific language, to deffine a behaviour.
- Step definition Its the actual definition or implementation of each of the steps/ behaviour deffined in the feature file.
- Context configuration Integration test can be spefcific to spring applicaiton, camel specific or any other environment. So cucmber can be configured with different context within which each of the step definition will be executed.
Below are the required dependency to work in the spring and camel environment.
implementation 'io.cucumber:cucumber-java:7.8.1'
implementation 'io.cucumber:cucumber-spring:7.8.1'
testImplementation 'io.cucumber:cucumber-junit:7.8.1'
testImplementation 'org.apache.camel:camel-test:3.4.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test:2.5.4'
Refer the official guide for any help wuth writing feature file. The extention of the feature file is .feature
. Below is one of the sample feature file.
Feature: SLCB integration test
Scenario: Test the payload of the SLCB
Given I have a batchId: "123-123-123", requestId: "3af-567-dfr", purpose: "integration test"
And I mock transactionList with two transactions each of "1" value
And I can start camel context
When I call the buildPayload route
Then the exchange should have a variable with SLCB payload
And I can parse SLCB payload to DTO
And total transaction amount is 2
And total transaction count is 2, failed is 0 and completed is 0
The step definition can be created in a simple plain java class. Inside the java class you can use all the design patterns specific to the context it is run in. So for example you want to use @Autowire
for any bean then make sure you are using the SpringBootTest
context and that bean is present in that context.
Each of the step definition need to match with the phrase mentioned in the feature file with proper annotation. A sample step definition for the Given I have a batchId: "123-123-123", requestId: "3af-567-dfr", purpose: "integration test"
expression is added below. Where {string} is the placeholder for the variable. To find more about variables data type refer this.
@Given("I have a batchId: {string}, requestId: {string}, purpose: {string}")
public void i_have_required_data(String batchId, String requestId, String purpose){
this.batchId = batchId;
this.requestId = requestId;
this.purpose = purpose;
}
Cucumber can be run in any context. And configuring this part totally depends on the scenario which we are testing. For configuring the context for spring applicaiton use the below annotation on default spring test class or create a new one.
@SpringBootTest
@CucumberContextConfiguration
@ActiveProfiles("test")
@ContextConfiguration(classes = <Main class for spring applciation>, loader = SpringBootContextLoader.class)
The @CucumberContextConfiguration
is responsible for making sure that all the stepDefinitions are executed in this particular environment.
Yay!! 💥 💥
Now we can run respective feature file directly form the intellij.
Adding gradle configuration will allow us to run all the cucumber feature file at using using a CLI.
configurations {
cucumberRuntime {
extendsFrom testImplementation
}
}
task cucumberCli() {
dependsOn assemble, testClasses
doLast {
javaexec {
main = "io.cucumber.core.cli.Main"
classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
args = [
'--plugin', 'pretty',
'--plugin', 'html:target/cucumber-report.html',
'--glue', 'org.mifos.connector.slcb.cucumber',
'src/test/java/resources']
}
}
}
Where the --glue
arg is for defining the package which contains the step definitions. And the last arg is the path which contains all the feature file. After making this configuration run the command ./gradlew cucumberCli
in terminal to make sure the newly added tasks is working.