Skip to content

Commit 3026c62

Browse files
committed
Test status quo for ConditionEvaluationResult and its factory methods
Prior to this commit, we did not have any "unit tests" for ConditionEvaluationResult, and while working on #4698 I noticed that we in fact have several issues in the implementation of and documentation for ConditionEvaluationResult. Thus, this commit introduces dedicated unit tests for the status quo, where individual TODOs will be addressed in separate issues/commits. See #4698 See #4699 (comment)
1 parent 2a52a06 commit 3026c62

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.jupiter.api.condition;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
import java.lang.annotation.Retention;
16+
import java.lang.annotation.RetentionPolicy;
17+
18+
import org.jspecify.annotations.Nullable;
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.NullSource;
23+
import org.junit.jupiter.params.provider.ValueSource;
24+
25+
/**
26+
* Unit tests for {@link ConditionEvaluationResult}.
27+
*
28+
* @since 5.13.3
29+
*/
30+
class ConditionEvaluationResultTests {
31+
32+
@Test
33+
void enabledWithReason() {
34+
var result = ConditionEvaluationResult.enabled("reason");
35+
36+
assertThat(result.isDisabled()).isFalse();
37+
assertThat(result.getReason()).contains("reason");
38+
assertThat(result).asString()//
39+
.isEqualTo("ConditionEvaluationResult [enabled = true, reason = 'reason']");
40+
}
41+
42+
@EmptyReasonsTest
43+
void enabledWithInvalidReason(@Nullable String reason) {
44+
@SuppressWarnings("NullAway")
45+
var result = ConditionEvaluationResult.enabled(reason);
46+
47+
assertThat(result.isDisabled()).isFalse();
48+
49+
if (reason == null) {
50+
assertThat(result.getReason()).isEmpty();
51+
assertThat(result).asString()//
52+
.isEqualTo("ConditionEvaluationResult [enabled = true, reason = '<unknown>']");
53+
}
54+
// TODO Remove else-block once issues are addressed.
55+
else {
56+
assertThat(result.getReason()).contains(reason);
57+
assertThat(result).asString()//
58+
.isEqualTo("ConditionEvaluationResult [enabled = true, reason = '%s']", reason);
59+
}
60+
}
61+
62+
@Test
63+
void disabledWithDefaultReason() {
64+
var result = ConditionEvaluationResult.disabled("default");
65+
66+
assertThat(result.isDisabled()).isTrue();
67+
assertThat(result.getReason()).contains("default");
68+
assertThat(result).asString()//
69+
.isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'default']");
70+
}
71+
72+
@EmptyReasonsTest
73+
void disabledWithInvalidDefaultReason(@Nullable String reason) {
74+
@SuppressWarnings("NullAway")
75+
var result = ConditionEvaluationResult.disabled(reason);
76+
77+
assertThat(result.isDisabled()).isTrue();
78+
79+
if (reason == null) {
80+
assertThat(result.getReason()).isEmpty();
81+
assertThat(result).asString()//
82+
.isEqualTo("ConditionEvaluationResult [enabled = false, reason = '<unknown>']");
83+
}
84+
// TODO Remove else-block once issues are addressed.
85+
else {
86+
assertThat(result.getReason()).contains(reason);
87+
assertThat(result).asString()//
88+
.isEqualTo("ConditionEvaluationResult [enabled = false, reason = '%s']", reason);
89+
}
90+
}
91+
92+
@EmptyReasonsTest
93+
void disabledWithValidDefaultReasonAndInvalidCustomReason(@Nullable String customReason) {
94+
@SuppressWarnings("NullAway")
95+
var result = ConditionEvaluationResult.disabled("default", customReason);
96+
97+
assertThat(result.isDisabled()).isTrue();
98+
assertThat(result.getReason()).contains("default");
99+
assertThat(result).asString()//
100+
.isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'default']");
101+
}
102+
103+
@EmptyReasonsTest
104+
void disabledWithInvalidDefaultReasonAndValidCustomReason(@Nullable String reason) {
105+
@SuppressWarnings("NullAway")
106+
var result = ConditionEvaluationResult.disabled(reason, "custom");
107+
108+
assertThat(result.isDisabled()).isTrue();
109+
110+
// TODO Convert to single assertion once issues are addressed.
111+
// The following should hold for all null/blank default reasons.
112+
// assertThat(result).asString().isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'custom']");
113+
114+
if (reason == null) {
115+
assertThat(result.getReason()).contains("null ==> custom");
116+
assertThat(result).asString()//
117+
.isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'null ==> custom']");
118+
}
119+
else {
120+
var generatedReason = reason + " ==> custom";
121+
assertThat(result.getReason()).contains(generatedReason);
122+
assertThat(result).asString()//
123+
.isEqualTo("ConditionEvaluationResult [enabled = false, reason = '%s']", generatedReason);
124+
}
125+
}
126+
127+
@EmptyReasonsTest
128+
void disabledWithInvalidDefaultReasonAndInvalidCustomReason(@Nullable String reason) {
129+
// We intentionally use the reason as both the default and custom reason.
130+
@SuppressWarnings("NullAway")
131+
var result = ConditionEvaluationResult.disabled(reason, reason);
132+
133+
assertThat(result.isDisabled()).isTrue();
134+
135+
if (reason == null) {
136+
assertThat(result.getReason()).isEmpty();
137+
assertThat(result).asString()//
138+
.isEqualTo("ConditionEvaluationResult [enabled = false, reason = '<unknown>']");
139+
}
140+
// TODO Remove else-block once issues are addressed.
141+
else {
142+
assertThat(result.getReason()).contains(reason);
143+
assertThat(result).asString()//
144+
.isEqualTo("ConditionEvaluationResult [enabled = false, reason = '%s']", reason);
145+
}
146+
}
147+
148+
@Test
149+
void disabledWithValidDefaultReasonAndCustomReason() {
150+
var result = ConditionEvaluationResult.disabled("default", "custom");
151+
152+
assertThat(result.isDisabled()).isTrue();
153+
assertThat(result.getReason()).contains("default ==> custom");
154+
assertThat(result).asString()//
155+
.isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'default ==> custom']");
156+
}
157+
158+
@Retention(RetentionPolicy.RUNTIME)
159+
@ParameterizedTest
160+
@NullSource
161+
@ValueSource(strings = { "", " ", " ", "\t", "\n" })
162+
@interface EmptyReasonsTest {
163+
}
164+
165+
}

0 commit comments

Comments
 (0)