forked from junit-team/junit-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassTemplateDemo.java
More file actions
99 lines (84 loc) · 2.72 KB
/
ClassTemplateDemo.java
File metadata and controls
99 lines (84 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright 2015-2025 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package example;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.List;
import java.util.stream.Stream;
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.ClassTemplate;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ClassTemplateInvocationContext;
import org.junit.jupiter.api.extension.ClassTemplateInvocationContextProvider;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
// tag::user_guide[]
@ClassTemplate
@ExtendWith(ClassTemplateDemo.MyClassTemplateInvocationContextProvider.class)
class ClassTemplateDemo {
static final List<String> WELL_KNOWN_FRUITS
// tag::custom_line_break[]
= List.of("apple", "banana", "lemon");
//end::user_guide[]
@Nullable
//tag::user_guide[]
private String fruit;
@Test
void notNull() {
assertNotNull(fruit);
}
@Test
void wellKnown() {
assertTrue(WELL_KNOWN_FRUITS.contains(fruit));
}
// end::user_guide[]
public
// tag::user_guide[]
static class MyClassTemplateInvocationContextProvider
// tag::custom_line_break[]
implements ClassTemplateInvocationContextProvider {
@Override
public boolean supportsClassTemplate(ExtensionContext context) {
return true;
}
@Override
public Stream<ClassTemplateInvocationContext>
// tag::custom_line_break[]
provideClassTemplateInvocationContexts(ExtensionContext context) {
return Stream.of(invocationContext("apple"), invocationContext("banana"));
}
private ClassTemplateInvocationContext invocationContext(String parameter) {
return new ClassTemplateInvocationContext() {
@Override
public String getDisplayName(int invocationIndex) {
return parameter;
}
// end::user_guide[]
@SuppressWarnings("Convert2Lambda")
// tag::user_guide[]
@Override
public List<Extension> getAdditionalExtensions() {
return singletonList(new TestInstancePostProcessor() {
@Override
public void postProcessTestInstance(
// tag::custom_line_break[]
Object testInstance, ExtensionContext context) {
((ClassTemplateDemo) testInstance).fruit = parameter;
}
});
}
};
}
}
}
// end::user_guide[]