Skip to content

Commit

Permalink
- TestListeners Registration can be performed using the Launcher API
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Bechtold authored and marcphilipp committed Oct 28, 2015
1 parent 36f0d62 commit 55a73e4
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
@@ -1,19 +1,21 @@
package org.junit.gen5.engine;

import java.util.ServiceLoader;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Consumer;

/**
* @author Stefan Bechtold
* @since 5.0
*/
public class TestListenerRegistry {
private static Iterable<TestListener> testListeners;
private static List<TestListener> testListeners = new LinkedList<>();

public static void registerListener(TestListener testListener) {
testListeners.add(testListener);
}

public static Iterable<TestListener> lookupAllTestListeners() {
if (testListeners == null) {
testListeners = ServiceLoader.load(TestListener.class);
}
return testListeners;
}

Expand Down
Expand Up @@ -2,6 +2,7 @@

import org.junit.gen5.engine.TestEngine;
import org.junit.gen5.engine.TestListener;
import org.junit.gen5.engine.TestListenerRegistry;
import org.junit.gen5.engine.TestPlanConfiguration;

import java.util.ServiceLoader;
Expand All @@ -12,6 +13,10 @@
public class Launcher {
private volatile ServiceLoader<TestEngine> testEngines;

public void registerTestListener(TestListener testListener) {
TestListenerRegistry.registerListener(testListener);
}

public TestPlan createTestPlanWithConfiguration(TestPlanConfiguration configuration) {
TestPlan testPlan = new TestPlan();
for (TestEngine testEngine : lookupAllTestEngines()) {
Expand Down
1 change: 1 addition & 0 deletions sample-project/build.gradle
@@ -1,6 +1,7 @@
dependencies {
testCompile(project(':sample-extension'))
testCompile(project(':junit-launcher'))
testCompile(project(':junit-console'))
testCompile(project(':junit5-api'))
testRuntime(project(':junit5-engine'))
}
5 changes: 5 additions & 0 deletions sample-project/src/test/java/com/example/Console.java
@@ -1,5 +1,7 @@
package com.example;

import org.junit.gen5.console.ColoredPrintingTestListener;
import org.junit.gen5.console.TestSummaryReportingTestListener;
import org.junit.gen5.engine.TestPlanConfiguration;
import org.junit.gen5.launcher.Launcher;
import org.junit.gen5.launcher.TestPlan;
Expand All @@ -9,6 +11,9 @@ public static void main(String[] args) throws Throwable {
// TODO Configure launcher?
Launcher launcher = new Launcher();

launcher.registerTestListener(new ColoredPrintingTestListener(System.out));
launcher.registerTestListener(new TestSummaryReportingTestListener(System.out));

TestPlanConfiguration testPlanConfiguration =
TestPlanConfiguration.builder()
.classNames(args)
Expand Down
17 changes: 13 additions & 4 deletions sample-project/src/test/java/com/example/SampleTestCase.java
@@ -1,21 +1,30 @@
package com.example;

import org.junit.gen5.api.Test;
import org.opentestalliance.TestAbortedException;
import org.opentestalliance.TestSkippedException;

/**
* Named *TestCase so Gradle will not try to run it.
*/
class SampleTestCase {
@Test
void skippingTest() {
throw new TestSkippedException("This test will be skipped!");
}

@Test
void abortedTest() {
throw new TestAbortedException("This test will be aborted!");
}

@Test
void failingTest() {
throw new AssertionError("this test should fail");
throw new AssertionError("This test will always fail!");
}

@Test
void succeedingTest() {
// no-op
System.out.println("success");
}

}
}

0 comments on commit 55a73e4

Please sign in to comment.