You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+4
Original file line number
Diff line number
Diff line change
@@ -3,13 +3,17 @@ Repository of re-usable test utilities.
3
3
4
4
### Modules
5
5
The following modules are defined:
6
+
* internal
6
7
* spock-all
7
8
* junit-extensions
8
9
* spock-extensions
9
10
* hamcrest-extensions
10
11
* mockito-extensions
11
12
* failsafe-controller
12
13
14
+
#### internal
15
+
Defines a set of common utilities used internally by all other modules. This module is not meant to be depended on from outside this workspace.
16
+
13
17
#### spock-all
14
18
Defines a single pom artifact that contains all dependencies required to develop and execute Spock specifications. Simply add the following to your pom file to be able to compile and run Spock specifications:
Copy file name to clipboardExpand all lines: docs/failsafe-controller.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
#Testing Failsafe Code
1
+
## Testing Failsafe Code
2
2
3
3
Failsafe (from `net.jodah.failsafe`) is a really nice library that is typically used in code to repeatedly execute a given task until it succeeds or aborts. Although it can be used synchronously, it is often used asynchronously using an executor such that the tasks are executed by other threads.
Copy file name to clipboardExpand all lines: docs/junit-extensions.md
+178-2
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,36 @@
1
-
#JUnit Extensions
1
+
## JUnit Extensions
2
+
3
+
JUnit extensions are divided in new method rules, method rule annotations, and test runners.
4
+
5
+
### JUnit Method Rules
6
+
7
+
#### MethodRuleAnnotationProcessor
8
+
The [MethodRuleAnnotationProcessor](../junit-extensions/src/main/java/org/codice/junit/rules/MethodRuleAnnotationProcessor.java) provides a JUnit4 method rule that implements a similar support to Spock extensions by allowing specification of simple JUnit method rules via the meta-annotation [ExtensionMethodRuleAnnotation](../junit-extensions/src/main/java/org/codice/junit/ExtensionMethodRuleAnnotation.java).
9
+
10
+
New annotations created referencing a JUnit method rule can be used to annotate JUnit test class when the method rule should apply to all tests in that class. These anntations can also be used to annotate a particular test method if the method rule should only apply to that test method.
11
+
12
+
The order these new annotations are defined in will dictate the order they will be applied from outermost to innermost.
13
+
14
+
Method rules referenced in this manner will not retain any states from one test method to the next as they will be re-instantiated each time. The method rule class must define either a public constructor with a single argument of the same annotation type as the annotation that is annotated with the meta-annotation [ExtensionMethodRuleAnnotation](../junit-extensions/src/main/java/org/codice/junit/ExtensionMethodRuleAnnotation.java) or a public default constructor. Defining a constructor with the annotation as a parameter allows customization of the method rule using your own annotation.
15
+
16
+
17
+
```
18
+
@RestoreSystemProperties // will apply to each test methods
19
+
public class MyTest {
20
+
@Rule public final MethodRuleAnnotationProcessor processor = new MethodRuleAnnotationProcessor();
21
+
22
+
@ClearInterruptions // will only apply to this test method
23
+
@Test
24
+
public void testSomething() throws Exception {
25
+
}
26
+
27
+
@Test
28
+
public void testSomethingElse() throws Exception {
29
+
}
30
+
}
31
+
```
32
+
33
+
The advantage of using the test runner [MethodRuleAnnotationRunner](../junit-extensions/src/main/java/org/codice/junit/MethodRuleAnnotationRunner.java) over the method rule [MethodRuleAnnotationProcessor](../junit-extensions/src/main/java/org/codice/junit/rules/MethodRuleAnnotationProcessor.java) is that it guarantees that all annotation-based rules will be considered outermost compared to any rules defined within the test class whereas the processor cannot guarantee that since it is at the mercy of JUnit in terms of how the rules are internally initialized.
2
34
3
35
#### RestoreSystemProperties
4
36
The [RestoreSystemProperties](../junit-extensions/src/main/java/org/codice/junit/rules/RestoreSystemProperties.java) provides a Java version of the Spock annotation which when defined as a JUnit rule will automatically reset the system properties to their initial values after each tests.
@@ -17,7 +49,7 @@ The [RestoreSystemProperties](../junit-extensions/src/main/java/org/codice/junit
17
49
```
18
50
19
51
#### ClearInterruptions
20
-
The [ClearInterruptions](../junit-extensions/src/main/java/org/codice/junit/rules/ClearInterruptions.java) provides a Java version of the Spock annotation which when defined as a JUnit rule will clear interruption states from the current thread after testing. For example:
52
+
The [ClearInterruptions](../junit-extensions/src/main/java/org/codice/junit/rules/ClearInterruptions.java) provides a Java version of the Spock annotation which when defined as a JUnit rule will clear interruption state from the current thread after testing. For example:
21
53
```
22
54
public class MyTest {
23
55
@Rule public final ClearInterruptions clearInterruptions = new ClearInterruptions();
@@ -34,6 +66,150 @@ The [ClearInterruptions](../junit-extensions/src/main/java/org/codice/junit/rule
34
66
}
35
67
```
36
68
69
+
#### MethodRuleChain
70
+
The [MethodRuleChain](../junit-extensions/src/main/java/org/codice/junit/rules/MethodRuleChain.java) provides a JUnit4 rule that can be used to create a controlled chain of method rules when the order they are processed is important.
71
+
72
+
```
73
+
public class MyTest {
74
+
@Rule public final MethodRuleChain chain =
75
+
MethodRuleChain
76
+
.outer(new RestoreSystemProperties())
77
+
.around(new ClearInterruptions());
78
+
79
+
@Test
80
+
public void testSomething() throws Exception {
81
+
System.setProperty("ddf.home", "some new location");
82
+
final MyClass obj = new MyClass(some);
83
+
84
+
obj.doSomething();
85
+
}
86
+
}
87
+
```
88
+
89
+
### JUnit Method Rule Annotations
90
+
91
+
#### RestoreSystemProperties
92
+
The [RestoreSystemProperties](../junit-extensions/src/main/java/org/codice/junit/RestoreSystemProperties.java) annotation can be used in conjunction with the MethodRuleAnnotationProcessor JUnit method rule to indicate all methods of a test class or specific ones where system properties should automatically be reset to their initial values after testing.
93
+
94
+
#### ClearInterruptions
95
+
The [ClearInterruptions](../junit-extensions/src/main/java/org/codice/junit/ClearInterruptions.java) annotation can be used in conjunction with the MethodRuleAnnotationProcessor JUnit method rule to indicate all methods of a test class or specific ones where the interruption state from the current thread should automatically be cleared after testing.
96
+
97
+
### JUnit Test Runners
98
+
99
+
#### MethodRuleAnnotationRunner
100
+
The [MethodRuleAnnotationRunner](../junit-extensions/src/main/java/org/codice/junit/MethodRuleAnnotationRunner.java) provides a JUnit4 test runner that supports annotation-based method rules similar support to Spock extensions by allowing specification of simple JUnit method rules via the meta-annotation [ExtensionMethodRuleAnnotation](../junit-extensions/src/main/java/org/codice/junit/ExtensionMethodRuleAnnotation.java).
101
+
102
+
New annotations created referencing another JUnit method rule can be used to annotate JUnit test class when the method rule should apply to all tests in that class. These annotations can also be used to annotate a particular test method if the method rule should only apply to that test method.
103
+
104
+
The order these new annotations are defined in will dictate the order they will be applied from outermost to innermost.
105
+
106
+
Method rules referenced in this manner will not retain any states from one test method to the next as they will be re-instantiated each time. The method rule class must define either a public constructor with a single argument of the same annotation type as the annotation that is annotated with the meta-annotation [ExtensionMethodRuleAnnotation](../junit-extensions/src/main/java/org/codice/junit/ExtensionMethodRuleAnnotation.java) or a public default constructor. Defining a constructor with the annotation as a parameter allows customization of the method rule using your own annotation.
107
+
108
+
```
109
+
@RestoreSystemProperties // will apply to each test methods
110
+
@RunWith(MethodRuleAnnotationRunner.class)
111
+
public class MyTest {
112
+
@ClearInterruptions // will only apply to this test method
113
+
@Test
114
+
public void testSomething() throws Exception {
115
+
}
116
+
117
+
@Test
118
+
public void testSomethingElse() throws Exception {
119
+
}
120
+
}
121
+
```
122
+
123
+
Example of a method rule annotation that can be customized:
124
+
125
+
```
126
+
public MyMethodRule implements MethodRule {
127
+
private final long timeout;
128
+
129
+
public MyMethodRule(MyAnnotation annotation) {
130
+
this.timeout = annotation.timeout();
131
+
}
132
+
133
+
@Override
134
+
public Statement apply(Statement statement, FrameworkMethod frameworkMethod, Object o) {
The advantage of using the test runner [MethodRuleAnnotationRunner](../junit-extensions/src/main/java/org/codice/junit/MethodRuleAnnotationRunner.java) over the method rule [MethodRuleAnnotationProcessor](../junit-extensions/src/main/java/org/codice/junit/rules/MethodRuleAnnotationProcessor.java) is that it guarantees that all annotation-based rules will be considered outermost compared to any rules defined within the test class whereas the processor cannot guarantee that since it is at the mercy of JUnit in terms of how the rules are internally initialized.
The [MethodRuleAnnotationRunnerWithParametersFactory](../junit-extensions/src/main/java/org/codice/junit/parameterized/MethodRuleAnnotationRunnerWithParametersFactory.java) provides a JUnit4 parameter factory to create test runners that allows specification of method rules via the meta-annotation [ExtensionMethodRuleAnnotation](../junit-extensions/src/main/java/org/codice/junit/ExtensionMethodRuleAnnotation.java).
153
+
Such a factory can be specified when using the `Parameterized` test runner to run parameterized tests.
154
+
155
+
New annotations created referencing another JUnit method rule can be used to annotate JUnit test class when the method rule should apply to all tests in that class. These annotations can also be used to annotate a particular test method if the method rule should only apply to that test method.
156
+
157
+
The order these new annotations are defined in will dictate the order they will be applied from outermost to innermost.
158
+
159
+
Method rules referenced in this manner will not retain any states from one test method to the next as they will be re-instantiated each time. The method rule class must define either a public constructor with a single argument of the same annotation type as the annotation that is annotated with the meta-annotation [ExtensionMethodRuleAnnotation](../junit-extensions/src/main/java/org/codice/junit/ExtensionMethodRuleAnnotation.java) or a public default constructor. Defining a constructor with the annotation as a parameter allows customization of the method rule using your own annotation.
160
+
161
+
```
162
+
@RestoreSystemProperties // will apply to each test methods
The advantage of using the test runner [MethodRuleAnnotationRunner](../junit-extensions/src/main/java/org/codice/junit/MethodRuleAnnotationRunner.java) over the method rule [MethodRuleAnnotationProcessor](../junit-extensions/src/main/java/org/codice/junit/rules/MethodRuleAnnotationProcessor.java) is that it guarantees that all annotation-based rules will be considered outermost compared to any rules defined within the test class whereas the processor cannot guarantee that since it is at the mercy of JUnit in terms of how the rules are internally initialized.
212
+
37
213
#### Definalizer
38
214
The [Definalizer JUnit test runner](../junit-extensions/src/main/java/org/codice/junit/DeFinalizer.java) is designed as a generic proxy test runner for another JUnit test runner by indirectly instantiating that runner in order to add support for de-finalizing (i.e. removing the final constraint) 3rd party Java classes that need to be mocked or stubbed during testing.
39
215
It does so by creating a classloader designed with an aggressive strategy where it will load all classes first before delegating to its parent. This classloader will therefore reload all classes while definalizing those that are requested except for all classes in the following packages:
Copy file name to clipboardExpand all lines: docs/spock-extensions.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
#Spock Extensions
1
+
## Spock Extensions
2
2
3
3
#### @ClearInterruptions
4
4
The [`@ClearInterruptions`](../spock-extensions/src/main/groovy/org/codice/spock/ClearInterruptions.groovy) annotation can be used to clear interruption states from the current thread after testing. For example:
* This is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either
7
+
* version 3 of the License, or any later version.
8
+
*
9
+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
+
* See the GNU Lesser General Public License for more details. A copy of the GNU Lesser General Public License is distributed along with this program and can be found at
0 commit comments