To build the project, type in your terminal mvn clean install
If you want a visual report in .html format
, go to target/html
.
Then open index.html
in your favourite browser.
The output will look something like this:
If you want a visual report about test coverage of project, go to target/site
, and open
index.html
in your favourite browser.
The example of the output:
Cucumber is a testing tool based on BDD. It means that testing via cucumber includes writing .features
files, and implementing their steps in java classes.
To get started add cucumber dependencies into your pom.xml like this:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
Then implement some .features
extension files like this maths_factorial.feature:
Feature: Factorial of number
Scenario: Find the product of two numbers
Given a number with value 3
When we find factorial of the number 3
Then the result must be 6
So, we have provided scenario for our test. Then we need to implement test runner java class like this: MathsUtilFactorialTest.java
@RunWith(Cucumber.class)
@CucumberOptions(
format = {"pretty", "html:target/html/"},
features = "src/test/java/com/github/junit/example/maths/factorial/"
)
public class MathUtilFactorialTest {
}
It is simply a test runner, all that we need is to provide Cucumber.class
and some options like format
and features
for creation html based reports and designation of .features
format files.
The final step is to implement a java class with test steps. We described them in features
format class. This class will contain some annotations like @Given
,@When
and @Then
.
Annotation @Given
means that method should take some parameters for test.
For example:
StringConcatSteps.java
@Given("^a string with value \"([^\"]*)\"$")
public void aStringWithValue(String str1) {
this.str1 = str1;
}
The string in this annotation is linking with .features
file strings, so it can take some values from .features
file.
Next annotation is @When
. This annotation is using for making some conditions, in our example we using it in the following form: @When
(our methods calling) @Then
(expected result). The following code is the example of using @When
annotation:
StringConcatSteps.java
@When("^we concatenate \"([^\"]*)\" and \"([^\"]*)\"$")
public void weConcatenateAnd(String str1, String str2) {
result = stringUtil.concat(str1, str2);
}
And the final step is to make method with annotation @Then
. It is our expected results, like this:
StringConcatSteps.java
@Then("^the result must be \"([^\"]*)\"$")
public void theResultMustBe(String result) {
Assert.assertThat(result,is(this.result));
}
As we can see this tests are very easy to understand but they takes a little more time that TDD tests like JUnit.
JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks. It is an adept of TDD. It is easier to implement than Cucumber because there is no need to write .features
files. There is only one java class which coontains test methods.
To get started add JUnit dependencies into your pom.xml like this:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
After dependencies injection we are ready to write some tests. First thing is to implement some setting up. Use @Before
annotation for setting up before every test or @BeforeClass
for single setting up.
MathUtilTest.java
@Before
public void setUp(){
logger.info("********* SETTING UP *********");
mathUtil = new MathUtil();
logger.info("********* SETTING UP SUCCESSFUL *********");
}
It is easy implementation for testing, we need to mark methods for testing with @Test
annotation, and then use AssertEquals
method from static import of org.junit.Assert.*
. We just put expected value and method call.
MathUtilTest.java
@Test
public void multiplyTest(){
logger.info("********* MULTIPLY TEST *********");
assertEquals(6, mathUtil.multiply(3, 2));
}
After tests we can shut down by adding @After
or @AfterClass
annotations. They are working same as @Before
and @BeforeClass
but they are running after tests.
MathUtilTest.java
@After
public void down(){
logger.info("********* SETTING DOWN *********");
mathUtil = null;
logger.info("********* SETTING DOWN SUCCESSFUL *********");
}