99. Testing
Testing
Unit tests
JUnit tests are the simple and fast tests that use mocks to check simple, single functionality. You should write JUnit to check every function that you use in your code. JUnit must be fast, it should be matters of seconds to run that test.
Naming param is <className>Test.java
Integration tests
Integration tests are here to check whenever the functions that you use in code work well together. Those tests may take longer to run. In maven they are run in integration-test phase.
Naming param is <className>IT.java, or <className>IntegrationTest.java.
Acceptance tests
Acceptance testing is a testing technique performed to determine whether or not the software system has met the requirement specifications. The main purpose of this test is to evaluate the system's compliance with the business requirements and verify if it is has met the required criteria for delivery to end users.
There are various forms of acceptance testing: User Acceptance Testing, Business Acceptance Testing, Alpha Testing, Beta Testing.
Naming param is AcceptanceTest<className>.java.
Cucumber
Cucumber is a software tool used by computer programmers for testing other software. It runs automated acceptance tests written in a behavior-driven development (BDD) style. Central to the Cucumber BDD approach is its plain language parser called Gherkin. It allows expected software behaviors to be specified in a logical language that customers can understand. As such, Cucumber allows the execution of feature documentation written in business-facing text.
Testing with Cucumber
To start working with Cucumber we have to add following dependencies to project:
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency>
To make a executable test you have to prepare 3 files: Feature description, Steps definition and Runner file.
Feature file should be located in resources directory, i.e. (...)/<moduleName>/src/test/resources/cucumber/<FeatureFileName>.feature
It is preferred that Steps definitions file is located in following directory (...)/<moduleName>/src/test/java/steps/<StepsFileName>.java
Runner file like all tests should be located in (...)/<moduleName>/src/test/java/AcceptanceTest<TestedClassName>.java
Feature
A feature is a Use Case that describes a specific function of the software being tested. There are three parts to a Feature
The Feature: keyword
The Feature name (on the same line as the keyword)
An optional description on the following lines
Example Feature definition
Feature: Withdraw Money from ATM A user with an account at a bank would like to withdraw money from an ATM. Provided he has a valid account and debit or credit card, he should be allowed to make the transaction. The ATM will tend the requested amount of money, return his card, and subtract amount of the withdrawal from the user's account. Scenario: Scenario 1 Given preconditions When actions Then results Scenario: Scenario 2 ...
Steps Definitions
It is a file which contains steps definitions. It contains source code which should be done within a test step. Parameters defined in feature files are passed to step definitions with regular expressions. To make them work correctly, you should configure methods for each step defined in Feature file. Each step is preceded by an annotation connected to step keywords defined in feature file (e.g. @Then, @When etc.).
Example step definition:
@Given("I have a cucumber step") public void i_have_a_cucumber_step() throws Throwable { System.out.println("Step definition exact match");
Runner file
In Runner file you have to set all options about tests, like which features are tested or where steps are located. There's few more options to configure but they are optional, the main thing of this file is that he making our test code working.
Required options are:
- format - It describes how cucumber will format test case output and if it create reports.
- glue - It contains path to package containing Step definition files
- features - It contains path to feature file.
Example runner:
package pl.wroc.pwr.qualityspy.vc; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; import org.junit.runner.RunWith; @RunWith(Cucumber.class) @CucumberOptions( monochrome = true, format = { "pretty", "html:target/cucumber" }, glue = "pl.wroc.pwr.qualityspy.vc.steps", features = "classpath:cucumber/Repository.feature" ) public class AcceptanceTestRepositoryCucumber { }
To run our test all we have to do is just run our test
If everything was configured correct, we should got message about completed tests.
After completed tests Cucumber automatically generate report file, where you can check what was tested, or if something went wrong where was the problem. Report file is generated as file (...)/<moduleName>/target/cucumber/index.html.