Skip to content

Commit 1c543f8

Browse files
marcos-lgmaibin
authored andcommitted
BAEL-1233 - Enabled Annotation in Spring 5 Testing (eugenp#3031)
* tests with enabled annotations * rollback pom * formatting
1 parent d076823 commit 1c543f8

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.jupiter;
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.springframework.test.context.junit.jupiter.EnabledIf;
9+
10+
@Target({ ElementType.TYPE, ElementType.METHOD })
11+
@Retention(RetentionPolicy.RUNTIME)
12+
@EnabledIf(
13+
expression = "#{systemProperties['java.version'].startsWith('1.8')}",
14+
reason = "Enabled on Java 8"
15+
)
16+
public @interface EnabledOnJava8 {
17+
18+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung.jupiter;
2+
3+
import static org.junit.jupiter.api.Assertions.assertTrue;
4+
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.test.context.TestPropertySource;
8+
import org.springframework.test.context.junit.jupiter.DisabledIf;
9+
import org.springframework.test.context.junit.jupiter.EnabledIf;
10+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
11+
12+
@SpringJUnitConfig(Spring5EnabledAnnotationTest.Config.class)
13+
@TestPropertySource(properties = { "tests.enabled=true" })
14+
public class Spring5EnabledAnnotationTest {
15+
16+
@Configuration
17+
static class Config {
18+
}
19+
20+
@EnabledIf("true")
21+
@Test
22+
void givenEnabledIfLiteral_WhenTrue_ThenTestExecuted() {
23+
assertTrue(true);
24+
}
25+
26+
@EnabledIf(expression = "${tests.enabled}", loadContext = true)
27+
@Test
28+
void givenEnabledIfExpression_WhenTrue_ThenTestExecuted() {
29+
assertTrue(true);
30+
}
31+
32+
@EnabledIf("#{systemProperties['java.version'].startsWith('1.8')}")
33+
@Test
34+
void givenEnabledIfSpel_WhenTrue_ThenTestExecuted() {
35+
assertTrue(true);
36+
}
37+
38+
@EnabledOnJava8
39+
@Test
40+
void givenEnabledOnJava8_WhenTrue_ThenTestExecuted() {
41+
assertTrue(true);
42+
}
43+
44+
@DisabledIf("#{systemProperties['java.version'].startsWith('1.7')}")
45+
@Test
46+
void givenDisabledIf_WhenTrue_ThenTestNotExecuted() {
47+
assertTrue(true);
48+
}
49+
50+
}

0 commit comments

Comments
 (0)