Merge pull request #7689 from sjmillington/testing-libraries
[BAEL-16669] moved testing-modules/testing modules into relevant folders
This commit is contained in:
commit
4e7b8cf19d
|
@ -0,0 +1,81 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>assertion-libraries</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>assertion-libraries</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.truth</groupId>
|
||||
<artifactId>truth</artifactId>
|
||||
<version>${truth.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.truth.extensions</groupId>
|
||||
<artifactId>truth-java8-extension</artifactId>
|
||||
<version>${truth.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-guava</artifactId>
|
||||
<version>${assertj-guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>javalite-common</artifactId>
|
||||
<version>${javalite.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jgotesting</groupId>
|
||||
<artifactId>jgotesting</artifactId>
|
||||
<version>${jgotesting.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-assertions-generator-maven-plugin</artifactId>
|
||||
<version>${assertj-generator.version}</version>
|
||||
<configuration>
|
||||
<classes>
|
||||
<param>com.baeldung.testing.assertj.custom.Person</param>
|
||||
</classes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<truth.version>0.32</truth.version>
|
||||
<assertj-guava.version>3.1.0</assertj-guava.version>
|
||||
<assertj-core.version>3.9.0</assertj-core.version>
|
||||
<assertj-generator.version>2.1.0</assertj-generator.version>
|
||||
<javalite.version>1.4.13</javalite.version>
|
||||
<jgotesting.version>0.12</jgotesting.version>
|
||||
</properties>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</project>
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj;
|
||||
package com.baeldung.assertj;
|
||||
|
||||
public class Dog {
|
||||
private String name;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj;
|
||||
package com.baeldung.assertj;
|
||||
|
||||
public class Member {
|
||||
private String name;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj;
|
||||
package com.baeldung.assertj;
|
||||
|
||||
public class Person {
|
||||
private String name;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj.custom;
|
||||
package com.baeldung.assertj.custom;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.truth;
|
||||
package com.baeldung.truth;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.truth;
|
||||
package com.baeldung.truth;
|
||||
|
||||
import com.google.common.truth.ComparableSubject;
|
||||
import com.google.common.truth.FailureStrategy;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj;
|
||||
package com.baeldung.assertj;
|
||||
|
||||
import static org.assertj.core.api.Assertions.allOf;
|
||||
import static org.assertj.core.api.Assertions.anyOf;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj;
|
||||
package com.baeldung.assertj;
|
||||
|
||||
import org.assertj.core.util.Maps;
|
||||
import org.junit.Ignore;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj;
|
||||
package com.baeldung.assertj;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ArrayListMultimap;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj;
|
||||
package com.baeldung.assertj;
|
||||
|
||||
import org.junit.Test;
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package com.baeldung.testing.assertj.custom;
|
||||
package com.baeldung.assertj.custom;
|
||||
|
||||
import static com.baeldung.testing.assertj.custom.Assertions.assertThat;
|
||||
import static com.baeldung.assertj.custom.Assertions.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import org.junit.Rule;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj.custom;
|
||||
package com.baeldung.assertj.custom;
|
||||
|
||||
public class Assertions {
|
||||
public static PersonAssert assertThat(Person actual) {
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj.custom;
|
||||
package com.baeldung.assertj.custom;
|
||||
|
||||
import org.assertj.core.api.AbstractAssert;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj.exceptions;
|
||||
package com.baeldung.assertj.exceptions;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.assertj.exceptions;
|
||||
package com.baeldung.assertj.exceptions;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.truth;
|
||||
package com.baeldung.truth;
|
||||
|
||||
import com.google.common.collect.ArrayListMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
|
@ -6,7 +6,7 @@ import com.google.common.collect.Range;
|
|||
import com.google.common.collect.Table;
|
||||
import com.google.common.collect.TreeBasedTable;
|
||||
import com.google.common.collect.TreeMultiset;
|
||||
import static com.baeldung.testing.truth.UserSubject.*;
|
||||
import static com.baeldung.truth.UserSubject.*;
|
||||
import static com.google.common.truth.Truth.*;
|
||||
import static com.google.common.truth.Truth8.*;
|
||||
import java.math.BigDecimal;
|
4
pom.xml
4
pom.xml
|
@ -796,7 +796,9 @@
|
|||
<module>structurizr</module>
|
||||
<module>struts-2</module>
|
||||
|
||||
<module>assertion-libraries</module>
|
||||
<module>testing-modules</module>
|
||||
<module>testing-modules-2</module>
|
||||
|
||||
<module>twilio</module>
|
||||
<module>twitter4j</module>
|
||||
|
@ -1490,7 +1492,9 @@
|
|||
<module>structurizr</module>
|
||||
<module>struts-2</module>
|
||||
|
||||
<module>assertion-libraries</module>
|
||||
<module>testing-modules</module>
|
||||
<module>testing-modules-2</module>
|
||||
|
||||
<module>twilio</module>
|
||||
<module>twitter4j</module>
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
|
||||
## Testing Modules
|
||||
|
||||
This is a aggregator module containing several modules focused on testing libraries 2.
|
||||
|
|
@ -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-modules-2</artifactId>
|
||||
<name>testing-modules-2</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>
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.calculator;
|
||||
package com.baeldung.calculator;
|
||||
|
||||
import cucumber.api.CucumberOptions;
|
||||
import cucumber.api.junit.Cucumber;
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.calculator;
|
||||
package com.baeldung.calculator;
|
||||
|
||||
import com.baeldung.cucumber.Calculator;
|
||||
import cucumber.api.java.Before;
|
|
@ -1,11 +1,11 @@
|
|||
package com.baeldung.mutation.test;
|
||||
package com.baeldung.mutation;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.testing.mutation.Palindrome;
|
||||
import com.baeldung.mutation.Palindrome;
|
||||
|
||||
public class PalindromeUnitTest {
|
||||
@Test
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.shopping;
|
||||
package com.baeldung.shopping;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.testing.shopping;
|
||||
package com.baeldung.shopping;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import cucumber.api.java8.En;
|
|
@ -1,136 +1,136 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>
|
||||
<groupId>org.baeldung</groupId>
|
||||
<artifactId>gatling</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>gatling</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.gatling</groupId>
|
||||
<artifactId>gatling-app</artifactId>
|
||||
<version>${gatling.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.gatling</groupId>
|
||||
<artifactId>gatling-recorder</artifactId>
|
||||
<version>${gatling.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.gatling.highcharts</groupId>
|
||||
<artifactId>gatling-charts-highcharts</artifactId>
|
||||
<version>${gatling.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.scala-lang</groupId>
|
||||
<artifactId>scala-library</artifactId>
|
||||
<version>${scala.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.gatling.highcharts</groupId>
|
||||
<artifactId>gatling-charts-highcharts</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.gatling</groupId>
|
||||
<artifactId>gatling-app</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.gatling</groupId>
|
||||
<artifactId>gatling-recorder</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.scala-lang</groupId>
|
||||
<artifactId>scala-library</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<testSourceDirectory>src/test/scala</testSourceDirectory>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>net.alchim31.maven</groupId>
|
||||
<artifactId>scala-maven-plugin</artifactId>
|
||||
<version>${scala-maven-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>net.alchim31.maven</groupId>
|
||||
<artifactId>scala-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<args>
|
||||
<!--<arg>-Ybackend:GenBCode</arg> -->
|
||||
<arg>-Ydelambdafy:method</arg>
|
||||
<arg>-target:jvm-1.8</arg>
|
||||
<arg>-deprecation</arg>
|
||||
<arg>-feature</arg>
|
||||
<arg>-unchecked</arg>
|
||||
<arg>-language:implicitConversions</arg>
|
||||
<arg>-language:postfixOps</arg>
|
||||
</args>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>simulation</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>io.gatling</groupId>
|
||||
<artifactId>gatling-maven-plugin</artifactId>
|
||||
<version>${gatling-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>test</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<disableCompiler>true</disableCompiler>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<encoding>UTF-8</encoding>
|
||||
<scala.version>2.12.6</scala.version> <!--2.11.12 --> <!--2.12.6 -->
|
||||
<gatling.version>2.3.1</gatling.version> <!--2.2.5 --> <!--2.3.1 -->
|
||||
<scala-maven-plugin.version>3.2.2</scala-maven-plugin.version> <!--3.2.2 --> <!--3.3.2 -->
|
||||
<gatling-maven-plugin.version>2.2.4</gatling-maven-plugin.version> <!--2.2.1 --> <!--2.2.4 -->
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>
|
||||
<groupId>org.baeldung</groupId>
|
||||
<artifactId>gatling</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>gatling</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.gatling</groupId>
|
||||
<artifactId>gatling-app</artifactId>
|
||||
<version>${gatling.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.gatling</groupId>
|
||||
<artifactId>gatling-recorder</artifactId>
|
||||
<version>${gatling.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.gatling.highcharts</groupId>
|
||||
<artifactId>gatling-charts-highcharts</artifactId>
|
||||
<version>${gatling.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.scala-lang</groupId>
|
||||
<artifactId>scala-library</artifactId>
|
||||
<version>${scala.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.gatling.highcharts</groupId>
|
||||
<artifactId>gatling-charts-highcharts</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.gatling</groupId>
|
||||
<artifactId>gatling-app</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.gatling</groupId>
|
||||
<artifactId>gatling-recorder</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.scala-lang</groupId>
|
||||
<artifactId>scala-library</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<testSourceDirectory>src/test/scala</testSourceDirectory>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>net.alchim31.maven</groupId>
|
||||
<artifactId>scala-maven-plugin</artifactId>
|
||||
<version>${scala-maven-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>net.alchim31.maven</groupId>
|
||||
<artifactId>scala-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<args>
|
||||
<!--<arg>-Ybackend:GenBCode</arg> -->
|
||||
<arg>-Ydelambdafy:method</arg>
|
||||
<arg>-target:jvm-1.8</arg>
|
||||
<arg>-deprecation</arg>
|
||||
<arg>-feature</arg>
|
||||
<arg>-unchecked</arg>
|
||||
<arg>-language:implicitConversions</arg>
|
||||
<arg>-language:postfixOps</arg>
|
||||
</args>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>simulation</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>io.gatling</groupId>
|
||||
<artifactId>gatling-maven-plugin</artifactId>
|
||||
<version>${gatling-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>test</phase>
|
||||
<goals>
|
||||
<goal>execute</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<disableCompiler>true</disableCompiler>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<encoding>UTF-8</encoding>
|
||||
<scala.version>2.12.6</scala.version> <!--2.11.12 --> <!--2.12.6 -->
|
||||
<gatling.version>2.3.1</gatling.version> <!--2.2.5 --> <!--2.3.1 -->
|
||||
<scala-maven-plugin.version>3.2.2</scala-maven-plugin.version> <!--3.2.2 --> <!--3.3.2 -->
|
||||
<gatling-maven-plugin.version>2.2.4</gatling-maven-plugin.version> <!--2.2.1 --> <!--2.2.4 -->
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
import io.gatling.app.Gatling
|
||||
import io.gatling.core.config.GatlingPropertiesBuilder
|
||||
|
||||
object Engine extends App {
|
||||
|
||||
val props = new GatlingPropertiesBuilder
|
||||
props.dataDirectory(IDEPathHelper.dataDirectory.toString)
|
||||
props.resultsDirectory(IDEPathHelper.resultsDirectory.toString)
|
||||
props.bodiesDirectory(IDEPathHelper.bodiesDirectory.toString)
|
||||
props.binariesDirectory(IDEPathHelper.mavenBinariesDirectory.toString)
|
||||
|
||||
Gatling.fromMap(props.build)
|
||||
}
|
||||
import io.gatling.app.Gatling
|
||||
import io.gatling.core.config.GatlingPropertiesBuilder
|
||||
|
||||
object Engine extends App {
|
||||
|
||||
val props = new GatlingPropertiesBuilder
|
||||
props.dataDirectory(IDEPathHelper.dataDirectory.toString)
|
||||
props.resultsDirectory(IDEPathHelper.resultsDirectory.toString)
|
||||
props.bodiesDirectory(IDEPathHelper.bodiesDirectory.toString)
|
||||
props.binariesDirectory(IDEPathHelper.mavenBinariesDirectory.toString)
|
||||
|
||||
Gatling.fromMap(props.build)
|
||||
}
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
import java.nio.file.Path
|
||||
|
||||
import io.gatling.commons.util.PathHelper._
|
||||
|
||||
object IDEPathHelper {
|
||||
|
||||
val gatlingConfUrl: Path = getClass.getClassLoader.getResource("gatling.conf").toURI
|
||||
val projectRootDir = gatlingConfUrl.ancestor(3)
|
||||
|
||||
val mavenSourcesDirectory = projectRootDir / "src" / "test" / "scala"
|
||||
val mavenResourcesDirectory = projectRootDir / "src" / "test" / "resources"
|
||||
val mavenTargetDirectory = projectRootDir / "target"
|
||||
val mavenBinariesDirectory = mavenTargetDirectory / "test-classes"
|
||||
|
||||
val dataDirectory = mavenResourcesDirectory / "data"
|
||||
val bodiesDirectory = mavenResourcesDirectory / "bodies"
|
||||
|
||||
val recorderOutputDirectory = mavenSourcesDirectory
|
||||
val resultsDirectory = mavenTargetDirectory / "gatling"
|
||||
|
||||
val recorderConfigFile = mavenResourcesDirectory / "recorder.conf"
|
||||
}
|
||||
import java.nio.file.Path
|
||||
|
||||
import io.gatling.commons.util.PathHelper._
|
||||
|
||||
object IDEPathHelper {
|
||||
|
||||
val gatlingConfUrl: Path = getClass.getClassLoader.getResource("gatling.conf").toURI
|
||||
val projectRootDir = gatlingConfUrl.ancestor(3)
|
||||
|
||||
val mavenSourcesDirectory = projectRootDir / "src" / "test" / "scala"
|
||||
val mavenResourcesDirectory = projectRootDir / "src" / "test" / "resources"
|
||||
val mavenTargetDirectory = projectRootDir / "target"
|
||||
val mavenBinariesDirectory = mavenTargetDirectory / "test-classes"
|
||||
|
||||
val dataDirectory = mavenResourcesDirectory / "data"
|
||||
val bodiesDirectory = mavenResourcesDirectory / "bodies"
|
||||
|
||||
val recorderOutputDirectory = mavenSourcesDirectory
|
||||
val resultsDirectory = mavenTargetDirectory / "gatling"
|
||||
|
||||
val recorderConfigFile = mavenResourcesDirectory / "recorder.conf"
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
import io.gatling.recorder.GatlingRecorder
|
||||
import io.gatling.recorder.config.RecorderPropertiesBuilder
|
||||
|
||||
object Recorder extends App {
|
||||
|
||||
val props = new RecorderPropertiesBuilder
|
||||
props.simulationOutputFolder(IDEPathHelper.recorderOutputDirectory.toString)
|
||||
props.simulationPackage("org.baeldung")
|
||||
props.bodiesFolder(IDEPathHelper.bodiesDirectory.toString)
|
||||
|
||||
GatlingRecorder.fromMap(props.build, Some(IDEPathHelper.recorderConfigFile))
|
||||
}
|
||||
import io.gatling.recorder.GatlingRecorder
|
||||
import io.gatling.recorder.config.RecorderPropertiesBuilder
|
||||
|
||||
object Recorder extends App {
|
||||
|
||||
val props = new RecorderPropertiesBuilder
|
||||
props.simulationOutputFolder(IDEPathHelper.recorderOutputDirectory.toString)
|
||||
props.simulationPackage("org.baeldung")
|
||||
props.bodiesFolder(IDEPathHelper.bodiesDirectory.toString)
|
||||
|
||||
GatlingRecorder.fromMap(props.build, Some(IDEPathHelper.recorderConfigFile))
|
||||
}
|
||||
|
|
|
@ -1,402 +0,0 @@
|
|||
loadLogFile([{
|
||||
"package": "mocks",
|
||||
"name": "ExampleSpockTest",
|
||||
"start": 1553898111660,
|
||||
"features": [
|
||||
{
|
||||
"name": "should calculate character occurrences in given string",
|
||||
"start": 1553898111662,
|
||||
"end": 1553898111699,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}
|
||||
],
|
||||
"end": 1553898111709,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "mocks",
|
||||
"name": "ItemServiceTest",
|
||||
"start": 1553898111714,
|
||||
"features": [
|
||||
{
|
||||
"name": "should spy on EventPublisher method call",
|
||||
"start": 1553898111714,
|
||||
"output": [
|
||||
"I've published: item-id\n"
|
||||
],
|
||||
"end": 1553898112250,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "should return items",
|
||||
"start": 1553898112250,
|
||||
"end": 1553898112260,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "should publish events about new non-empty saved offers",
|
||||
"start": 1553898112260,
|
||||
"end": 1553898112267,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "should return different items for different ids lists",
|
||||
"start": 1553898112267,
|
||||
"end": 1553898112280,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "should throw ExternalItemProviderException when ItemProvider fails",
|
||||
"start": 1553898112281,
|
||||
"end": 1553898112294,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "should return different items on subsequent call",
|
||||
"start": 1553898112294,
|
||||
"narrative": "When method is called for the first time\nThen empty list is returned\nWhen method is called for the second time\nThen item with id=1 is returned\nWhen method is called for the thirdtime\nThen item with id=2 is returned",
|
||||
"end": 1553898112298,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "should return items sorted by name",
|
||||
"start": 1553898112299,
|
||||
"end": 1553898112307,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}
|
||||
],
|
||||
"end": 1553898112310,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "FirstSpecification",
|
||||
"name": "FirstSpecification",
|
||||
"start": 1553898112314,
|
||||
"features": [
|
||||
{
|
||||
"name": "Should verify notify was called",
|
||||
"start": 1553898112314,
|
||||
"end": 1553898112324,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Should return true value for mock",
|
||||
"start": 1553898112325,
|
||||
"end": 1553898112344,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Should return default value for mock",
|
||||
"start": 1553898112344,
|
||||
"end": 1553898112347,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "numbers to the power of two",
|
||||
"start": 1553898112347,
|
||||
"end": 1553898112358,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Should get an index out of bounds when removing a non-existent item",
|
||||
"start": 1553898112358,
|
||||
"end": 1553898112364,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Should be able to remove from list",
|
||||
"start": 1553898112364,
|
||||
"end": 1553898112366,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "two plus two should equal four",
|
||||
"start": 1553898112366,
|
||||
"end": 1553898112368,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "one plus one should equal two",
|
||||
"start": 1553898112368,
|
||||
"end": 1553898112391,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}
|
||||
],
|
||||
"end": 1553898112394,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "IgnoreTest",
|
||||
"start": 1553898112395,
|
||||
"end": 1553898112395,
|
||||
"result": "skipped"
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "RetryTest",
|
||||
"start": 1553898112403,
|
||||
"end": 1553898112405,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "This title is easy to read for humans",
|
||||
"start": 1553898112407,
|
||||
"end": 1553898112408,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "SeeTest",
|
||||
"start": 1553898112409,
|
||||
"end": 1553898112411,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "StepwiseTest",
|
||||
"start": 1553898112422,
|
||||
"end": 1553898112423,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "NarrativeDescriptionTest",
|
||||
"start": 1553898112427,
|
||||
"narrative": "as a user\n i want to save favourite items \n and then get the list of them",
|
||||
"end": 1553898112433,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "SubjectTest",
|
||||
"start": 1553898112434,
|
||||
"end": 1553898112436,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "IgnoreRestTest",
|
||||
"start": 1553898112437,
|
||||
"end": 1553898112437,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "StackTraceTest",
|
||||
"start": 1553898112438,
|
||||
"features": [
|
||||
{
|
||||
"name": "stacktrace",
|
||||
"start": 1553898112438,
|
||||
"exceptions": [
|
||||
"java.lang.RuntimeException: blabla\n\tat extensions.StackTraceTest.stacktrace(StackTraceTest.groovy:10)\n"
|
||||
],
|
||||
"end": 1553898112455,
|
||||
"result": "failed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}
|
||||
],
|
||||
"end": 1553898112470,
|
||||
"result": "failed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "IgnoreIfTest",
|
||||
"start": 1553898112471,
|
||||
"end": 1553898112472,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "RequiresTest",
|
||||
"start": 1553898112473,
|
||||
"features": [
|
||||
{
|
||||
"name": "I will run only on Windows",
|
||||
"start": 1553898112474,
|
||||
"end": 1553898112474,
|
||||
"result": "skipped"
|
||||
}
|
||||
],
|
||||
"end": 1553898112476,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "IssueTest",
|
||||
"start": 1553898112477,
|
||||
"features": [
|
||||
{
|
||||
"name": "I'm using Spock configuration file",
|
||||
"start": 1553898112477,
|
||||
"tags": [
|
||||
{
|
||||
"name": "Bug LO-1000",
|
||||
"key": "issue",
|
||||
"value": "LO-1000",
|
||||
"url": "http:\/\/jira.org\/issues\/LO-1000"
|
||||
}
|
||||
],
|
||||
"end": 1553898112489,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}
|
||||
],
|
||||
"end": 1553898112490,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "TimeoutTest",
|
||||
"start": 1553898112491,
|
||||
"features": [
|
||||
{
|
||||
"name": "I will fail after 200 millis",
|
||||
"start": 1553898112491,
|
||||
"end": 1553898112514,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}
|
||||
],
|
||||
"end": 1553898112517,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
||||
loadLogFile([{
|
||||
"package": "extensions",
|
||||
"name": "RestoreSystemPropertiesTest",
|
||||
"start": 1553898112518,
|
||||
"features": [
|
||||
{
|
||||
"name": "all environment variables will be saved before execution and restored after tests",
|
||||
"start": 1553898112518,
|
||||
"end": 1553898112532,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}
|
||||
],
|
||||
"end": 1553898112539,
|
||||
"result": "passed",
|
||||
"attachments": [
|
||||
|
||||
]
|
||||
}])
|
||||
|
|
@ -15,4 +15,17 @@
|
|||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>pl.pragmatists</groupId>
|
||||
<artifactId>JUnitParams</artifactId>
|
||||
<version>${jUnitParams.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<jUnitParams.version>1.1.0</jUnitParams.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -1,126 +1,126 @@
|
|||
package com.baeldung.junit5.mockito;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import com.baeldung.junit5.mockito.repository.MailClient;
|
||||
import com.baeldung.junit5.mockito.repository.SettingRepository;
|
||||
import com.baeldung.junit5.mockito.repository.UserRepository;
|
||||
import com.baeldung.junit5.mockito.service.DefaultUserService;
|
||||
import com.baeldung.junit5.mockito.service.Errors;
|
||||
import com.baeldung.junit5.mockito.service.UserService;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class UserServiceUnitTest {
|
||||
|
||||
UserService userService;
|
||||
SettingRepository settingRepository;
|
||||
@Mock UserRepository userRepository;
|
||||
@Mock MailClient mailClient;
|
||||
|
||||
User user;
|
||||
|
||||
@BeforeEach
|
||||
void init(@Mock SettingRepository settingRepository) {
|
||||
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
|
||||
lenient().when(settingRepository.getUserMinAge()).thenReturn(10);
|
||||
when(settingRepository.getUserNameMinLength()).thenReturn(4);
|
||||
lenient().when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false);
|
||||
this.settingRepository = settingRepository;
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) {
|
||||
// Given
|
||||
user = new User("Jerry", 12);
|
||||
when(userRepository.insert(any(User.class))).then(new Answer<User>() {
|
||||
int sequence = 1;
|
||||
|
||||
@Override
|
||||
public User answer(InvocationOnMock invocation) throws Throwable {
|
||||
User user = (User) invocation.getArgument(0);
|
||||
user.setId(sequence++);
|
||||
return user;
|
||||
}
|
||||
});
|
||||
|
||||
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
|
||||
|
||||
// When
|
||||
User insertedUser = userService.register(user);
|
||||
|
||||
// Then
|
||||
verify(userRepository).insert(user);
|
||||
Assertions.assertNotNull(user.getId());
|
||||
verify(mailClient).sendUserRegistrationMail(insertedUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenShortName_whenSaveUser_thenGiveShortUsernameError() {
|
||||
// Given
|
||||
user = new User("tom", 12);
|
||||
|
||||
// When
|
||||
try {
|
||||
userService.register(user);
|
||||
fail("Should give an error");
|
||||
} catch(Exception ex) {
|
||||
assertEquals(ex.getMessage(), Errors.USER_NAME_SHORT);
|
||||
}
|
||||
|
||||
// Then
|
||||
verify(userRepository, never()).insert(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSmallAge_whenSaveUser_thenGiveYoungUserError() {
|
||||
// Given
|
||||
user = new User("jerry", 3);
|
||||
|
||||
// When
|
||||
try {
|
||||
userService.register(user);
|
||||
fail("Should give an error");
|
||||
} catch(Exception ex) {
|
||||
assertEquals(ex.getMessage(), Errors.USER_AGE_YOUNG);
|
||||
}
|
||||
|
||||
// Then
|
||||
verify(userRepository, never()).insert(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenUserWithExistingName_whenSaveUser_thenGiveUsernameAlreadyExistsError() {
|
||||
// Given
|
||||
user = new User("jerry", 12);
|
||||
Mockito.reset(userRepository);
|
||||
when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(true);
|
||||
|
||||
// When
|
||||
try {
|
||||
userService.register(user);
|
||||
fail("Should give an error");
|
||||
} catch(Exception ex) {
|
||||
assertEquals(ex.getMessage(), Errors.USER_NAME_DUPLICATE);
|
||||
}
|
||||
|
||||
// Then
|
||||
verify(userRepository, never()).insert(user);
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.junit5.mockito;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.junit.platform.runner.JUnitPlatform;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import com.baeldung.junit5.mockito.repository.MailClient;
|
||||
import com.baeldung.junit5.mockito.repository.SettingRepository;
|
||||
import com.baeldung.junit5.mockito.repository.UserRepository;
|
||||
import com.baeldung.junit5.mockito.service.DefaultUserService;
|
||||
import com.baeldung.junit5.mockito.service.Errors;
|
||||
import com.baeldung.junit5.mockito.service.UserService;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
@RunWith(JUnitPlatform.class)
|
||||
public class UserServiceUnitTest {
|
||||
|
||||
UserService userService;
|
||||
SettingRepository settingRepository;
|
||||
@Mock UserRepository userRepository;
|
||||
@Mock MailClient mailClient;
|
||||
|
||||
User user;
|
||||
|
||||
@BeforeEach
|
||||
void init(@Mock SettingRepository settingRepository) {
|
||||
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
|
||||
lenient().when(settingRepository.getUserMinAge()).thenReturn(10);
|
||||
when(settingRepository.getUserNameMinLength()).thenReturn(4);
|
||||
lenient().when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false);
|
||||
this.settingRepository = settingRepository;
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) {
|
||||
// Given
|
||||
user = new User("Jerry", 12);
|
||||
when(userRepository.insert(any(User.class))).then(new Answer<User>() {
|
||||
int sequence = 1;
|
||||
|
||||
@Override
|
||||
public User answer(InvocationOnMock invocation) throws Throwable {
|
||||
User user = (User) invocation.getArgument(0);
|
||||
user.setId(sequence++);
|
||||
return user;
|
||||
}
|
||||
});
|
||||
|
||||
userService = new DefaultUserService(userRepository, settingRepository, mailClient);
|
||||
|
||||
// When
|
||||
User insertedUser = userService.register(user);
|
||||
|
||||
// Then
|
||||
verify(userRepository).insert(user);
|
||||
Assertions.assertNotNull(user.getId());
|
||||
verify(mailClient).sendUserRegistrationMail(insertedUser);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenShortName_whenSaveUser_thenGiveShortUsernameError() {
|
||||
// Given
|
||||
user = new User("tom", 12);
|
||||
|
||||
// When
|
||||
try {
|
||||
userService.register(user);
|
||||
fail("Should give an error");
|
||||
} catch(Exception ex) {
|
||||
assertEquals(ex.getMessage(), Errors.USER_NAME_SHORT);
|
||||
}
|
||||
|
||||
// Then
|
||||
verify(userRepository, never()).insert(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenSmallAge_whenSaveUser_thenGiveYoungUserError() {
|
||||
// Given
|
||||
user = new User("jerry", 3);
|
||||
|
||||
// When
|
||||
try {
|
||||
userService.register(user);
|
||||
fail("Should give an error");
|
||||
} catch(Exception ex) {
|
||||
assertEquals(ex.getMessage(), Errors.USER_AGE_YOUNG);
|
||||
}
|
||||
|
||||
// Then
|
||||
verify(userRepository, never()).insert(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenUserWithExistingName_whenSaveUser_thenGiveUsernameAlreadyExistsError() {
|
||||
// Given
|
||||
user = new User("jerry", 12);
|
||||
Mockito.reset(userRepository);
|
||||
when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(true);
|
||||
|
||||
// When
|
||||
try {
|
||||
userService.register(user);
|
||||
fail("Should give an error");
|
||||
} catch(Exception ex) {
|
||||
assertEquals(ex.getMessage(), Errors.USER_NAME_DUPLICATE);
|
||||
}
|
||||
|
||||
// Then
|
||||
verify(userRepository, never()).insert(user);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,104 +1,104 @@
|
|||
package org.baeldung.bddmockito;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
|
||||
|
||||
public class BDDMockitoIntegrationTest {
|
||||
|
||||
PhoneBookService phoneBookService;
|
||||
PhoneBookRepository phoneBookRepository;
|
||||
|
||||
String momContactName = "Mom";
|
||||
String momPhoneNumber = "01234";
|
||||
String xContactName = "x";
|
||||
String tooLongPhoneNumber = "01111111111111";
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
phoneBookRepository = Mockito.mock(PhoneBookRepository.class);
|
||||
phoneBookService = new PhoneBookService(phoneBookRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidContactName_whenSearchInPhoneBook_thenRetunPhoneNumber() {
|
||||
given(phoneBookRepository.contains(momContactName)).willReturn(true);
|
||||
given(phoneBookRepository.getPhoneNumberByContactName(momContactName))
|
||||
.will((InvocationOnMock invocation) -> {
|
||||
if(invocation.getArgument(0).equals(momContactName)) {
|
||||
return momPhoneNumber;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
String phoneNumber = phoneBookService.search(momContactName);
|
||||
|
||||
then(phoneBookRepository).should().contains(momContactName);
|
||||
then(phoneBookRepository).should().getPhoneNumberByContactName(momContactName);
|
||||
Assert.assertEquals(phoneNumber, momPhoneNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidContactName_whenSearch_thenRetunNull() {
|
||||
given(phoneBookRepository.contains(xContactName)).willReturn(false);
|
||||
|
||||
String phoneNumber = phoneBookService.search(xContactName);
|
||||
|
||||
then(phoneBookRepository).should().contains(xContactName);
|
||||
then(phoneBookRepository).should(never()).getPhoneNumberByContactName(xContactName);
|
||||
Assert.assertEquals(phoneNumber, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidContactNameAndPhoneNumber_whenRegister_thenSucceed() {
|
||||
given(phoneBookRepository.contains(momContactName)).willReturn(false);
|
||||
|
||||
phoneBookService.register(momContactName, momPhoneNumber);
|
||||
|
||||
verify(phoneBookRepository).insert(momContactName, momPhoneNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyPhoneNumber_whenRegister_thenFail() {
|
||||
given(phoneBookRepository.contains(momContactName)).willReturn(false);
|
||||
|
||||
phoneBookService.register(xContactName, "");
|
||||
|
||||
then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLongPhoneNumber_whenRegister_thenFail() {
|
||||
given(phoneBookRepository.contains(xContactName)).willReturn(false);
|
||||
willThrow(new RuntimeException())
|
||||
.given(phoneBookRepository).insert(any(String.class), eq(tooLongPhoneNumber));
|
||||
|
||||
try {
|
||||
phoneBookService.register(xContactName, tooLongPhoneNumber);
|
||||
fail("Should throw exception");
|
||||
} catch (RuntimeException ex) { }
|
||||
|
||||
then(phoneBookRepository).should(never()).insert(momContactName, tooLongPhoneNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistentContactName_whenRegister_thenFail() {
|
||||
given(phoneBookRepository.contains(momContactName))
|
||||
.willThrow(new RuntimeException("Name already exist"));
|
||||
|
||||
try {
|
||||
phoneBookService.register(momContactName, momPhoneNumber);
|
||||
fail("Should throw exception");
|
||||
} catch(Exception ex) { }
|
||||
|
||||
then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber);
|
||||
}
|
||||
|
||||
}
|
||||
package org.baeldung.bddmockito;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
|
||||
|
||||
public class BDDMockitoIntegrationTest {
|
||||
|
||||
PhoneBookService phoneBookService;
|
||||
PhoneBookRepository phoneBookRepository;
|
||||
|
||||
String momContactName = "Mom";
|
||||
String momPhoneNumber = "01234";
|
||||
String xContactName = "x";
|
||||
String tooLongPhoneNumber = "01111111111111";
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
phoneBookRepository = Mockito.mock(PhoneBookRepository.class);
|
||||
phoneBookService = new PhoneBookService(phoneBookRepository);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidContactName_whenSearchInPhoneBook_thenRetunPhoneNumber() {
|
||||
given(phoneBookRepository.contains(momContactName)).willReturn(true);
|
||||
given(phoneBookRepository.getPhoneNumberByContactName(momContactName))
|
||||
.will((InvocationOnMock invocation) -> {
|
||||
if(invocation.getArgument(0).equals(momContactName)) {
|
||||
return momPhoneNumber;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
String phoneNumber = phoneBookService.search(momContactName);
|
||||
|
||||
then(phoneBookRepository).should().contains(momContactName);
|
||||
then(phoneBookRepository).should().getPhoneNumberByContactName(momContactName);
|
||||
Assert.assertEquals(phoneNumber, momPhoneNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInvalidContactName_whenSearch_thenRetunNull() {
|
||||
given(phoneBookRepository.contains(xContactName)).willReturn(false);
|
||||
|
||||
String phoneNumber = phoneBookService.search(xContactName);
|
||||
|
||||
then(phoneBookRepository).should().contains(xContactName);
|
||||
then(phoneBookRepository).should(never()).getPhoneNumberByContactName(xContactName);
|
||||
Assert.assertEquals(phoneNumber, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenValidContactNameAndPhoneNumber_whenRegister_thenSucceed() {
|
||||
given(phoneBookRepository.contains(momContactName)).willReturn(false);
|
||||
|
||||
phoneBookService.register(momContactName, momPhoneNumber);
|
||||
|
||||
verify(phoneBookRepository).insert(momContactName, momPhoneNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyPhoneNumber_whenRegister_thenFail() {
|
||||
given(phoneBookRepository.contains(momContactName)).willReturn(false);
|
||||
|
||||
phoneBookService.register(xContactName, "");
|
||||
|
||||
then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLongPhoneNumber_whenRegister_thenFail() {
|
||||
given(phoneBookRepository.contains(xContactName)).willReturn(false);
|
||||
willThrow(new RuntimeException())
|
||||
.given(phoneBookRepository).insert(any(String.class), eq(tooLongPhoneNumber));
|
||||
|
||||
try {
|
||||
phoneBookService.register(xContactName, tooLongPhoneNumber);
|
||||
fail("Should throw exception");
|
||||
} catch (RuntimeException ex) { }
|
||||
|
||||
then(phoneBookRepository).should(never()).insert(momContactName, tooLongPhoneNumber);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenExistentContactName_whenRegister_thenFail() {
|
||||
given(phoneBookRepository.contains(momContactName))
|
||||
.willThrow(new RuntimeException("Name already exist"));
|
||||
|
||||
try {
|
||||
phoneBookService.register(momContactName, momPhoneNumber);
|
||||
fail("Should throw exception");
|
||||
} catch(Exception ex) { }
|
||||
|
||||
then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
package org.baeldung.bddmockito;
|
||||
|
||||
public interface PhoneBookRepository {
|
||||
|
||||
/**
|
||||
* Insert phone record
|
||||
* @param name Contact name
|
||||
* @param phone Phone number
|
||||
*/
|
||||
void insert(String name, String phone);
|
||||
|
||||
/**
|
||||
* Search for contact phone number
|
||||
* @param name Contact name
|
||||
* @return phone number
|
||||
*/
|
||||
String getPhoneNumberByContactName(String name);
|
||||
|
||||
/**
|
||||
* Check if the phonebook contains this contact
|
||||
* @param name Contact name
|
||||
* @return true if this contact name exists
|
||||
*/
|
||||
boolean contains(String name);
|
||||
|
||||
}
|
||||
package org.baeldung.bddmockito;
|
||||
|
||||
public interface PhoneBookRepository {
|
||||
|
||||
/**
|
||||
* Insert phone record
|
||||
* @param name Contact name
|
||||
* @param phone Phone number
|
||||
*/
|
||||
void insert(String name, String phone);
|
||||
|
||||
/**
|
||||
* Search for contact phone number
|
||||
* @param name Contact name
|
||||
* @return phone number
|
||||
*/
|
||||
String getPhoneNumberByContactName(String name);
|
||||
|
||||
/**
|
||||
* Check if the phonebook contains this contact
|
||||
* @param name Contact name
|
||||
* @return true if this contact name exists
|
||||
*/
|
||||
boolean contains(String name);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,34 +1,34 @@
|
|||
package org.baeldung.bddmockito;
|
||||
|
||||
public class PhoneBookService {
|
||||
|
||||
private PhoneBookRepository phoneBookRepository;
|
||||
|
||||
public PhoneBookService(PhoneBookRepository phoneBookRepository) {
|
||||
this.phoneBookRepository = phoneBookRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a contact
|
||||
* @param name Contact name
|
||||
* @param phone Phone number
|
||||
*/
|
||||
public void register(String name, String phone) {
|
||||
if(!name.isEmpty() && !phone.isEmpty() && !phoneBookRepository.contains(name)) {
|
||||
phoneBookRepository.insert(name, phone);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for a phone number by contact name
|
||||
* @param name Contact name
|
||||
* @return Phone number
|
||||
*/
|
||||
public String search(String name) {
|
||||
if(!name.isEmpty() && phoneBookRepository.contains(name)) {
|
||||
return phoneBookRepository.getPhoneNumberByContactName(name);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
package org.baeldung.bddmockito;
|
||||
|
||||
public class PhoneBookService {
|
||||
|
||||
private PhoneBookRepository phoneBookRepository;
|
||||
|
||||
public PhoneBookService(PhoneBookRepository phoneBookRepository) {
|
||||
this.phoneBookRepository = phoneBookRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a contact
|
||||
* @param name Contact name
|
||||
* @param phone Phone number
|
||||
*/
|
||||
public void register(String name, String phone) {
|
||||
if(!name.isEmpty() && !phone.isEmpty() && !phoneBookRepository.contains(name)) {
|
||||
phoneBookRepository.insert(name, phone);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for a phone number by contact name
|
||||
* @param name Contact name
|
||||
* @return Phone number
|
||||
*/
|
||||
public String search(String name) {
|
||||
if(!name.isEmpty() && phoneBookRepository.contains(name)) {
|
||||
return phoneBookRepository.getPhoneNumberByContactName(name);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>javafaker</artifactId>
|
||||
<name>javafaker</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>mocks</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.javafaker</groupId>
|
||||
<artifactId>javafaker</artifactId>
|
||||
<version>${javafaker.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<javafaker.version>0.15</javafaker.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,27 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>jukito</artifactId>
|
||||
<name>jukito</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>mocks</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jukito</groupId>
|
||||
<artifactId>jukito</artifactId>
|
||||
<version>${jukito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<jukito.version>1.5</jukito.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
70
testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/ArticleReader.java
Executable file → Normal file
70
testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/ArticleReader.java
Executable file → Normal file
|
@ -1,36 +1,36 @@
|
|||
package com.baeldung.easymock;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class ArticleReader {
|
||||
|
||||
private List<BaeldungArticle> articles;
|
||||
private Iterator<BaeldungArticle> articleIter;
|
||||
|
||||
public ArticleReader() {
|
||||
this(new ArrayList<>());
|
||||
}
|
||||
|
||||
public ArticleReader(List<BaeldungArticle> articles) {
|
||||
this.articles = articles;
|
||||
this.articleIter = this.articles.iterator();
|
||||
}
|
||||
|
||||
public List<BaeldungArticle> ofTopic(String topic) {
|
||||
return articles
|
||||
.stream()
|
||||
.filter(article -> article
|
||||
.title()
|
||||
.contains(topic))
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
public BaeldungArticle next() {
|
||||
return this.articleIter.next();
|
||||
}
|
||||
|
||||
package com.baeldung.easymock;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class ArticleReader {
|
||||
|
||||
private List<BaeldungArticle> articles;
|
||||
private Iterator<BaeldungArticle> articleIter;
|
||||
|
||||
public ArticleReader() {
|
||||
this(new ArrayList<>());
|
||||
}
|
||||
|
||||
public ArticleReader(List<BaeldungArticle> articles) {
|
||||
this.articles = articles;
|
||||
this.articleIter = this.articles.iterator();
|
||||
}
|
||||
|
||||
public List<BaeldungArticle> ofTopic(String topic) {
|
||||
return articles
|
||||
.stream()
|
||||
.filter(article -> article
|
||||
.title()
|
||||
.contains(topic))
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
public BaeldungArticle next() {
|
||||
return this.articleIter.next();
|
||||
}
|
||||
|
||||
}
|
48
testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungArticle.java
Executable file → Normal file
48
testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungArticle.java
Executable file → Normal file
|
@ -1,25 +1,25 @@
|
|||
package com.baeldung.easymock;
|
||||
|
||||
public class BaeldungArticle {
|
||||
|
||||
public static BaeldungArticle simpleArticle(String title, String content) {
|
||||
return new BaeldungArticle(title, content);
|
||||
}
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
|
||||
private BaeldungArticle(String title, String content) {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String title() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public String content() {
|
||||
return this.content;
|
||||
}
|
||||
|
||||
package com.baeldung.easymock;
|
||||
|
||||
public class BaeldungArticle {
|
||||
|
||||
public static BaeldungArticle simpleArticle(String title, String content) {
|
||||
return new BaeldungArticle(title, content);
|
||||
}
|
||||
|
||||
private String title;
|
||||
private String content;
|
||||
|
||||
private BaeldungArticle(String title, String content) {
|
||||
this.title = title;
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String title() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public String content() {
|
||||
return this.content;
|
||||
}
|
||||
|
||||
}
|
78
testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungReader.java
Executable file → Normal file
78
testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungReader.java
Executable file → Normal file
|
@ -1,40 +1,40 @@
|
|||
package com.baeldung.easymock;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BaeldungReader {
|
||||
|
||||
private ArticleReader articleReader;
|
||||
private IArticleWriter articleWriter;
|
||||
|
||||
public BaeldungReader() {
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
public BaeldungReader(ArticleReader articleReader) {
|
||||
this.articleReader = articleReader;
|
||||
}
|
||||
|
||||
public BaeldungReader(IArticleWriter writer) {
|
||||
this.articleWriter = writer;
|
||||
}
|
||||
|
||||
public BaeldungReader(ArticleReader articleReader, IArticleWriter writer) {
|
||||
this.articleReader = articleReader;
|
||||
this.articleWriter = writer;
|
||||
}
|
||||
|
||||
public BaeldungArticle readNext() {
|
||||
return articleReader.next();
|
||||
}
|
||||
|
||||
public List<BaeldungArticle> readTopic(String topic) {
|
||||
return articleReader.ofTopic(topic);
|
||||
}
|
||||
|
||||
public String write(String title, String content) {
|
||||
return articleWriter.write(title, content);
|
||||
}
|
||||
|
||||
package com.baeldung.easymock;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BaeldungReader {
|
||||
|
||||
private ArticleReader articleReader;
|
||||
private IArticleWriter articleWriter;
|
||||
|
||||
public BaeldungReader() {
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
public BaeldungReader(ArticleReader articleReader) {
|
||||
this.articleReader = articleReader;
|
||||
}
|
||||
|
||||
public BaeldungReader(IArticleWriter writer) {
|
||||
this.articleWriter = writer;
|
||||
}
|
||||
|
||||
public BaeldungReader(ArticleReader articleReader, IArticleWriter writer) {
|
||||
this.articleReader = articleReader;
|
||||
this.articleWriter = writer;
|
||||
}
|
||||
|
||||
public BaeldungArticle readNext() {
|
||||
return articleReader.next();
|
||||
}
|
||||
|
||||
public List<BaeldungArticle> readTopic(String topic) {
|
||||
return articleReader.ofTopic(topic);
|
||||
}
|
||||
|
||||
public String write(String title, String content) {
|
||||
return articleWriter.write(title, content);
|
||||
}
|
||||
|
||||
}
|
12
testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IArticleWriter.java
Executable file → Normal file
12
testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IArticleWriter.java
Executable file → Normal file
|
@ -1,7 +1,7 @@
|
|||
package com.baeldung.easymock;
|
||||
|
||||
public interface IArticleWriter {
|
||||
|
||||
String write(String title, String content);
|
||||
|
||||
package com.baeldung.easymock;
|
||||
|
||||
public interface IArticleWriter {
|
||||
|
||||
String write(String title, String content);
|
||||
|
||||
}
|
114
testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedUnitTest.java
Executable file → Normal file
114
testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedUnitTest.java
Executable file → Normal file
|
@ -1,57 +1,57 @@
|
|||
package com.baeldung.easymock;
|
||||
|
||||
import org.easymock.EasyMockRunner;
|
||||
import org.easymock.Mock;
|
||||
import org.easymock.TestSubject;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
@RunWith(EasyMockRunner.class)
|
||||
public class BaeldungReaderAnnotatedUnitTest {
|
||||
|
||||
@Mock ArticleReader mockArticleReader;
|
||||
|
||||
@Mock IArticleWriter mockArticleWriter;
|
||||
|
||||
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
replay(mockArticleReader);
|
||||
baeldungReader.readNext();
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
@Mock BaeldungReader mockBaeldungReader;
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
|
||||
expect(mockArticleWriter.write("title", "content")).andReturn(null);
|
||||
replay(mockArticleWriter);
|
||||
baeldungReader.write("title", "content");
|
||||
verify(mockArticleWriter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
|
||||
expect(mockArticleReader.next())
|
||||
.andReturn(null)
|
||||
.times(2)
|
||||
.andThrow(new NoSuchElementException());
|
||||
replay(mockArticleReader);
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
baeldungReader.readNext();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
package com.baeldung.easymock;
|
||||
|
||||
import org.easymock.EasyMockRunner;
|
||||
import org.easymock.Mock;
|
||||
import org.easymock.TestSubject;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
@RunWith(EasyMockRunner.class)
|
||||
public class BaeldungReaderAnnotatedUnitTest {
|
||||
|
||||
@Mock ArticleReader mockArticleReader;
|
||||
|
||||
@Mock IArticleWriter mockArticleWriter;
|
||||
|
||||
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
replay(mockArticleReader);
|
||||
baeldungReader.readNext();
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
@Mock BaeldungReader mockBaeldungReader;
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
|
||||
expect(mockArticleWriter.write("title", "content")).andReturn(null);
|
||||
replay(mockArticleWriter);
|
||||
baeldungReader.write("title", "content");
|
||||
verify(mockArticleWriter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
|
||||
expect(mockArticleReader.next())
|
||||
.andReturn(null)
|
||||
.times(2)
|
||||
.andThrow(new NoSuchElementException());
|
||||
replay(mockArticleReader);
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
baeldungReader.readNext();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
114
testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleUnitTest.java
Executable file → Normal file
114
testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleUnitTest.java
Executable file → Normal file
|
@ -1,57 +1,57 @@
|
|||
package com.baeldung.easymock;
|
||||
|
||||
import org.easymock.EasyMockRule;
|
||||
import org.easymock.Mock;
|
||||
import org.easymock.TestSubject;
|
||||
import org.junit.*;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
public class BaeldungReaderAnnotatedWithRuleUnitTest {
|
||||
|
||||
@Rule public EasyMockRule mockRule = new EasyMockRule(this);
|
||||
|
||||
@Mock ArticleReader mockArticleReader;
|
||||
|
||||
@Mock IArticleWriter mockArticleWriter;
|
||||
|
||||
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
replay(mockArticleReader);
|
||||
baeldungReader.readNext();
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
@Mock BaeldungReader mockBaeldungReader;
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
|
||||
expect(mockArticleWriter.write("title", "content")).andReturn(null);
|
||||
replay(mockArticleWriter);
|
||||
baeldungReader.write("title", "content");
|
||||
verify(mockArticleWriter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
|
||||
expect(mockArticleReader.next())
|
||||
.andReturn(null)
|
||||
.times(2)
|
||||
.andThrow(new NoSuchElementException());
|
||||
replay(mockArticleReader);
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
baeldungReader.readNext();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
package com.baeldung.easymock;
|
||||
|
||||
import org.easymock.EasyMockRule;
|
||||
import org.easymock.Mock;
|
||||
import org.easymock.TestSubject;
|
||||
import org.junit.*;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
public class BaeldungReaderAnnotatedWithRuleUnitTest {
|
||||
|
||||
@Rule public EasyMockRule mockRule = new EasyMockRule(this);
|
||||
|
||||
@Mock ArticleReader mockArticleReader;
|
||||
|
||||
@Mock IArticleWriter mockArticleWriter;
|
||||
|
||||
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
replay(mockArticleReader);
|
||||
baeldungReader.readNext();
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
@Mock BaeldungReader mockBaeldungReader;
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
|
||||
expect(mockArticleWriter.write("title", "content")).andReturn(null);
|
||||
replay(mockArticleWriter);
|
||||
baeldungReader.write("title", "content");
|
||||
verify(mockArticleWriter);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
|
||||
expect(mockArticleReader.next())
|
||||
.andReturn(null)
|
||||
.times(2)
|
||||
.andThrow(new NoSuchElementException());
|
||||
replay(mockArticleReader);
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
baeldungReader.readNext();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
52
testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationUnitTest.java
Executable file → Normal file
52
testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationUnitTest.java
Executable file → Normal file
|
@ -1,27 +1,27 @@
|
|||
package com.baeldung.easymock;
|
||||
|
||||
import org.easymock.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
public class BaeldungReaderMockDelegationUnitTest {
|
||||
|
||||
EasyMockSupport easyMockSupport = new EasyMockSupport();
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadAndWriteSequencially_thenWorks() {
|
||||
ArticleReader mockArticleReader = easyMockSupport.createMock(ArticleReader.class);
|
||||
IArticleWriter mockArticleWriter = easyMockSupport.createMock(IArticleWriter.class);
|
||||
BaeldungReader baeldungReader = new BaeldungReader(mockArticleReader, mockArticleWriter);
|
||||
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
expect(mockArticleWriter.write("title", "content")).andReturn("");
|
||||
easyMockSupport.replayAll();
|
||||
|
||||
baeldungReader.readNext();
|
||||
baeldungReader.write("title", "content");
|
||||
easyMockSupport.verifyAll();
|
||||
}
|
||||
|
||||
package com.baeldung.easymock;
|
||||
|
||||
import org.easymock.*;
|
||||
import org.junit.*;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
|
||||
public class BaeldungReaderMockDelegationUnitTest {
|
||||
|
||||
EasyMockSupport easyMockSupport = new EasyMockSupport();
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadAndWriteSequencially_thenWorks() {
|
||||
ArticleReader mockArticleReader = easyMockSupport.createMock(ArticleReader.class);
|
||||
IArticleWriter mockArticleWriter = easyMockSupport.createMock(IArticleWriter.class);
|
||||
BaeldungReader baeldungReader = new BaeldungReader(mockArticleReader, mockArticleWriter);
|
||||
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
expect(mockArticleWriter.write("title", "content")).andReturn("");
|
||||
easyMockSupport.replayAll();
|
||||
|
||||
baeldungReader.readNext();
|
||||
baeldungReader.write("title", "content");
|
||||
easyMockSupport.verifyAll();
|
||||
}
|
||||
|
||||
}
|
82
testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportUnitTest.java
Executable file → Normal file
82
testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportUnitTest.java
Executable file → Normal file
|
@ -1,42 +1,42 @@
|
|||
package com.baeldung.easymock;
|
||||
|
||||
import org.easymock.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(EasyMockRunner.class)
|
||||
public class BaeldungReaderMockSupportUnitTest extends EasyMockSupport {
|
||||
|
||||
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
|
||||
@Mock ArticleReader mockArticleReader;
|
||||
@Mock IArticleWriter mockArticleWriter;
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadAndWriteSequencially_thenWorks() {
|
||||
expect(mockArticleReader.next())
|
||||
.andReturn(null)
|
||||
.times(2)
|
||||
.andThrow(new NoSuchElementException());
|
||||
expect(mockArticleWriter.write("title", "content")).andReturn("BAEL-201801");
|
||||
replayAll();
|
||||
|
||||
Exception expectedException = null;
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
baeldungReader.readNext();
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
expectedException = exception;
|
||||
}
|
||||
String articleId = baeldungReader.write("title", "content");
|
||||
verifyAll();
|
||||
assertEquals(NoSuchElementException.class, expectedException.getClass());
|
||||
assertEquals("BAEL-201801", articleId);
|
||||
}
|
||||
|
||||
package com.baeldung.easymock;
|
||||
|
||||
import org.easymock.*;
|
||||
import org.junit.*;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(EasyMockRunner.class)
|
||||
public class BaeldungReaderMockSupportUnitTest extends EasyMockSupport {
|
||||
|
||||
@TestSubject BaeldungReader baeldungReader = new BaeldungReader();
|
||||
@Mock ArticleReader mockArticleReader;
|
||||
@Mock IArticleWriter mockArticleWriter;
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadAndWriteSequencially_thenWorks() {
|
||||
expect(mockArticleReader.next())
|
||||
.andReturn(null)
|
||||
.times(2)
|
||||
.andThrow(new NoSuchElementException());
|
||||
expect(mockArticleWriter.write("title", "content")).andReturn("BAEL-201801");
|
||||
replayAll();
|
||||
|
||||
Exception expectedException = null;
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
baeldungReader.readNext();
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
expectedException = exception;
|
||||
}
|
||||
String articleId = baeldungReader.write("title", "content");
|
||||
verifyAll();
|
||||
assertEquals(NoSuchElementException.class, expectedException.getClass());
|
||||
assertEquals("BAEL-201801", articleId);
|
||||
}
|
||||
|
||||
}
|
208
testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderUnitTest.java
Executable file → Normal file
208
testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderUnitTest.java
Executable file → Normal file
|
@ -1,105 +1,105 @@
|
|||
package com.baeldung.easymock;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BaeldungReaderUnitTest {
|
||||
|
||||
private BaeldungReader baeldungReader;
|
||||
|
||||
private ArticleReader mockArticleReader;
|
||||
|
||||
private IArticleWriter mockArticleWriter;
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
|
||||
mockArticleReader = mock(ArticleReader.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
replay(mockArticleReader);
|
||||
|
||||
BaeldungArticle article = baeldungReader.readNext();
|
||||
verify(mockArticleReader);
|
||||
assertEquals(null, article);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadNextAndSkimTopics_thenAllAllowed() {
|
||||
mockArticleReader = strictMock(ArticleReader.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
expect(mockArticleReader.ofTopic("easymock")).andReturn(null);
|
||||
replay(mockArticleReader);
|
||||
|
||||
baeldungReader.readNext();
|
||||
baeldungReader.readTopic("easymock");
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadNextAndOthers_thenAllowed() {
|
||||
mockArticleReader = niceMock(ArticleReader.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
replay(mockArticleReader);
|
||||
|
||||
baeldungReader.readNext();
|
||||
baeldungReader.readTopic("easymock");
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenWriteMaliciousContent_thenArgumentIllegal() {
|
||||
mockArticleWriter = mock(IArticleWriter.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleWriter);
|
||||
expect(mockArticleWriter.write("easymock", "<body onload=alert('baeldung')>")).andThrow(new IllegalArgumentException());
|
||||
replay(mockArticleWriter);
|
||||
|
||||
Exception expectedException = null;
|
||||
try {
|
||||
baeldungReader.write("easymock", "<body onload=alert('baeldung')>");
|
||||
} catch (Exception exception) {
|
||||
expectedException = exception;
|
||||
}
|
||||
|
||||
verify(mockArticleWriter);
|
||||
assertEquals(IllegalArgumentException.class, expectedException.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
|
||||
mockArticleWriter = mock(IArticleWriter.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleWriter);
|
||||
expect(mockArticleWriter.write("title", "content")).andReturn(null);
|
||||
replay(mockArticleWriter);
|
||||
String articleId = baeldungReader.write("title", "content");
|
||||
verify(mockArticleWriter);
|
||||
assertEquals(null, articleId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
|
||||
ArticleReader mockArticleReader = mock(ArticleReader.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||
expect(mockArticleReader.next())
|
||||
.andReturn(null)
|
||||
.times(2)
|
||||
.andThrow(new NoSuchElementException());
|
||||
replay(mockArticleReader);
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
baeldungReader.readNext();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
package com.baeldung.easymock;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.easymock.EasyMock.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class BaeldungReaderUnitTest {
|
||||
|
||||
private BaeldungReader baeldungReader;
|
||||
|
||||
private ArticleReader mockArticleReader;
|
||||
|
||||
private IArticleWriter mockArticleWriter;
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadNext_thenNextArticleRead() {
|
||||
mockArticleReader = mock(ArticleReader.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
replay(mockArticleReader);
|
||||
|
||||
BaeldungArticle article = baeldungReader.readNext();
|
||||
verify(mockArticleReader);
|
||||
assertEquals(null, article);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadNextAndSkimTopics_thenAllAllowed() {
|
||||
mockArticleReader = strictMock(ArticleReader.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
expect(mockArticleReader.ofTopic("easymock")).andReturn(null);
|
||||
replay(mockArticleReader);
|
||||
|
||||
baeldungReader.readNext();
|
||||
baeldungReader.readTopic("easymock");
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenReadNextAndOthers_thenAllowed() {
|
||||
mockArticleReader = niceMock(ArticleReader.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||
|
||||
expect(mockArticleReader.next()).andReturn(null);
|
||||
replay(mockArticleReader);
|
||||
|
||||
baeldungReader.readNext();
|
||||
baeldungReader.readTopic("easymock");
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenWriteMaliciousContent_thenArgumentIllegal() {
|
||||
mockArticleWriter = mock(IArticleWriter.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleWriter);
|
||||
expect(mockArticleWriter.write("easymock", "<body onload=alert('baeldung')>")).andThrow(new IllegalArgumentException());
|
||||
replay(mockArticleWriter);
|
||||
|
||||
Exception expectedException = null;
|
||||
try {
|
||||
baeldungReader.write("easymock", "<body onload=alert('baeldung')>");
|
||||
} catch (Exception exception) {
|
||||
expectedException = exception;
|
||||
}
|
||||
|
||||
verify(mockArticleWriter);
|
||||
assertEquals(IllegalArgumentException.class, expectedException.getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBaeldungReader_whenWrite_thenWriterCalled() {
|
||||
mockArticleWriter = mock(IArticleWriter.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleWriter);
|
||||
expect(mockArticleWriter.write("title", "content")).andReturn(null);
|
||||
replay(mockArticleWriter);
|
||||
String articleId = baeldungReader.write("title", "content");
|
||||
verify(mockArticleWriter);
|
||||
assertEquals(null, articleId);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArticlesInReader_whenReadTillEnd_thenThrowException() {
|
||||
ArticleReader mockArticleReader = mock(ArticleReader.class);
|
||||
baeldungReader = new BaeldungReader(mockArticleReader);
|
||||
expect(mockArticleReader.next())
|
||||
.andReturn(null)
|
||||
.times(2)
|
||||
.andThrow(new NoSuchElementException());
|
||||
replay(mockArticleReader);
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
baeldungReader.readNext();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
verify(mockArticleReader);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,8 +2,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-modules</artifactId>
|
||||
<name>testing-modules</name>
|
||||
<artifactId>testing-libraries</artifactId>
|
||||
<name>testing-libraries</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
|
@ -31,7 +31,6 @@
|
|||
<module>selenium-junit-testng</module>
|
||||
<module>spring-testing</module>
|
||||
<module>test-containers</module>
|
||||
<module>testing</module>
|
||||
<module>testng</module>
|
||||
<module>junit-5-basics</module>
|
||||
<module>easymock</module>
|
||||
|
|
|
@ -1,49 +1,49 @@
|
|||
package com.baeldung.restassured;
|
||||
|
||||
public class Odd {
|
||||
|
||||
float price;
|
||||
int status;
|
||||
float ck;
|
||||
String name;
|
||||
|
||||
Odd(float price, int status, float ck, String name) {
|
||||
this.price = price;
|
||||
this.status = status;
|
||||
this.ck = ck;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(float price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public float getCk() {
|
||||
return ck;
|
||||
}
|
||||
|
||||
public void setCk(float ck) {
|
||||
this.ck = ck;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.restassured;
|
||||
|
||||
public class Odd {
|
||||
|
||||
float price;
|
||||
int status;
|
||||
float ck;
|
||||
String name;
|
||||
|
||||
Odd(float price, int status, float ck, String name) {
|
||||
this.price = price;
|
||||
this.status = status;
|
||||
this.ck = ck;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public float getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(float price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public float getCk() {
|
||||
return ck;
|
||||
}
|
||||
|
||||
public void setCk(float ck) {
|
||||
this.ck = ck;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,23 +1,23 @@
|
|||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
public class Employee {
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String employeeToString() {
|
||||
return "id: " + getId() + "; name: " + getName();
|
||||
}
|
||||
|
||||
}
|
||||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
public class Employee {
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
private String employeeToString() {
|
||||
return "id: " + getId() + "; name: " + getName();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class EmployeeService {
|
||||
@Autowired
|
||||
private HRService hrService;
|
||||
|
||||
public String findEmployeeStatus(Integer employeeId) {
|
||||
return "Employee " + employeeId + " status: " + hrService.getEmployeeStatus(employeeId);
|
||||
}
|
||||
}
|
||||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class EmployeeService {
|
||||
@Autowired
|
||||
private HRService hrService;
|
||||
|
||||
public String findEmployeeStatus(Integer employeeId) {
|
||||
return "Employee " + employeeId + " status: " + hrService.getEmployeeStatus(employeeId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class HRService {
|
||||
|
||||
public String getEmployeeStatus(Integer employeeId) {
|
||||
return "Inactive";
|
||||
}
|
||||
}
|
||||
package org.baeldung.reflectiontestutils.repository;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class HRService {
|
||||
|
||||
public String getEmployeeStatus(Integer employeeId) {
|
||||
return "Inactive";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,46 +1,46 @@
|
|||
package org.baeldung.reflectiontestutils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.baeldung.reflectiontestutils.repository.Employee;
|
||||
import org.baeldung.reflectiontestutils.repository.EmployeeService;
|
||||
import org.baeldung.reflectiontestutils.repository.HRService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ReflectionTestUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenNonPublicField_thenReflectionTestUtilsSetField() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
assertTrue(employee.getId().equals(1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNonPublicMethod_thenReflectionTestUtilsInvokeMethod() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
employee.setName("Smith, John");
|
||||
assertTrue(ReflectionTestUtils.invokeMethod(employee, "employeeToString").equals("id: 1; name: Smith, John"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInjectingMockOfDependency_thenReflectionTestUtilsSetField() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
employee.setName("Smith, John");
|
||||
|
||||
HRService hrService = mock(HRService.class);
|
||||
when(hrService.getEmployeeStatus(employee.getId())).thenReturn("Active");
|
||||
EmployeeService employeeService = new EmployeeService();
|
||||
|
||||
// Inject mock into the private field
|
||||
ReflectionTestUtils.setField(employeeService, "hrService", hrService);
|
||||
assertEquals("Employee " + employee.getId() + " status: Active", employeeService.findEmployeeStatus(employee.getId()));
|
||||
}
|
||||
}
|
||||
package org.baeldung.reflectiontestutils;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.baeldung.reflectiontestutils.repository.Employee;
|
||||
import org.baeldung.reflectiontestutils.repository.EmployeeService;
|
||||
import org.baeldung.reflectiontestutils.repository.HRService;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ReflectionTestUtilsUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenNonPublicField_thenReflectionTestUtilsSetField() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
assertTrue(employee.getId().equals(1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNonPublicMethod_thenReflectionTestUtilsInvokeMethod() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
employee.setName("Smith, John");
|
||||
assertTrue(ReflectionTestUtils.invokeMethod(employee, "employeeToString").equals("id: 1; name: Smith, John"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInjectingMockOfDependency_thenReflectionTestUtilsSetField() {
|
||||
Employee employee = new Employee();
|
||||
ReflectionTestUtils.setField(employee, "id", 1);
|
||||
employee.setName("Smith, John");
|
||||
|
||||
HRService hrService = mock(HRService.class);
|
||||
when(hrService.getEmployeeStatus(employee.getId())).thenReturn("Active");
|
||||
EmployeeService employeeService = new EmployeeService();
|
||||
|
||||
// Inject mock into the private field
|
||||
ReflectionTestUtils.setField(employeeService, "hrService", hrService);
|
||||
assertEquals("Employee " + employee.getId() + " status: Active", employeeService.findEmployeeStatus(employee.getId()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,24 +0,0 @@
|
|||
=========
|
||||
|
||||
## Mutation Testing
|
||||
|
||||
### Relevant Articles:
|
||||
- [Mutation Testing with PITest](http://www.baeldung.com/java-mutation-testing-with-pitest)
|
||||
- [Intro to JaCoCo](http://www.baeldung.com/jacoco)
|
||||
- [AssertJ’s Java 8 Features](http://www.baeldung.com/assertJ-java-8-features)
|
||||
- [AssertJ for Guava](http://www.baeldung.com/assertJ-for-guava)
|
||||
- [Introduction to AssertJ](http://www.baeldung.com/introduction-to-assertj)
|
||||
- [Cucumber and Scenario Outline](http://www.baeldung.com/cucumber-scenario-outline)
|
||||
- [Testing with Google Truth](http://www.baeldung.com/google-truth)
|
||||
- [Testing with JGoTesting](http://www.baeldung.com/jgotesting)
|
||||
- [Introduction to JUnitParams](http://www.baeldung.com/junit-params)
|
||||
- [Cucumber Java 8 Support](http://www.baeldung.com/cucumber-java-8-support)
|
||||
- [Introduction to Lambda Behave](http://www.baeldung.com/lambda-behave)
|
||||
- [Introduction to Jukito](http://www.baeldung.com/jukito)
|
||||
- [Custom JUnit 4 Test Runners](http://www.baeldung.com/junit-4-custom-runners)
|
||||
- [Guide to JSpec](http://www.baeldung.com/jspec)
|
||||
- [Custom Assertions with AssertJ](http://www.baeldung.com/assertj-custom-assertion)
|
||||
- [Using Conditions with AssertJ Assertions](http://www.baeldung.com/assertj-conditions)
|
||||
- [A Guide to JavaFaker](https://www.baeldung.com/java-faker)
|
||||
- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java)
|
||||
- [AssertJ Exception Assertions](http://www.baeldung.com/assertj-exception-assertion)
|
|
@ -1,183 +0,0 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>testing</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>testing</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.insightfullogic</groupId>
|
||||
<artifactId>lambda-behave</artifactId>
|
||||
<version>${lambda-behave.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-guava</artifactId>
|
||||
<version>${assertj-guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</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>
|
||||
<dependency>
|
||||
<groupId>org.pitest</groupId>
|
||||
<artifactId>pitest-parent</artifactId>
|
||||
<version>${pitest.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.truth</groupId>
|
||||
<artifactId>truth</artifactId>
|
||||
<version>${truth.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.truth.extensions</groupId>
|
||||
<artifactId>truth-java8-extension</artifactId>
|
||||
<version>${truth.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>pl.pragmatists</groupId>
|
||||
<artifactId>JUnitParams</artifactId>
|
||||
<version>${jUnitParams.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jgotesting</groupId>
|
||||
<artifactId>jgotesting</artifactId>
|
||||
<version>${jgotesting.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jukito</groupId>
|
||||
<artifactId>jukito</artifactId>
|
||||
<version>${jukito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>javalite-common</artifactId>
|
||||
<version>${javalite.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.javafaker</groupId>
|
||||
<artifactId>javafaker</artifactId>
|
||||
<version>${javafaker.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.pitest</groupId>
|
||||
<artifactId>pitest-maven</artifactId>
|
||||
<version>${pitest.version}</version>
|
||||
<configuration>
|
||||
<targetClasses>
|
||||
<param>com.baeldung.testing.mutation.*</param>
|
||||
</targetClasses>
|
||||
<targetTests>
|
||||
<param>com.baeldung.mutation.test.*</param>
|
||||
</targetTests>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>${jacoco.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>prepare-agent</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>report</id>
|
||||
<phase>prepare-package</phase>
|
||||
<goals>
|
||||
<goal>report</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>jacoco-check</id>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<rules>
|
||||
<rule>
|
||||
<element>PACKAGE</element>
|
||||
<limits>
|
||||
<limit>
|
||||
<counter>LINE</counter>
|
||||
<value>COVEREDRATIO</value>
|
||||
<minimum>0</minimum>
|
||||
</limit>
|
||||
</limits>
|
||||
</rule>
|
||||
</rules>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-assertions-generator-maven-plugin</artifactId>
|
||||
<version>${assertj-generator.version}</version>
|
||||
<configuration>
|
||||
<classes>
|
||||
<param>com.baeldung.testing.assertj.custom.Person</param>
|
||||
</classes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<cucumber.version>1.2.5</cucumber.version>
|
||||
<pitest.version>1.1.10</pitest.version>
|
||||
<jacoco.version>0.7.7.201606060606</jacoco.version>
|
||||
<guava.version>21.0</guava.version>
|
||||
<assertj-guava.version>3.1.0</assertj-guava.version>
|
||||
<assertj-core.version>3.9.0</assertj-core.version>
|
||||
<assertj-generator.version>2.1.0</assertj-generator.version>
|
||||
<truth.version>0.32</truth.version>
|
||||
<jUnitParams.version>1.1.0</jUnitParams.version>
|
||||
<jgotesting.version>0.12</jgotesting.version>
|
||||
<javalite.version>1.4.13</javalite.version>
|
||||
<lambda-behave.version>0.4</lambda-behave.version>
|
||||
<assertj-guava.version>3.0.0</assertj-guava.version>
|
||||
<jukito.version>1.5</jukito.version>
|
||||
<javafaker.version>0.15</javafaker.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,13 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,48 +1,48 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>testng</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>testng</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>${testng.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>testng</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<testResources>
|
||||
<testResource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</testResource>
|
||||
</testResources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<testng.version>6.10</testng.version>
|
||||
</properties>
|
||||
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
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>testng</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>testng</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<version>${testng.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>testng</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<testResources>
|
||||
<testResource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</testResource>
|
||||
</testResources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<!-- testing -->
|
||||
<testng.version>6.10</testng.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -1,67 +1,67 @@
|
|||
package com.baeldung.reports;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.ITestContext;
|
||||
import org.testng.ITestListener;
|
||||
import org.testng.ITestResult;
|
||||
|
||||
public class CustomisedListener implements ITestListener {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger("CUSTOM_LOGS");
|
||||
|
||||
@Override
|
||||
public void onFinish(ITestContext context) {
|
||||
LOGGER.info("PASSED TEST CASES");
|
||||
context.getPassedTests()
|
||||
.getAllResults()
|
||||
.forEach(result -> {
|
||||
LOGGER.info(result.getName());
|
||||
});
|
||||
LOGGER.info("FAILED TEST CASES");
|
||||
context.getFailedTests()
|
||||
.getAllResults()
|
||||
.forEach(result -> {
|
||||
LOGGER.info(result.getName());
|
||||
});
|
||||
LOGGER.info("Test completed on: " + context.getEndDate()
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(ITestContext arg0) {
|
||||
LOGGER.info("Started testing on: " + arg0.getStartDate()
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestFailure(ITestResult arg0) {
|
||||
LOGGER.info("Failed : " + arg0.getName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestSkipped(ITestResult arg0) {
|
||||
LOGGER.info("Skipped Test: " + arg0.getName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestStart(ITestResult arg0) {
|
||||
LOGGER.info("Testing: " + arg0.getName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestSuccess(ITestResult arg0) {
|
||||
long timeTaken = ((arg0.getEndMillis() - arg0.getStartMillis()));
|
||||
LOGGER.info("Tested: " + arg0.getName() + " Time taken:" + timeTaken + " ms");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.reports;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.ITestContext;
|
||||
import org.testng.ITestListener;
|
||||
import org.testng.ITestResult;
|
||||
|
||||
public class CustomisedListener implements ITestListener {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger("CUSTOM_LOGS");
|
||||
|
||||
@Override
|
||||
public void onFinish(ITestContext context) {
|
||||
LOGGER.info("PASSED TEST CASES");
|
||||
context.getPassedTests()
|
||||
.getAllResults()
|
||||
.forEach(result -> {
|
||||
LOGGER.info(result.getName());
|
||||
});
|
||||
LOGGER.info("FAILED TEST CASES");
|
||||
context.getFailedTests()
|
||||
.getAllResults()
|
||||
.forEach(result -> {
|
||||
LOGGER.info(result.getName());
|
||||
});
|
||||
LOGGER.info("Test completed on: " + context.getEndDate()
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(ITestContext arg0) {
|
||||
LOGGER.info("Started testing on: " + arg0.getStartDate()
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestFailure(ITestResult arg0) {
|
||||
LOGGER.info("Failed : " + arg0.getName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestSkipped(ITestResult arg0) {
|
||||
LOGGER.info("Skipped Test: " + arg0.getName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestStart(ITestResult arg0) {
|
||||
LOGGER.info("Testing: " + arg0.getName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTestSuccess(ITestResult arg0) {
|
||||
long timeTaken = ((arg0.getEndMillis() - arg0.getStartMillis()));
|
||||
LOGGER.info("Tested: " + arg0.getName() + " Time taken:" + timeTaken + " ms");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,111 +1,111 @@
|
|||
package com.baeldung.reports;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.*;
|
||||
import org.testng.xml.XmlSuite;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class CustomisedReports implements IReporter {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class);
|
||||
|
||||
private static final String ROW_TEMPLATE = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
|
||||
|
||||
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
|
||||
String reportTemplate = initReportTemplate();
|
||||
|
||||
final String body = suites
|
||||
.stream()
|
||||
.flatMap(suiteToResults())
|
||||
.collect(Collectors.joining());
|
||||
|
||||
saveReportTemplate(outputDirectory, reportTemplate.replaceFirst("</tbody>", String.format("%s</tbody>", body)));
|
||||
}
|
||||
|
||||
private Function<ISuite, Stream<? extends String>> suiteToResults() {
|
||||
return suite -> suite.getResults().entrySet()
|
||||
.stream()
|
||||
.flatMap(resultsToRows(suite));
|
||||
}
|
||||
|
||||
private Function<Map.Entry<String, ISuiteResult>, Stream<? extends String>> resultsToRows(ISuite suite) {
|
||||
return e -> {
|
||||
ITestContext testContext = e.getValue().getTestContext();
|
||||
|
||||
Set<ITestResult> failedTests = testContext
|
||||
.getFailedTests()
|
||||
.getAllResults();
|
||||
Set<ITestResult> passedTests = testContext
|
||||
.getPassedTests()
|
||||
.getAllResults();
|
||||
Set<ITestResult> skippedTests = testContext
|
||||
.getSkippedTests()
|
||||
.getAllResults();
|
||||
|
||||
String suiteName = suite.getName();
|
||||
|
||||
return Stream
|
||||
.of(failedTests, passedTests, skippedTests)
|
||||
.flatMap(results -> generateReportRows(e.getKey(), suiteName, results).stream());
|
||||
};
|
||||
}
|
||||
|
||||
private List<String> generateReportRows(String testName, String suiteName, Set<ITestResult> allTestResults) {
|
||||
return allTestResults.stream()
|
||||
.map(testResultToResultRow(testName, suiteName))
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
private Function<ITestResult, String> testResultToResultRow(String testName, String suiteName) {
|
||||
return testResult -> {
|
||||
switch (testResult.getStatus()) {
|
||||
case ITestResult.FAILURE:
|
||||
return String.format(ROW_TEMPLATE, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA");
|
||||
|
||||
case ITestResult.SUCCESS:
|
||||
return String.format(ROW_TEMPLATE, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()));
|
||||
|
||||
case ITestResult.SKIP:
|
||||
return String.format(ROW_TEMPLATE, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA");
|
||||
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private String initReportTemplate() {
|
||||
String template = null;
|
||||
byte[] reportTemplate;
|
||||
try {
|
||||
reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html"));
|
||||
template = new String(reportTemplate, "UTF-8");
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Problem initializing template", e);
|
||||
}
|
||||
return template;
|
||||
}
|
||||
|
||||
private void saveReportTemplate(String outputDirectory, String reportTemplate) {
|
||||
new File(outputDirectory).mkdirs();
|
||||
try {
|
||||
PrintWriter reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html"))));
|
||||
reportWriter.println(reportTemplate);
|
||||
reportWriter.flush();
|
||||
reportWriter.close();
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Problem saving template", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.baeldung.reports;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.*;
|
||||
import org.testng.xml.XmlSuite;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public class CustomisedReports implements IReporter {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class);
|
||||
|
||||
private static final String ROW_TEMPLATE = "<tr class=\"%s\"><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
|
||||
|
||||
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
|
||||
String reportTemplate = initReportTemplate();
|
||||
|
||||
final String body = suites
|
||||
.stream()
|
||||
.flatMap(suiteToResults())
|
||||
.collect(Collectors.joining());
|
||||
|
||||
saveReportTemplate(outputDirectory, reportTemplate.replaceFirst("</tbody>", String.format("%s</tbody>", body)));
|
||||
}
|
||||
|
||||
private Function<ISuite, Stream<? extends String>> suiteToResults() {
|
||||
return suite -> suite.getResults().entrySet()
|
||||
.stream()
|
||||
.flatMap(resultsToRows(suite));
|
||||
}
|
||||
|
||||
private Function<Map.Entry<String, ISuiteResult>, Stream<? extends String>> resultsToRows(ISuite suite) {
|
||||
return e -> {
|
||||
ITestContext testContext = e.getValue().getTestContext();
|
||||
|
||||
Set<ITestResult> failedTests = testContext
|
||||
.getFailedTests()
|
||||
.getAllResults();
|
||||
Set<ITestResult> passedTests = testContext
|
||||
.getPassedTests()
|
||||
.getAllResults();
|
||||
Set<ITestResult> skippedTests = testContext
|
||||
.getSkippedTests()
|
||||
.getAllResults();
|
||||
|
||||
String suiteName = suite.getName();
|
||||
|
||||
return Stream
|
||||
.of(failedTests, passedTests, skippedTests)
|
||||
.flatMap(results -> generateReportRows(e.getKey(), suiteName, results).stream());
|
||||
};
|
||||
}
|
||||
|
||||
private List<String> generateReportRows(String testName, String suiteName, Set<ITestResult> allTestResults) {
|
||||
return allTestResults.stream()
|
||||
.map(testResultToResultRow(testName, suiteName))
|
||||
.collect(toList());
|
||||
}
|
||||
|
||||
private Function<ITestResult, String> testResultToResultRow(String testName, String suiteName) {
|
||||
return testResult -> {
|
||||
switch (testResult.getStatus()) {
|
||||
case ITestResult.FAILURE:
|
||||
return String.format(ROW_TEMPLATE, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA");
|
||||
|
||||
case ITestResult.SUCCESS:
|
||||
return String.format(ROW_TEMPLATE, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis()));
|
||||
|
||||
case ITestResult.SKIP:
|
||||
return String.format(ROW_TEMPLATE, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA");
|
||||
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private String initReportTemplate() {
|
||||
String template = null;
|
||||
byte[] reportTemplate;
|
||||
try {
|
||||
reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html"));
|
||||
template = new String(reportTemplate, "UTF-8");
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Problem initializing template", e);
|
||||
}
|
||||
return template;
|
||||
}
|
||||
|
||||
private void saveReportTemplate(String outputDirectory, String reportTemplate) {
|
||||
new File(outputDirectory).mkdirs();
|
||||
try {
|
||||
PrintWriter reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html"))));
|
||||
reportWriter.println(reportTemplate);
|
||||
reportWriter.flush();
|
||||
reportWriter.close();
|
||||
} catch (IOException e) {
|
||||
LOGGER.error("Problem saving template", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
|
@ -1,13 +1,13 @@
|
|||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||
<suite name="My test suite">
|
||||
<listeners>
|
||||
<listener class-name="com.baeldung.reports.CustomisedListener"></listener>
|
||||
</listeners>
|
||||
<test name="numbersXML">
|
||||
<parameter name="value" value="1"/>
|
||||
<parameter name="isEven" value="false"/>
|
||||
<classes>
|
||||
<class name="com.baeldung.ParametrizedLongRunningUnitTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||
<suite name="My test suite">
|
||||
<listeners>
|
||||
<listener class-name="com.baeldung.reports.CustomisedListener"></listener>
|
||||
</listeners>
|
||||
<test name="numbersXML">
|
||||
<parameter name="value" value="1"/>
|
||||
<parameter name="isEven" value="false"/>
|
||||
<classes>
|
||||
<class name="com.baeldung.ParametrizedLongRunningUnitTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
|
@ -1,13 +1,13 @@
|
|||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||
<suite name="regression_test">
|
||||
<test name="test groups">
|
||||
<groups>
|
||||
<run>
|
||||
<include name="regression"/>
|
||||
</run>
|
||||
</groups>
|
||||
<classes>
|
||||
<class name="com.baeldung.SummationServiceIntegrationTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||
<suite name="regression_test">
|
||||
<test name="test groups">
|
||||
<groups>
|
||||
<run>
|
||||
<include name="regression"/>
|
||||
</run>
|
||||
</groups>
|
||||
<classes>
|
||||
<class name="com.baeldung.SummationServiceIntegrationTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
|
@ -1,17 +1,17 @@
|
|||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||
<suite name="regression_test">
|
||||
<test name="test setup">
|
||||
<groups>
|
||||
<run>
|
||||
<include name="regression"/>
|
||||
</run>
|
||||
</groups>
|
||||
<classes>
|
||||
<class name="com.baeldung.SummationServiceIntegrationTest">
|
||||
<methods>
|
||||
<include name="givenNumbers_sumEquals_thenCorrect"/>
|
||||
</methods>
|
||||
</class>
|
||||
</classes>
|
||||
</test>
|
||||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||
<suite name="regression_test">
|
||||
<test name="test setup">
|
||||
<groups>
|
||||
<run>
|
||||
<include name="regression"/>
|
||||
</run>
|
||||
</groups>
|
||||
<classes>
|
||||
<class name="com.baeldung.SummationServiceIntegrationTest">
|
||||
<methods>
|
||||
<include name="givenNumbers_sumEquals_thenCorrect"/>
|
||||
</methods>
|
||||
</class>
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
|
@ -1,13 +1,13 @@
|
|||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||
<suite name="suite" parallel="classes" thread-count="2">
|
||||
<listeners>
|
||||
<listener class-name="com.baeldung.reports.CustomisedReports" />
|
||||
</listeners>
|
||||
<test name="test suite">
|
||||
<classes>
|
||||
<class name="com.baeldung.RegistrationLongRunningUnitTest" />
|
||||
<class name="com.baeldung.SignInLongRunningUnitTest" />
|
||||
<class name="com.baeldung.SimpleLongRunningUnitTest" />
|
||||
</classes>
|
||||
</test>
|
||||
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
|
||||
<suite name="suite" parallel="classes" thread-count="2">
|
||||
<listeners>
|
||||
<listener class-name="com.baeldung.reports.CustomisedReports" />
|
||||
</listeners>
|
||||
<test name="test suite">
|
||||
<classes>
|
||||
<class name="com.baeldung.RegistrationLongRunningUnitTest" />
|
||||
<class name="com.baeldung.SignInLongRunningUnitTest" />
|
||||
<class name="com.baeldung.SimpleLongRunningUnitTest" />
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
Loading…
Reference in New Issue