Skip to content

Commit 79576af

Browse files
committed
How to use junit.jupiter Extensions in OSGi and plain java
1 parent 852b0b4 commit 79576af

File tree

6 files changed

+105
-2
lines changed

6 files changed

+105
-2
lines changed

eclipse.platform.releng/bundles/org.eclipse.test/META-INF/MANIFEST.MF

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ Require-Bundle: org.apache.ant,
1111
org.eclipse.core.runtime,
1212
org.eclipse.ui.ide.application,
1313
org.eclipse.equinox.app,
14-
org.junit
14+
org.apache.aries.spifly.dynamic.bundle,
15+
org.junit,
16+
junit-jupiter-api
1517
Import-Package: org.junit.jupiter.api,
1618
org.junit.platform.engine,
1719
org.junit.platform.engine.discovery,
@@ -24,5 +26,6 @@ Import-Package: org.junit.jupiter.api,
2426
org.junit.jupiter.engine
2527
Bundle-ActivationPolicy: lazy
2628
Bundle-RequiredExecutionEnvironment: JavaSE-17
27-
Export-Package: org.eclipse.test
29+
Export-Package: org.eclipse.test,
30+
org.eclipse.test.services
2831
Automatic-Module-Name: org.eclipse.test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.test.services.LoggingTestExtension
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
junit.jupiter.extensions.autodetection.enabled=true
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.eclipse.test.services;
2+
3+
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
4+
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
5+
import org.junit.jupiter.api.extension.ExtensionContext;
6+
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
7+
import org.junit.jupiter.api.extension.ExtensionContext.Store;
8+
9+
/** logs start, stop, duration, error for all tests executed **/
10+
public class LoggingTestExtension implements AfterTestExecutionCallback, BeforeTestExecutionCallback {
11+
public static String BEFORE_TEST_START = "Test Before: ";
12+
public static String AFTER_TEST_PASSED = "Test Passed: ";
13+
public static String AFTER_TEST_FAILED = "Test Failed: ";
14+
15+
public LoggingTestExtension() {
16+
System.out.println("LoggingTestService");
17+
}
18+
@Override
19+
public void beforeTestExecution(ExtensionContext context) throws Exception {
20+
long n0 = System.nanoTime();
21+
getStore(context).put("TIME", n0);
22+
System.out.println(BEFORE_TEST_START + context.getDisplayName());
23+
}
24+
25+
@Override
26+
public void afterTestExecution(ExtensionContext context) throws Exception {
27+
long t1 = System.nanoTime();
28+
long t0 = getStore(context).remove("TIME", long.class);
29+
String took = " after: " + (t1 - t0) / 1_000_000 + "ms";
30+
Throwable t = context.getExecutionException().orElse(null);
31+
if (t == null) {
32+
System.out.println(AFTER_TEST_PASSED + context.getDisplayName() + took);
33+
} else {
34+
System.out.println(AFTER_TEST_FAILED + context.getDisplayName() + took);
35+
t.printStackTrace(System.out);
36+
}
37+
}
38+
39+
private Store getStore(ExtensionContext context) {
40+
return context.getStore(Namespace.create(getClass(), context.getRequiredTestMethod()));
41+
}
42+
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.eclipse.test.services;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintStream;
7+
8+
import org.junit.jupiter.api.AfterAll;
9+
import org.junit.jupiter.api.BeforeAll;
10+
import org.junit.jupiter.api.Test;
11+
12+
/** verifies the Extension is used **/
13+
public class LoggingTestExtensionTest {
14+
private static final String SOMETHING = "something";
15+
private static final ByteArrayOutputStream OUT = new ByteArrayOutputStream();
16+
private static PrintStream oldStdOut;
17+
18+
@BeforeAll
19+
public static void beforeAll() {
20+
oldStdOut = System.out;
21+
System.setOut(new PrintStream(OUT));
22+
}
23+
@Test
24+
public void testWritten() {
25+
System.out.println(SOMETHING);
26+
}
27+
28+
@AfterAll
29+
public static void afterAll() {
30+
String output = new String(OUT.toByteArray());
31+
System.setOut(oldStdOut);
32+
assertTrue(output, output.contains("testWritten"));
33+
assertTrue(output, output.contains(SOMETHING));
34+
assertTrue(output, output.contains(org.eclipse.test.services.LoggingTestExtension.BEFORE_TEST_START));
35+
assertTrue(output, output.contains(org.eclipse.test.services.LoggingTestExtension.AFTER_TEST_PASSED));
36+
}
37+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.eclipse.test.services;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
// Manual demonstration:
6+
public class LoggingTestExtensionTest2 {
7+
8+
@Test
9+
public void testWritten() {
10+
System.out.println("real");
11+
}
12+
13+
@Test
14+
public void testWritten2() {
15+
throw new RuntimeException("intended");
16+
}
17+
18+
}

0 commit comments

Comments
 (0)