File tree Expand file tree Collapse file tree
java/com/stackify/test/conditions
test/java/com/stackify/test Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .stackify .test .conditions ;
2+
3+ import java .lang .annotation .ElementType ;
4+ import java .lang .annotation .Retention ;
5+ import java .lang .annotation .RetentionPolicy ;
6+ import java .lang .annotation .Target ;
7+
8+ import org .junit .jupiter .api .extension .ExtendWith ;
9+
10+ @ Target ({ ElementType .METHOD })
11+ @ Retention (RetentionPolicy .RUNTIME )
12+ @ ExtendWith (DisabledOnEnvironmentCondition .class )
13+ public @interface DisabledOnEnvironment {
14+ String [] value ();
15+ }
Original file line number Diff line number Diff line change 1+ package com .stackify .test .conditions ;
2+
3+ import java .io .IOException ;
4+ import java .util .Arrays ;
5+ import java .util .Optional ;
6+ import java .util .Properties ;
7+
8+ import org .junit .jupiter .api .extension .ConditionEvaluationResult ;
9+ import org .junit .jupiter .api .extension .TestExecutionCondition ;
10+ import org .junit .jupiter .api .extension .TestExtensionContext ;
11+ import org .junit .platform .commons .support .AnnotationSupport ;
12+
13+ import com .stackify .utils .ConnectionUtil ;
14+
15+ public class DisabledOnEnvironmentCondition implements TestExecutionCondition {
16+
17+ @ Override
18+ public ConditionEvaluationResult evaluate (TestExtensionContext context ) {
19+ Properties props = new Properties ();
20+ String env = "" ;
21+ try {
22+ props .load (ConnectionUtil .class .getResourceAsStream ("/application.properties" ));
23+ env = props .getProperty ("env" );
24+ } catch (IOException e ) {
25+ e .printStackTrace ();
26+ }
27+ Optional <DisabledOnEnvironment > disabled = AnnotationSupport .findAnnotation (context .getElement ().get (), DisabledOnEnvironment .class );
28+ if (disabled .isPresent ()) {
29+ String [] envs = disabled .get ().value ();
30+ if (Arrays .asList (envs ).contains (env )) {
31+ return ConditionEvaluationResult .disabled ("Disabled on environment " + env );
32+ }
33+ }
34+
35+ return ConditionEvaluationResult .enabled ("Enabled on environment " +env );
36+ }
37+
38+ }
Original file line number Diff line number Diff line change 1+ env =dev
Original file line number Diff line number Diff line change 2323import com .stackify .daos .UserDAO ;
2424import com .stackify .models .User ;
2525
26+ import com .stackify .test .conditions .DisabledOnEnvironment ;
27+
2628public class UsersTest implements DatabaseConnectionTest {
2729
2830 private static UserDAO userDAO ;
@@ -110,7 +112,7 @@ void testThrows() {
110112 }
111113
112114 @ Test
113- @ Disabled
115+ @ DisabledOnEnvironment ({ "dev" , "prod" )
114116 void testFail () {
115117 fail ("this test fails" );
116118 }
You can’t perform that action at this time.
0 commit comments