Skip to content

Commit 0a2c3e5

Browse files
committed
Fix @EnabledIf/@DisabledIf unit tests
1 parent 67c5f40 commit 0a2c3e5

File tree

5 files changed

+74
-82
lines changed

5 files changed

+74
-82
lines changed

junit-jupiter-api/src/main/java/org/junit/jupiter/api/condition/MethodBasedCondition.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Method;
2020
import java.util.Optional;
2121
import java.util.function.Function;
22+
import java.util.function.Supplier;
2223

2324
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
2425
import org.junit.jupiter.api.extension.ExecutionCondition;
@@ -91,12 +92,13 @@ private boolean acceptsExtensionContextOrNoArguments(Method method) {
9192
}
9293

9394
private ConditionEvaluationResult buildConditionEvaluationResult(boolean methodResult, A annotation) {
94-
String defaultReason = format("Condition provided in %s evaluated to %s", this.annotationType, methodResult);
95+
Supplier<String> defaultReason = () -> format("@%s(\"%s\") evaluated to %s",
96+
this.annotationType.getSimpleName(), this.methodName.apply(annotation), methodResult);
9597
if (isEnabled(methodResult)) {
96-
return enabled(defaultReason);
98+
return enabled(defaultReason.get());
9799
}
98100
String customReason = this.customDisabledReason.apply(annotation);
99-
return StringUtils.isNotBlank(customReason) ? disabled(customReason) : disabled(defaultReason);
101+
return StringUtils.isNotBlank(customReason) ? disabled(customReason) : disabled(defaultReason.get());
100102
}
101103

102104
protected abstract boolean isEnabled(boolean methodResult);

junit-jupiter-engine/src/test/java/org/junit/jupiter/api/condition/DisabledIfConditionTests.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
package org.junit.jupiter.api.condition;
1212

13-
import org.junit.jupiter.api.Nested;
1413
import org.junit.jupiter.api.Test;
1514
import org.junit.jupiter.api.extension.ExecutionCondition;
1615

@@ -61,7 +60,8 @@ void disabledBecauseStaticConditionMethodReturnsTrue() {
6160
void enabledBecauseStaticConditionMethodReturnsFalse() {
6261
evaluateCondition();
6362
assertEnabled();
64-
assertReasonContains("Condition provided in @DisabledIf evaluates to false");
63+
assertReasonContains("""
64+
@DisabledIf("staticMethodThatReturnsFalse") evaluated to false""");
6565
}
6666

6767
/**
@@ -71,7 +71,8 @@ void enabledBecauseStaticConditionMethodReturnsFalse() {
7171
void disabledBecauseConditionMethodReturnsTrue() {
7272
evaluateCondition();
7373
assertDisabled();
74-
assertReasonContains("Condition provided in @DisabledIf evaluates to true");
74+
assertReasonContains("""
75+
@DisabledIf("methodThatReturnsTrue") evaluated to true""");
7576
}
7677

7778
/**
@@ -81,32 +82,31 @@ void disabledBecauseConditionMethodReturnsTrue() {
8182
void enabledBecauseConditionMethodReturnsFalse() {
8283
evaluateCondition();
8384
assertEnabled();
84-
assertReasonContains("Condition provided in @DisabledIf evaluates to false");
85+
assertReasonContains("""
86+
@DisabledIf("methodThatReturnsFalse") evaluated to false""");
8587
}
8688

87-
@Nested
88-
class ExternalConditionMethod {
89-
90-
/**
91-
* @see DisabledIfIntegrationTests.ExternalConditionMethod#disabledBecauseConditionMethodReturnsTrue()
92-
*/
93-
@Test
94-
void disabledBecauseConditionMethodReturnsTrue() {
95-
evaluateCondition();
96-
assertDisabled();
97-
assertReasonContains("Condition provided in @DisabledIf evaluates to true");
98-
}
99-
100-
/**
101-
* @see DisabledIfIntegrationTests.ExternalConditionMethod#enabledBecauseConditionMethodReturnsFalse()
102-
*/
103-
@Test
104-
void enabledBecauseConditionMethodReturnsFalse() {
105-
evaluateCondition();
106-
assertEnabled();
107-
assertReasonContains("Condition provided in @DisabledIf evaluates to false");
108-
}
89+
/**
90+
* @see DisabledIfIntegrationTests.ExternalConditionMethod#disabledBecauseStaticExternalConditionMethodReturnsTrue()
91+
*/
92+
@Test
93+
void disabledBecauseStaticExternalConditionMethodReturnsTrue() {
94+
evaluateCondition();
95+
assertDisabled();
96+
assertReasonContains("""
97+
@DisabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsTrue") evaluated to true""");
98+
}
10999

100+
/**
101+
* @see DisabledIfIntegrationTests.ExternalConditionMethod#enabledBecauseStaticExternalConditionMethodReturnsFalse()
102+
*/
103+
@Test
104+
void enabledBecauseStaticExternalConditionMethodReturnsFalse() {
105+
evaluateCondition();
106+
assertEnabled();
107+
assertReasonContains(
108+
"""
109+
@DisabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse") evaluated to false""");
110110
}
111111

112112
}

junit-jupiter-engine/src/test/java/org/junit/jupiter/api/condition/DisabledIfIntegrationTests.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,15 @@ void disabledBecauseConditionMethodReturnsTrue() {
5050
void enabledBecauseConditionMethodReturnsFalse() {
5151
}
5252

53-
@Nested
54-
class ExternalConditionMethod {
55-
56-
@Test
57-
@DisabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsTrue")
58-
void disabledBecauseConditionMethodReturnsTrue() {
59-
fail("Should be disabled");
60-
}
61-
62-
@Test
63-
@DisabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse")
64-
void enabledBecauseConditionMethodReturnsFalse() {
65-
}
53+
@Test
54+
@DisabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsTrue")
55+
void disabledBecauseStaticExternalConditionMethodReturnsTrue() {
56+
fail("Should be disabled");
57+
}
6658

59+
@Test
60+
@DisabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse")
61+
void enabledBecauseStaticExternalConditionMethodReturnsFalse() {
6762
}
6863

6964
@Nested

junit-jupiter-engine/src/test/java/org/junit/jupiter/api/condition/EnabledIfConditionTests.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
package org.junit.jupiter.api.condition;
1212

13-
import org.junit.jupiter.api.Nested;
1413
import org.junit.jupiter.api.Test;
1514
import org.junit.jupiter.api.extension.ExecutionCondition;
1615

@@ -51,7 +50,8 @@ void enabledBecauseAnnotationIsNotPresent() {
5150
void enabledBecauseStaticConditionMethodReturnsTrue() {
5251
evaluateCondition();
5352
assertEnabled();
54-
assertReasonContains("Condition provided in @EnabledIf evaluates to true");
53+
assertReasonContains("""
54+
@EnabledIf("staticMethodThatReturnsTrue") evaluated to true""");
5555
}
5656

5757
/**
@@ -71,7 +71,8 @@ void disabledBecauseStaticConditionMethodReturnsFalse() {
7171
void enabledBecauseConditionMethodReturnsTrue() {
7272
evaluateCondition();
7373
assertEnabled();
74-
assertReasonContains("Condition provided in @EnabledIf evaluates to true");
74+
assertReasonContains("""
75+
@EnabledIf("methodThatReturnsTrue") evaluated to true""");
7576
}
7677

7778
/**
@@ -81,32 +82,31 @@ void enabledBecauseConditionMethodReturnsTrue() {
8182
void disabledBecauseConditionMethodReturnsFalse() {
8283
evaluateCondition();
8384
assertDisabled();
84-
assertReasonContains("Condition provided in @EnabledIf evaluates to false");
85+
assertReasonContains("""
86+
@EnabledIf("methodThatReturnsFalse") evaluated to false""");
8587
}
8688

87-
@Nested
88-
class ExternalConditionMethod {
89-
90-
/**
91-
* @see EnabledIfIntegrationTests.ExternalConditionMethod#enabledBecauseConditionMethodReturnsTrue()
92-
*/
93-
@Test
94-
void enabledBecauseConditionMethodReturnsTrue() {
95-
evaluateCondition();
96-
assertEnabled();
97-
assertReasonContains("Condition provided in @EnabledIf evaluates to true");
98-
}
99-
100-
/**
101-
* @see EnabledIfIntegrationTests.ExternalConditionMethod#disabledBecauseConditionMethodReturnsFalse()
102-
*/
103-
@Test
104-
void disabledBecauseConditionMethodReturnsFalse() {
105-
evaluateCondition();
106-
assertDisabled();
107-
assertReasonContains("Condition provided in @EnabledIf evaluates to false");
108-
}
89+
/**
90+
* @see EnabledIfIntegrationTests.ExternalConditionMethod#enabledBecauseStaticExternalConditionMethodReturnsTrue()
91+
*/
92+
@Test
93+
void enabledBecauseStaticExternalConditionMethodReturnsTrue() {
94+
evaluateCondition();
95+
assertEnabled();
96+
assertReasonContains("""
97+
@EnabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsTrue") evaluated to true""");
98+
}
10999

100+
/**
101+
* @see EnabledIfIntegrationTests.ExternalConditionMethod#disabledBecauseStaticExternalConditionMethodReturnsFalse()
102+
*/
103+
@Test
104+
void disabledBecauseStaticExternalConditionMethodReturnsFalse() {
105+
evaluateCondition();
106+
assertDisabled();
107+
assertReasonContains(
108+
"""
109+
@EnabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse") evaluated to false""");
110110
}
111111

112112
}

junit-jupiter-engine/src/test/java/org/junit/jupiter/api/condition/EnabledIfIntegrationTests.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,23 @@ void disabledBecauseConditionMethodReturnsFalse() {
5050
fail("Should be disabled");
5151
}
5252

53-
@Nested
54-
class ExternalConditionMethod {
55-
56-
@Test
57-
@EnabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsTrue")
58-
void enabledBecauseConditionMethodReturnsTrue() {
59-
}
60-
61-
@Test
62-
@EnabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse")
63-
void disabledBecauseConditionMethodReturnsFalse() {
64-
fail("Should be disabled");
65-
}
53+
@Test
54+
@EnabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsTrue")
55+
void enabledBecauseStaticExternalConditionMethodReturnsTrue() {
56+
}
6657

58+
@Test
59+
@EnabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse")
60+
void disabledBecauseStaticExternalConditionMethodReturnsFalse() {
61+
fail("Should be disabled");
6762
}
6863

6964
@Nested
7065
@EnabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse")
7166
class ConditionallyDisabledClass {
7267

7368
@Test
74-
void disabledBecauseConditionMethodReturnsTrue() {
69+
void disabledBecauseConditionMethodReturnsFalse() {
7570
fail("Should be disabled");
7671
}
7772

0 commit comments

Comments
 (0)