Skip to content
This repository has been archived by the owner on Aug 30, 2023. It is now read-only.

Commit

Permalink
Add JUnit5 support
Browse files Browse the repository at this point in the history
JUnit5 support works in the same way as for other xUnit engines.

Also, all related documentation is updated.

#160
  • Loading branch information
extsoft committed Oct 31, 2019
1 parent d8cb36e commit 7ed60c8
Show file tree
Hide file tree
Showing 15 changed files with 443 additions and 19 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Sunshine
Sunshine allows you to manage suits of your automated tests directly from Java code. It can work
on top of [TestNg](https://testng.org/doc/index.html) or [JUnit4](https://junit.org/junit4/).
Sunshine allows you to manage suits of your automated tests directly from Java code. It supports
[TestNg](https://testng.org/doc/index.html), [JUnit 4](https://junit.org/junit4/), and
[JUnit 5](https://junit.org/junit5/).

Please read the users documentation on [http://sunshine.tatools.org](http://sunshine.tatools.org).

Expand Down
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ project(':sunshine-testng-integration-tests') {
}
}

project(':sunshine-junit5') {
apply from: rootProject.file('gradle/jacoco.gradle')
apply from: rootProject.file('gradle/bintray.gradle')
dependencies {
compile project(':sunshine-core')
}
}

defaultTasks 'clean', 'ready'

//task to write the version parameter given via command line into the "gradle.properties" files.
Expand Down
6 changes: 4 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ Welcome to Sunshine's documentation!
====================================
.. _TestNG: https://testng.org/doc/index.html
.. _JUnit4: https://junit.org/junit4/
.. _JUnit5: https://junit.org/junit5/

Sunshine allows you to manage suits of your automated tests directly from Java code. It can work
on top of TestNG_ or JUnit4_.

Sunshine allows you to manage suits of your automated tests directly from Java code. It supports
TestNG_, JUnit4_, and JUnit5_.

.. toctree::
:maxdepth: 3
Expand Down
33 changes: 25 additions & 8 deletions docs/md/artifacts.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,34 @@
# Artifacts

Sunshine consists of 3 libraries
Sunshine consists of 4 libraries
- `sunshine-core` provides interfaces and common implementations
- `sunshine-junit4` wraps JUnit4 to allow the creation of [entry points](entry-points.md)
- `sunshine-junit5` wraps JUnit5 to allow the creation of [entry points](entry-points.md)
- `sunshine-testng` wraps TestNG to allow the creation of [entry points](entry-points.md)

Artifact|`sunshine-testng`|`sunshine-junit4`|`sunshine-core`
---|---|---|---
Library|[![](https://img.shields.io/maven-central/v/org.tatools/sunshine-testng.svg)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-testng%22)|[![](https://img.shields.io/maven-central/v/org.tatools/sunshine-junit4.svg)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-junit4%22)|[![](https://img.shields.io/maven-central/v/org.tatools/sunshine-core.svg)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-core%22)
Javadoc|[![](https://www.javadoc.io/badge/org.tatools/sunshine-testng.svg)](https://www.javadoc.io/doc/org.tatools/sunshine-testng)|[![](https://www.javadoc.io/badge/org.tatools/sunshine-junit4.svg)](https://www.javadoc.io/doc/org.tatools/sunshine-junit4)|[![](https://www.javadoc.io/badge/org.tatools/sunshine-core.svg)](https://www.javadoc.io/doc/org.tatools/sunshine-core)
Since `0.4.x` version Sunshine uses `org.tatools` group ID. If you need the earlier version
please use `io.github.tatools` instead.

## `sunshine-core`
Artifacts:
[![](https://img.shields.io/maven-central/v/org.tatools/sunshine-core.svg)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-core%22)
[![](https://www.javadoc.io/badge/org.tatools/sunshine-core.svg)](https://www.javadoc.io/doc/org.tatools/sunshine-core)

`sunshine-junit4` was tested on `4.11` version of JUnit4 and `sunshine-testng` on `6.11` version of TestNG.
## `sunshine-junit4`
The library is tested on JUnit4 `4.11`.

Since `0.4.x` version Sunshine uses `org.tatools` group ID. If you need the earlier version
please use `io.github.tatools` instead.
Artifacts:
[![](https://img.shields.io/maven-central/v/org.tatools/sunshine-junit4.svg)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-junit4%22)
[![](https://www.javadoc.io/badge/org.tatools/sunshine-junit4.svg)](https://www.javadoc.io/doc/org.tatools/sunshine-junit4)

## `sunshine-junit5`
Artifacts:
[![](https://img.shields.io/maven-central/v/org.tatools/sunshine-junit5.svg)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-junit5%22)
[![](https://www.javadoc.io/badge/org.tatools/sunshine-junit5.svg)](https://www.javadoc.io/doc/org.tatools/sunshine-junit5)

## `sunshine-testng`
The library is tested on TestNG `6.11`.

Artifacts:
[![](https://img.shields.io/maven-central/v/org.tatools/sunshine-testng.svg)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-testng%22)
[![](https://www.javadoc.io/badge/org.tatools/sunshine-testng.svg)](https://www.javadoc.io/doc/org.tatools/sunshine-testng)
6 changes: 6 additions & 0 deletions docs/md/entry-points.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ Class: [`org.tatools.sunshine.junit4.Sunshine`](https://github.com/tatools/sunsh
The class exposes the same behavior as default TestNG entry point exposes except the last option. All tests will be
executed using JUnit4.

## Default JUnit5 entry point
Class: [`org.tatools.sunshine.junit5.Sunshine`](https://github.com/tatools/sunshine/blob/master/sunshine-junit5/src/main/java/org/tatools/sunshine/junit5/Sunshine.java)

The class exposes the same behavior as default TestNG entry point exposes except the last option. All tests will be
executed using JUnit5.

## Code snippets for different use cases
### Specify regex
There is a regex which has used to filter classes from current classpath.
Expand Down
13 changes: 6 additions & 7 deletions docs/md/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ According to [Sunshine's concept](concept.md), the code and automated tests have
So, please move everything to `src/main` except unit tests.

## Step 2: Add Sunshine library
Sunshine's library has to be selected based on a test runner is used by the project.
Please refer to
[![](https://img.shields.io/maven-central/v/org.tatools/sunshine-testng.svg?label=sunshine-testng)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-testng%22)
if you use TestNG or
[![](https://img.shields.io/maven-central/v/org.tatools/sunshine-junit4.svg?label=sunshine-junit4)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-junit4%22)
for JUnit4.
Based on the xUnit engine you are using, please select an appropriate library and add to your build configuration.
Available libraries:
- [![](https://img.shields.io/maven-central/v/org.tatools/sunshine-testng.svg?label=sunshine-testng)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-testng%22)
- [![](https://img.shields.io/maven-central/v/org.tatools/sunshine-junit4.svg?label=sunshine-junit4)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-junit4%22)
- [![](https://img.shields.io/maven-central/v/org.tatools/sunshine-junit5.svg?label=sunshine-junit5)](https://search.maven.org/search?q=g:%22org.tatools%22%20AND%20a:%22sunshine-junit5%22)

To find out more references please visit [artifacts page](artifacts.md).
To find out more references, please visit [artifacts page](artifacts.md).

## Step 3: Configure code packaging
The recommended packaging is an `uber-JAR` - also known as a `fat JAR` or `JAR with dependencies` -
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ rootProject.name = 'sunshine'
include 'sunshine-core-deps', 'sunshine-core'
include 'sunshine-testng', 'sunshine-testng-integration-tests'
include 'sunshine-junit4', 'sunshine-junit4-integration-tests'
include 'sunshine-junit5'
15 changes: 15 additions & 0 deletions sunshine-junit5/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
dependencies {
compileOnly 'org.projectlombok:lombok:1.16.14'
annotationProcessor 'org.projectlombok:lombok:1.16.14'
testCompileOnly 'org.projectlombok:lombok:1.16.14'
testAnnotationProcessor 'org.projectlombok:lombok:1.16.14'
compile 'org.junit.jupiter:junit-jupiter:5.5.2'
compile 'org.junit.platform:junit-platform-launcher:1.5.2'
testCompile 'org.hamcrest:hamcrest-all:1.3'
}

task ready(dependsOn: check) {
doLast {
println("Unit testing of Sunshine Junit5 is completed.")
}
}
2 changes: 2 additions & 0 deletions sunshine-junit5/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NAME="Sunshine JUnit5 adapter"
DESCRIPTION="The package is Sunshine's adapter for JUnit5 tests runner."
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.tatools.sunshine.junit5;

import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.tatools.sunshine.core.*;

/**
* The class provides a {@link Kernel} implementation of JUnit 5 runner.
*
* @author Dmytro Serdiuk
* @version $Id$
*/
public class Junit5Kernel implements Kernel<TestExecutionListener> {

private final Launcher launcher;
private final SunshineSuite tests;
private final SummaryGeneratingListener reporter;

/**
* Initializes a newly created {@link Junit5Kernel} object so that it represents
* an JUnit4 runner.
*
* @param sunshineSuite the suite with desired tests
*/
public Junit5Kernel(SunshineSuite sunshineSuite) {
this(LauncherFactory.create(), sunshineSuite);
}

/**
* Initializes a newly created {@link Junit5Kernel} object so that it represents
* an JUnit4 runner.
*
* @param launcher the launcher for a given test suite
* @param sunshineSuite the suite with desired tests
*/
private Junit5Kernel(Launcher launcher, SunshineSuite sunshineSuite) {
this.tests = sunshineSuite;
this.launcher = launcher;
this.reporter = new SummaryGeneratingListener();
this.launcher.registerTestExecutionListeners(this.reporter);
}

/**
* Returns a status of JUnite 5 tests execution.
*
* @return the status for the current execution
* @throws KernelException if any error occurs during JUnit tests execution
*/
@Override
public final Status status() throws KernelException {
try {
launcher.execute(
LauncherDiscoveryRequestBuilder.request()
.selectors(
tests.tests().stream().map(
sunshineTest -> DiscoverySelectors.selectClass(sunshineTest.toString())
).toArray(DiscoverySelector[]::new)
).build()
);
return new Junit5Status(this.reporter.getSummary());
} catch (SuiteException e) {
throw new KernelException("Some problem occurs in the Junit5Kernel", e);
}
}

/**
* Returns a new instance of the JUnit 5 kernel with provided listeners based
* on the current instance configuration.
*
* @param testExecutionListeners at least one desired listener
* @return the new instance of the JUnit 5 kernel
*/
@Override
public final Kernel<TestExecutionListener> with(TestExecutionListener... testExecutionListeners) {
final Launcher fork = LauncherFactory.create();
fork.registerTestExecutionListeners(testExecutionListeners);
return new Junit5Kernel(fork, this.tests);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.tatools.sunshine.junit5;

import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
import org.junit.platform.launcher.listeners.TestExecutionSummary;
import org.tatools.sunshine.core.Status;

/**
* The class provides an implementation of the {@link Status} for JUnit 5 execution.
*
* @author Dmytro Serdiuk
* @version $Id$
*/
public class Junit5Status implements Status {
private final TestExecutionSummary summary;
private final short passed = 0;
private final short failed = 1;

/**
* Initializes a newly created instance to represent a status of
* JUnit 5 execution.
*
* @param testExecutionSummary the report provided by {@link SummaryGeneratingListener}
*/
public Junit5Status(TestExecutionSummary testExecutionSummary) {
this.summary = testExecutionSummary;
}

@Override
public final short code() {
return this.summary.getTotalFailureCount() == 0 ? this.passed : this.failed;
}

@Override
public final int runCount() {
return Math.toIntExact(this.summary.getTestsFoundCount());
}

@Override
public final int failureCount() {
return Math.toIntExact(this.summary.getTestsFailedCount());
}

@Override
public final int ignoreCount() {
return Math.toIntExact(this.summary.getTestsSkippedCount());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.tatools.sunshine.junit5;


import org.tatools.sunshine.core.*;

/**
* The {@link Sunshine} class is a main class to run JUnit 5 tests.
*
* @author Dmytro Serdiuk
* @version $Id$
*/
public final class Sunshine {

public static void main(String[] args) {
new Sun(
new Junit5Kernel(
new SunshineSuitePrintable(
new SunshineSuiteFilterable(
new SuiteFromFileSystem(
new FileSystemOfClasspathClasses()
),
new VerboseRegex(new RegexCondition())
)
)
)
).shine();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* The main package of the integration with JUnit 5.
*
* @author Dmytro Serdiuk
* @version $Id$
*/
package org.tatools.sunshine.junit5;
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.tatools.sunshine.junit5;

import lombok.EqualsAndHashCode;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.tatools.sunshine.core.KernelException;

import java.util.ArrayList;


/**
* @author Dmytro Serdiuk
* @version $Id$
*/
public class Junit5KernelTest {

@Test
public void run() throws KernelException {
MatcherAssert.assertThat(
new Junit5Kernel(ArrayList::new).status().code(),
Matchers.equalTo((short) 0)
);
}


@Test
public void with() throws KernelException {
final Listener l1 = new Listener();
final Listener l2 = new Listener();
new Junit5Kernel(ArrayList::new).with(l1).with(l2).status();
MatcherAssert.assertThat(l1, Matchers.not(Matchers.equalTo(l2)));
}

@EqualsAndHashCode
private final class Listener implements TestExecutionListener {
private int call = 0;

@Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
this.call = 1;
}
}
}
Loading

0 comments on commit 7ed60c8

Please sign in to comment.