File tree Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ apply plugin: 'osgi'
3
3
version = rootProject. version
4
4
5
5
dependencies {
6
+ api ' org.opentest4j:opentest4j:1.2.0'
6
7
testImplementation(group : ' junit' , name : ' junit' , version : ' 4.13' ) {
7
8
transitive = false
8
9
}
Original file line number Diff line number Diff line change
1
+ package org .hamcrest ;
2
+
3
+ import org .opentest4j .TestAbortedException ;
4
+
5
+ public final class MatcherAssume {
6
+
7
+ private MatcherAssume () {
8
+ }
9
+
10
+ public static <T > void assumeThat (T assumption , Matcher <? super T > matcher ) {
11
+ assumeThat ("" , assumption , matcher );
12
+ }
13
+
14
+ public static <T > void assumeThat (String message , T assumption , Matcher <? super T > matcher ) {
15
+ if (!matcher .matches (assumption )) {
16
+ throwTestAbortedException (message );
17
+ }
18
+ }
19
+
20
+ private static void throwTestAbortedException (String message ) {
21
+ throw new TestAbortedException (isNotBlank (message ) ? "Assumption failed: " + message : "Assumption failed" );
22
+ }
23
+
24
+ private static boolean isNotBlank (String string ) {
25
+ return string != null && !string .trim ().isEmpty ();
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ package org .hamcrest ;
2
+
3
+ import org .junit .Test ;
4
+ import org .opentest4j .TestAbortedException ;
5
+
6
+ import static org .hamcrest .MatcherAssume .assumeThat ;
7
+ import static org .hamcrest .Matchers .startsWith ;
8
+ import static org .junit .Assert .*;
9
+
10
+ public class MatcherAssumeTest {
11
+
12
+ @ Test public void
13
+ assumptionFailsWithMessage () {
14
+ try {
15
+ assumeThat ("Custom assumption" , "a" , startsWith ("abc" ));
16
+ fail ("should have failed" );
17
+ }
18
+ catch (TestAbortedException e ) {
19
+ assertEquals ("Assumption failed: Custom assumption" , e .getMessage ());
20
+ }
21
+ }
22
+
23
+ @ Test public void
24
+ assumptionFailsWithDefaultMessage () {
25
+ try {
26
+ assumeThat ("a" , startsWith ("abc" ));
27
+ fail ("should have failed" );
28
+ }
29
+ catch (TestAbortedException e ) {
30
+ assertEquals ("Assumption failed" , e .getMessage ());
31
+ }
32
+ }
33
+
34
+ @ Test public void
35
+ assumptionSucceeds () {
36
+ assumeThat ("xyz" , startsWith ("xy" ));
37
+ }
38
+ }
You can’t perform that action at this time.
0 commit comments