Skip to content

Commit 390b1f5

Browse files
committed
first draft - 20211129
1 parent f6fc321 commit 390b1f5

File tree

78 files changed

+3879
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+3879
-0
lines changed

00-basics/pom.xml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>junit5-testengines</artifactId>
7+
<groupId>org.rapidpm.event</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>junit5-testengines-00-basics</artifactId>
13+
14+
<dependencies>
15+
<!-- <dependency>-->
16+
<!-- <groupId>org.junit.platform</groupId>-->
17+
<!-- <artifactId>junit-platform-launcher</artifactId>-->
18+
<!-- <version>1.5.1</version>-->
19+
<!-- <scope>test</scope>-->
20+
<!-- </dependency>-->
21+
<dependency>
22+
<groupId>org.junit.platform</groupId>
23+
<artifactId>junit-platform-testkit</artifactId>
24+
<version>${junit.platform.version}</version>
25+
<scope>test</scope>
26+
</dependency>
27+
</dependencies>
28+
29+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.rapidpm.junit.example;
2+
3+
import org.rapidpm.frp.model.Quad;
4+
5+
import java.util.Map;
6+
import java.util.concurrent.atomic.AtomicLong;
7+
import java.util.stream.Stream;
8+
9+
import static java.util.stream.Collectors.toConcurrentMap;
10+
11+
public class DataSource {
12+
13+
public static class User
14+
extends Quad<Long, String, String, String> {
15+
16+
public User(Long id, String login, String passwd, String name) {
17+
super(id, login, passwd, name);
18+
}
19+
20+
public Long id() {
21+
return getT1();
22+
}
23+
24+
public String login() {
25+
return getT2();
26+
}
27+
28+
public String passwd() {
29+
return getT3();
30+
}
31+
32+
public String name() {
33+
return getT4();
34+
}
35+
36+
}
37+
38+
private AtomicLong idGenerator = new AtomicLong(0);
39+
40+
private Map<Long, User> persistenceStore = Stream.of(new User(idGenerator.incrementAndGet(), "admin", "admin", "Mr Admin"),
41+
new User(idGenerator.incrementAndGet(), "user", "user", "Mr User"))
42+
.collect(toConcurrentMap(User::id, u -> u));
43+
44+
public User load(Long id){
45+
return persistenceStore.get(id);
46+
}
47+
48+
public void addUser(String login, String passwd, String name){
49+
final User u = new User(idGenerator.incrementAndGet(), login, passwd, name);
50+
persistenceStore.put(u.id(), u);
51+
}
52+
53+
public void deleteUser(User u){
54+
persistenceStore.remove(u.id());
55+
}
56+
57+
public Stream<User> queryForLogin(String login, String password){
58+
return persistenceStore.values()
59+
.stream()
60+
.filter(u -> u.login().equals(login))
61+
.filter(u -> u.passwd().equals(password));
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.rapidpm.junit.example;
2+
3+
public class LoginService {
4+
5+
private DataSource dataSource;
6+
7+
public LoginService(DataSource dataSource) {
8+
this.dataSource = dataSource;
9+
}
10+
11+
public boolean checkLogin(String login, String passwd){
12+
return dataSource.queryForLogin(login, passwd).findFirst().isPresent();
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package junit.org.rapidpm.junit.basics;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
class Basic00Test {
6+
7+
@Test
8+
void test001() {
9+
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package junit.org.rapidpm.junit.basics;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.*;
5+
6+
public class Basic01Test
7+
implements BeforeAllCallback, BeforeEachCallback,
8+
AfterAllCallback, AfterEachCallback {
9+
10+
@Override
11+
public void beforeAll(ExtensionContext extensionContext) throws Exception { }
12+
13+
@Override
14+
public void beforeEach(ExtensionContext extensionContext) throws Exception { }
15+
16+
@Override
17+
public void afterEach(ExtensionContext extensionContext) throws Exception { }
18+
19+
20+
@Override
21+
public void afterAll(ExtensionContext extensionContext) throws Exception { }
22+
23+
@Test
24+
void test001() { }
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package junit.org.rapidpm.junit.basics;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.AfterEachCallback;
5+
import org.junit.jupiter.api.extension.BeforeEachCallback;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import org.junit.jupiter.api.extension.ExtensionContext;
8+
9+
public class Basic02Test {
10+
11+
public static class MyExtension
12+
implements BeforeEachCallback, AfterEachCallback {
13+
14+
@Override
15+
public void beforeEach(ExtensionContext extensionContext) throws Exception { }
16+
17+
@Override
18+
public void afterEach(ExtensionContext extensionContext) throws Exception { }
19+
}
20+
21+
@ExtendWith(MyExtension.class)
22+
public static class ExtendedTestClass {
23+
@Test
24+
void test001() { }
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package junit.org.rapidpm.junit.basics;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.BeforeEachCallback;
5+
import org.junit.jupiter.api.extension.ExtendWith;
6+
import org.junit.jupiter.api.extension.ExtensionContext;
7+
import org.junit.jupiter.api.extension.Extensions;
8+
9+
public class Basic03Test {
10+
public static class MyExtensionA
11+
implements BeforeEachCallback {
12+
@Override
13+
public void beforeEach(ExtensionContext extensionContext) throws Exception { }
14+
}
15+
16+
public static class MyExtensionB
17+
implements BeforeEachCallback {
18+
@Override
19+
public void beforeEach(ExtensionContext extensionContext) throws Exception { }
20+
}
21+
22+
@ExtendWith(Basic03Test.MyExtensionB.class)
23+
@ExtendWith(Basic03Test.MyExtensionA.class)
24+
public static class ExtendedTestClass {
25+
@Test
26+
void test001() { }
27+
}
28+
29+
@Extensions({
30+
@ExtendWith({Basic03Test.MyExtensionB.class}),
31+
@ExtendWith({Basic03Test.MyExtensionA.class})
32+
})
33+
public @interface BothExtensions { }
34+
35+
@BothExtensions
36+
public static class MyExtendedTestClass {
37+
@Test
38+
void test001() { }
39+
}
40+
}
41+
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package junit.org.rapidpm.junit.basics;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.extension.AfterEachCallback;
5+
import org.junit.jupiter.api.extension.BeforeEachCallback;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import org.junit.jupiter.api.extension.ExtensionContext;
8+
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
9+
import org.rapidpm.dependencies.core.logger.HasLogger;
10+
11+
public class Basic04Test {
12+
13+
public static class MyExtension
14+
implements BeforeEachCallback, AfterEachCallback, HasLogger {
15+
16+
public static final String KEY = "KEY";
17+
public static final Namespace MY_EXTENSION_NAMESPACE = Namespace.GLOBAL;
18+
19+
20+
@Override
21+
public void beforeEach(ExtensionContext ctx) throws Exception {
22+
final ExtensionContext.Store store = ctx.getStore(MY_EXTENSION_NAMESPACE);
23+
//create and start something
24+
store.put(KEY, "something");
25+
logger().info("beforeEach");
26+
}
27+
28+
@Override
29+
public void afterEach(ExtensionContext ctx) throws Exception {
30+
final ExtensionContext.Store store = ctx.getStore(Namespace.GLOBAL);
31+
//stop something again
32+
final String value = store.get(KEY, String.class);
33+
logger().info("afterEach");
34+
}
35+
36+
@Test
37+
@ExtendWith(MyExtension.class)
38+
void test001() {
39+
//possible to annotate only a method
40+
}
41+
}
42+
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package junit.org.rapidpm.junit.basics;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.extension.*;
6+
import org.rapidpm.frp.model.serial.Pair;
7+
8+
9+
public class Basic05Test {
10+
11+
public static class Demo
12+
extends Pair<Integer, String> {
13+
public Demo(Integer id, String value) {
14+
super(id, value);
15+
}
16+
17+
public Integer id() {
18+
return getT1();
19+
}
20+
21+
public String value() {
22+
return getT2();
23+
}
24+
}
25+
26+
public static class DemoParameterResolver
27+
implements ParameterResolver {
28+
29+
@Override
30+
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
31+
throws ParameterResolutionException {
32+
final Class<?> type = parameterContext.getParameter()
33+
.getType();
34+
return Demo.class.isAssignableFrom(type);
35+
}
36+
37+
@Override
38+
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
39+
throws ParameterResolutionException {
40+
return new Demo(1, "Hello World");
41+
}
42+
}
43+
44+
45+
@Test
46+
@ExtendWith(Basic05Test.DemoParameterResolver.class)
47+
void test001(Demo demo){
48+
Assertions.assertEquals("Hello World", demo.value());
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package junit.org.rapidpm.junit.example;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
import org.rapidpm.junit.example.LoginService;
6+
7+
public class LoginServiceTest {
8+
9+
10+
// make it happen :-)
11+
//@Test
12+
void test01(LoginService service){
13+
Assertions.assertTrue(service.checkLogin("admin", "admin"));
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package junit.org.rapidpm.junit.tdd;
2+
3+
4+
import org.junit.jupiter.api.Disabled;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.platform.testkit.engine.EngineTestKit;
7+
8+
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
9+
10+
public class TestEngineTDD01Test {
11+
12+
public static class DemoTestClass {
13+
@Test
14+
void test001() { }
15+
16+
@Test
17+
@Disabled //enable for testing TestEngine ;-)
18+
void test002() { throw new RuntimeException("I must fail");}
19+
20+
@Test
21+
@Disabled
22+
void test003() { }
23+
}
24+
25+
@Test
26+
void verifyJupiterContainerStats() {
27+
EngineTestKit.engine("junit-jupiter")
28+
.selectors(selectClass(DemoTestClass.class))
29+
.execute()
30+
.containers()
31+
.assertStatistics(stats -> stats.started(2)
32+
.succeeded(2)
33+
.skipped(0));
34+
}
35+
36+
@Test
37+
@Disabled //enable for testing TestEngine ;-)
38+
void verifyJupiterTestStats() {
39+
EngineTestKit.engine("junit-jupiter")
40+
.selectors(selectClass(DemoTestClass.class))
41+
.execute()
42+
.tests()
43+
.assertStatistics(stats -> stats.started(2)
44+
.succeeded(1)
45+
.skipped(1)
46+
.failed(1));
47+
}
48+
49+
50+
}

0 commit comments

Comments
 (0)