re-organize testing modules

This commit is contained in:
Loredana
2019-09-08 15:30:03 +03:00
parent 4e7b8cf19d
commit 35116b0e69
104 changed files with 2325 additions and 191 deletions

View File

@@ -0,0 +1,10 @@
## Relevant Articles
- [Mutation Testing with PITest](http://www.baeldung.com/java-mutation-testing-with-pitest)
- [Intro to JaCoCo](http://www.baeldung.com/jacoco)
- [Cucumber and Scenario Outline](http://www.baeldung.com/cucumber-scenario-outline)
- [Cucumber Java 8 Support](http://www.baeldung.com/cucumber-java-8-support)
- [Introduction to Lambda Behave](http://www.baeldung.com/lambda-behave)
- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java)

View File

@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>testing-libraries</artifactId>
<name>testing-libraries</name>
<packaging>pom</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>..</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>com.insightfullogic</groupId>
<artifactId>lambda-behave</artifactId>
<version>${lambda-behave.version}</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java8</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<lambda-behave.version>0.4</lambda-behave.version>
<cucumber.version>1.2.5</cucumber.version>
</properties>
</project>

View File

@@ -0,0 +1,7 @@
package com.baeldung.cucumber;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}

View File

@@ -0,0 +1,24 @@
package com.baeldung.lambdabehave;
public class Calculator {
private int x;
private int y;
Calculator(int x, int y) {
this.x = x;
this.y = y;
}
public int add() {
return this.x + this.y;
}
public int divide(int a, int b) {
return a / b;
}
public int add(int a, int b) {
return a + b;
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.testing.mutation;
public class Palindrome {
public boolean isPalindrome(String inputString) {
if (inputString.length() == 0) {
return true;
} else {
char firstChar = inputString.charAt(0);
char lastChar = inputString.charAt(inputString.length() - 1);
String mid = inputString.substring(1, inputString.length() - 1);
return (firstChar == lastChar) && isPalindrome(mid);
}
}
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.calculator;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
features = {"classpath:features/calculator.feature", "classpath:features/calculator-scenario-outline.feature"}
, plugin = {"pretty", "json:target/reports/json/calculator.json"}
, glue = {"com.baeldung.cucumber.calculator"}
)
public class CalculatorIntegrationTest {
}

View File

@@ -0,0 +1,38 @@
package com.baeldung.calculator;
import com.baeldung.cucumber.Calculator;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.hamcrest.Matchers;
import org.junit.Assert;
public class CalculatorRunSteps {
private int total;
private Calculator calculator;
@Before
private void init() {
total = -999;
}
@Given("^I have a calculator$")
public void initializeCalculator() throws Throwable {
calculator = new Calculator();
}
@When("^I add (-?\\d+) and (-?\\d+)$")
public void testAdd(int num1, int num2) throws Throwable {
total = calculator.add(num1, num2);
}
@Then("^the result should be (-?\\d+)$")
public void validateResult(int result) throws Throwable {
Assert.assertThat(total, Matchers.equalTo(result));
}
}

View File

@@ -0,0 +1,54 @@
package com.baeldung.lambdabehave;
import com.insightfullogic.lambdabehave.JunitSuiteRunner;
import com.insightfullogic.lambdabehave.Suite;
import com.insightfullogic.lambdabehave.generators.Generator;
import com.insightfullogic.lambdabehave.generators.SourceGenerator;
import org.junit.runner.RunWith;
@RunWith(JunitSuiteRunner.class)
public class CalculatorUnitTest {
private Calculator calculator;
{
Suite.describe("Lambda behave example tests", it -> {
it.isSetupWith(() -> {
calculator = new Calculator(1, 2);
});
it.should("Add the given numbers", expect -> {
expect.that(calculator.add()).is(3);
});
it.should("Throw an exception if divide by 0", expect -> {
expect.exception(ArithmeticException.class, () -> {
calculator.divide(1, 0);
});
});
it.uses(2, 3, 5)
.and(23, 10, 33)
.toShow("%d + %d = %d", (expect, a, b, c) -> {
expect.that(calculator.add(a, b)).is(c);
});
it.requires(2)
.example(Generator.asciiStrings())
.toShow("Reversing a String twice returns the original String", (expect, str) -> {
String same = new StringBuilder(str).reverse()
.reverse()
.toString();
expect.that(same)
.isEqualTo(str);
});
it.requires(2)
.withSource(SourceGenerator.deterministicNumbers(5626689007407L))
.example(Generator.asciiStrings())
.toShow("Reversing a String twice returns the original String", (expect, str) -> {
String same = new StringBuilder(str).reverse()
.reverse()
.toString();
expect.that(same)
.isEqualTo(str);
});
});
}
}

View File

@@ -0,0 +1,34 @@
package com.baeldung.mutation;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.baeldung.mutation.Palindrome;
public class PalindromeUnitTest {
@Test
public void whenEmptyString_thanAccept() {
Palindrome palindromeTester = new Palindrome();
assertTrue(palindromeTester.isPalindrome("noon"));
}
@Test
public void whenPalindrom_thanAccept() {
Palindrome palindromeTester = new Palindrome();
assertTrue(palindromeTester.isPalindrome("noon"));
}
@Test
public void whenNotPalindrom_thanReject(){
Palindrome palindromeTester = new Palindrome();
assertFalse(palindromeTester.isPalindrome("box"));
}
@Test
public void whenNearPalindrom_thanReject(){
Palindrome palindromeTester = new Palindrome();
assertFalse(palindromeTester.isPalindrome("neon"));
}
}

View File

@@ -0,0 +1,12 @@
package com.baeldung.shopping;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features = { "classpath:features/shopping.feature" })
public class ShoppingIntegrationTest {
}

View File

@@ -0,0 +1,22 @@
package com.baeldung.shopping;
import static org.junit.Assert.assertEquals;
import cucumber.api.java8.En;
public class ShoppingStepsDef implements En {
private int budget = 0;
public ShoppingStepsDef() {
Given("I have (\\d+) in my wallet", (Integer money) -> budget = money);
When("I buy .* with (\\d+)", (Integer price) -> budget -= price);
Then("I should have (\\d+) in my wallet", (Integer finalBudget) -> {
assertEquals(budget, finalBudget.intValue());
});
}
}

View File

@@ -0,0 +1,16 @@
Feature: Calculator
As a user
I want to use a calculator to add numbers
So that I don't need to add myself
Scenario Outline: Add two numbers <num1> & <num2>
Given I have a calculator
When I add <num1> and <num2>
Then the result should be <total>
Examples:
| num1 | num2 | total |
| -2 | 3 | 1 |
| 10 | 15 | 25 |
| 99 | -99 | 0 |
| -1 | -10 | -11 |

View File

@@ -0,0 +1,24 @@
Feature: Calculator
As a user
I want to use a calculator to add numbers
So that I don't need to add myself
Scenario: Add two numbers -2 & 3
Given I have a calculator
When I add -2 and 3
Then the result should be 1
Scenario: Add two numbers 10 & 15
Given I have a calculator
When I add 10 and 15
Then the result should be 25
Scenario: Add two numbers 99 & -99
Given I have a calculator
When I add 99 and -99
Then the result should be 0
Scenario: Add two numbers -1 & -10
Given I have a calculator
When I add -1 and -10
Then the result should be -11

View File

@@ -0,0 +1,11 @@
Feature: Shopping
Scenario: Track my budget
Given I have 100 in my wallet
When I buy milk with 10
Then I should have 90 in my wallet
Scenario: Track my budget
Given I have 200 in my wallet
When I buy rice with 20
Then I should have 180 in my wallet