Skip to content

Commit 8bfe25a

Browse files
committed
Test @EnabledIf and @DisabledIf with custom ClassLoader arrangement
See #3292 See #3280
1 parent cd1b3b0 commit 8bfe25a

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext con
5757
.orElseGet(this::enabledByDefault);
5858
}
5959

60-
private Method getConditionMethod(String fullyQualifiedMethodName, ExtensionContext context) {
60+
// package-private for testing
61+
Method getConditionMethod(String fullyQualifiedMethodName, ExtensionContext context) {
6162
Class<?> testClass = context.getRequiredTestClass();
6263
if (!fullyQualifiedMethodName.contains("#")) {
6364
return findMethod(testClass, fullyQualifiedMethodName);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2015-2023 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+
import static org.mockito.Mockito.doReturn;
15+
import static org.mockito.Mockito.mock;
16+
import static org.mockito.Mockito.when;
17+
18+
import java.lang.reflect.Method;
19+
import java.util.Optional;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
23+
import org.junit.jupiter.api.extension.ExtensionContext;
24+
import org.junit.platform.commons.test.TestClassLoader;
25+
import org.junit.platform.commons.util.ReflectionUtils;
26+
27+
/**
28+
* Tests for {@link DisabledIfCondition} using custom {@link ClassLoader} arrangements.
29+
*
30+
* @since 5.10
31+
*/
32+
public class DisabledIfConditionClassLoaderTests {
33+
34+
@Test
35+
// No need to introduce a "disabled" version of this test, since it would simply be the
36+
// logical inverse of this method and would therefore not provide any further benefit.
37+
void enabledWithStaticMethodInTypeFromDifferentClassLoader() throws Exception {
38+
try (var testClassLoader = TestClassLoader.forClasses(getClass(), StaticConditionMethods.class)) {
39+
var testClass = testClassLoader.loadClass(getClass().getName());
40+
assertThat(testClass.getClassLoader()).isSameAs(testClassLoader);
41+
42+
ExtensionContext context = mock();
43+
Method annotatedMethod = ReflectionUtils.findMethod(getClass(), "enabledMethod").get();
44+
when(context.getElement()).thenReturn(Optional.of(annotatedMethod));
45+
doReturn(testClass).when(context).getRequiredTestClass();
46+
47+
DisabledIfCondition condition = new DisabledIfCondition();
48+
ConditionEvaluationResult result = condition.evaluateExecutionCondition(context);
49+
assertThat(result).isNotNull();
50+
assertThat(result.isDisabled()).isFalse();
51+
52+
Method conditionMethod = condition.getConditionMethod(
53+
"org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse", context);
54+
assertThat(conditionMethod).isNotNull();
55+
Class<?> declaringClass = conditionMethod.getDeclaringClass();
56+
assertThat(declaringClass.getClassLoader()).isSameAs(testClassLoader);
57+
assertThat(declaringClass.getName()).isEqualTo(StaticConditionMethods.class.getName());
58+
assertThat(declaringClass).isNotEqualTo(StaticConditionMethods.class);
59+
}
60+
}
61+
62+
@DisabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsFalse")
63+
private void enabledMethod() {
64+
}
65+
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2015-2023 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+
import static org.mockito.Mockito.doReturn;
15+
import static org.mockito.Mockito.mock;
16+
import static org.mockito.Mockito.when;
17+
18+
import java.lang.reflect.Method;
19+
import java.util.Optional;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
23+
import org.junit.jupiter.api.extension.ExtensionContext;
24+
import org.junit.platform.commons.test.TestClassLoader;
25+
import org.junit.platform.commons.util.ReflectionUtils;
26+
27+
/**
28+
* Tests for {@link EnabledIfCondition} using custom {@link ClassLoader} arrangements.
29+
*
30+
* @since 5.10
31+
*/
32+
public class EnabledIfConditionClassLoaderTests {
33+
34+
@Test
35+
// No need to introduce a "disabled" version of this test, since it would simply be the
36+
// logical inverse of this method and would therefore not provide any further benefit.
37+
void enabledWithStaticMethodInTypeFromDifferentClassLoader() throws Exception {
38+
try (var testClassLoader = TestClassLoader.forClasses(getClass(), StaticConditionMethods.class)) {
39+
var testClass = testClassLoader.loadClass(getClass().getName());
40+
assertThat(testClass.getClassLoader()).isSameAs(testClassLoader);
41+
42+
ExtensionContext context = mock();
43+
Method annotatedMethod = ReflectionUtils.findMethod(getClass(), "enabledMethod").get();
44+
when(context.getElement()).thenReturn(Optional.of(annotatedMethod));
45+
doReturn(testClass).when(context).getRequiredTestClass();
46+
47+
EnabledIfCondition condition = new EnabledIfCondition();
48+
ConditionEvaluationResult result = condition.evaluateExecutionCondition(context);
49+
assertThat(result).isNotNull();
50+
assertThat(result.isDisabled()).isFalse();
51+
52+
Method conditionMethod = condition.getConditionMethod(
53+
"org.junit.jupiter.api.condition.StaticConditionMethods#returnsTrue", context);
54+
assertThat(conditionMethod).isNotNull();
55+
Class<?> declaringClass = conditionMethod.getDeclaringClass();
56+
assertThat(declaringClass.getClassLoader()).isSameAs(testClassLoader);
57+
assertThat(declaringClass.getName()).isEqualTo(StaticConditionMethods.class.getName());
58+
assertThat(declaringClass).isNotEqualTo(StaticConditionMethods.class);
59+
}
60+
}
61+
62+
@EnabledIf("org.junit.jupiter.api.condition.StaticConditionMethods#returnsTrue")
63+
private void enabledMethod() {
64+
}
65+
66+
}

0 commit comments

Comments
 (0)