-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathRule.java
98 lines (93 loc) · 3.08 KB
/
Rule.java
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
package org.junit;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotates fields that reference rules or methods that return a rule. A field must be public, not
* static, and a subtype of {@link org.junit.rules.TestRule} (preferred) or
* {@link org.junit.rules.MethodRule}. A method must be public, not static,
* and must return a subtype of {@link org.junit.rules.TestRule} (preferred) or
* {@link org.junit.rules.MethodRule}.
* <p>
* The {@link org.junit.runners.model.Statement} passed
* to the {@link org.junit.rules.TestRule} will run any {@link Before} methods,
* then the {@link Test} method, and finally any {@link After} methods,
* throwing an exception if any of these fail.
*
* <h3>Usage</h3>
* <p>
* For example, here is a test class that creates a temporary folder before
* each test method, and deletes it after each:
* <pre>
* public static class HasTempFolder {
* @Rule
* public TemporaryFolder folder= new TemporaryFolder();
*
* @Test
* public void testUsingTempFolder() throws IOException {
* File createdFile= folder.newFile("myfile.txt");
* File createdFolder= folder.newFolder("subfolder");
* // ...
* }
* }
* </pre>
* <p>
* And the same using a method.
* <pre>
* public static class HasTempFolder {
* private TemporaryFolder folder= new TemporaryFolder();
*
* @Rule
* public TemporaryFolder getFolder() {
* return folder;
* }
*
* @Test
* public void testUsingTempFolder() throws IOException {
* File createdFile= folder.newFile("myfile.txt");
* File createdFolder= folder.newFolder("subfolder");
* // ...
* }
* }
* </pre>
* <p>
* For more information and more examples, see
* {@link org.junit.rules.TestRule}.
*
* <h3>Ordering</h3>
* <p>
* You can use {@link #order()} (default = -1) if you want control over the order in which the
* Rules are applied. Rules with the lowest {@link #order()} value will be applied first. If
* multiple rules have the same {@link #order()} then those implemented as methods will be applied
* before those implemented as fields, with the ordering within those groups dependant on your JVM's
* implementation of the reflection API (generally undefined).
*
* <pre>
* public class ThreeRules {
* @Rule(order = 0)
* public LoggingRule outer = new LoggingRule("outer rule");
*
* @Rule(order = 1)
* public LoggingRule middle = new LoggingRule("middle rule");
*
* @Rule(order = 2)
* public LoggingRule inner = new LoggingRule("inner rule");
*
* // ...
* }
* </pre>
*
* @since 4.7
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface Rule {
int DEFAULT_ORDER = -1;
/**
* Specifies the order in which rules are applied. The rules with a higher value are inner.
*
* @since 4.13
*/
int order() default DEFAULT_ORDER;
}