Skip to content

Commit 882e33b

Browse files
committed
Polish TestClassLoader usage
1 parent c184dbc commit 882e33b

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertThrowsAssertionsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,9 @@ void assertThrowsWithExecutableThatThrowsInstanceOfStaticNestedClassAsUnexpected
241241
@Test
242242
@SuppressWarnings("unchecked")
243243
void assertThrowsWithExecutableThatThrowsSameExceptionTypeFromDifferentClassLoader() throws Exception {
244-
try (TestClassLoader enigmaClassLoader = TestClassLoader.forClasses(EnigmaThrowable.class)) {
244+
try (var testClassLoader = TestClassLoader.forClasses(EnigmaThrowable.class)) {
245245
// Load expected exception type from different class loader
246-
Class<? extends Throwable> enigmaThrowableClass = (Class<? extends Throwable>) enigmaClassLoader.loadClass(
246+
Class<? extends Throwable> enigmaThrowableClass = (Class<? extends Throwable>) testClassLoader.loadClass(
247247
EnigmaThrowable.class.getName());
248248

249249
try {

junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertThrowsExactlyAssertionsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ void assertThrowsWithExecutableThatThrowsInstanceOfStaticNestedClassAsUnexpected
281281
@Test
282282
@SuppressWarnings("unchecked")
283283
void assertThrowsWithExecutableThatThrowsSameExceptionTypeFromDifferentClassLoader() throws Exception {
284-
try (TestClassLoader enigmaClassLoader = TestClassLoader.forClasses(EnigmaThrowable.class)) {
284+
try (var testClassLoader = TestClassLoader.forClasses(EnigmaThrowable.class)) {
285285
// Load expected exception type from different class loader
286-
Class<? extends Throwable> enigmaThrowableClass = (Class<? extends Throwable>) enigmaClassLoader.loadClass(
286+
Class<? extends Throwable> enigmaThrowableClass = (Class<? extends Throwable>) testClassLoader.loadClass(
287287
EnigmaThrowable.class.getName());
288288

289289
try {

junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/extension/TestInstanceFactoryTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -692,13 +692,12 @@ private static class ProxyTestInstanceFactory implements TestInstanceFactory {
692692
public Object createTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext extensionContext) {
693693
Class<?> testClass = factoryContext.getTestClass();
694694
String className = testClass.getName();
695-
String prefix = TestInstanceFactoryTests.class.getName();
696695

697696
instantiated(getClass(), testClass);
698697

699-
try (TestClassLoader proxyClassLoader = TestClassLoader.forClassNamePrefix(prefix)) {
698+
try (var testClassLoader = TestClassLoader.forClasses(testClass)) {
700699
// Load test class from different class loader
701-
Class<?> clazz = proxyClassLoader.loadClass(className);
700+
Class<?> clazz = testClassLoader.loadClass(className);
702701
return ReflectionUtils.newInstance(clazz);
703702
}
704703
catch (Exception ex) {

junit-jupiter-params/src/test/java/org/junit/jupiter/params/converter/DefaultArgumentConverterTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,17 @@ void convertsStringToClass() {
211211
@Test
212212
void convertsStringToClassWithCustomTypeFromDifferentClassLoader() throws Exception {
213213
String customTypeName = Enigma.class.getName();
214-
try (TestClassLoader customTypeClassLoader = TestClassLoader.forClasses(Enigma.class)) {
215-
var customType = customTypeClassLoader.loadClass(customTypeName);
216-
assertThat(customType.getClassLoader()).isInstanceOf(TestClassLoader.class);
214+
try (var testClassLoader = TestClassLoader.forClasses(Enigma.class)) {
215+
var customType = testClassLoader.loadClass(customTypeName);
216+
assertThat(customType.getClassLoader()).isSameAs(testClassLoader);
217217

218218
var declaringExecutable = ReflectionUtils.findMethod(customType, "foo").get();
219-
assertThat(declaringExecutable.getDeclaringClass().getClassLoader()).isInstanceOf(TestClassLoader.class);
219+
assertThat(declaringExecutable.getDeclaringClass().getClassLoader()).isSameAs(testClassLoader);
220220

221221
var clazz = (Class<?>) convert(customTypeName, Class.class, parameterContext(declaringExecutable));
222222
assertThat(clazz).isNotEqualTo(Enigma.class);
223223
assertThat(clazz).isEqualTo(customType);
224-
assertThat(clazz.getClassLoader()).isInstanceOf(TestClassLoader.class);
224+
assertThat(clazz.getClassLoader()).isSameAs(testClassLoader);
225225
}
226226
}
227227

platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,24 +1034,24 @@ void findMethodByParameterNamesWithWithCustomTypeFromDifferentClassLoader() thro
10341034
var customTypeName = CustomType.class.getName();
10351035
var nestedTypeName = CustomType.NestedType.class.getName();
10361036

1037-
try (TestClassLoader customTypeClassLoader = TestClassLoader.forClassNamePrefix(customTypeName)) {
1038-
var customType = customTypeClassLoader.loadClass(customTypeName);
1039-
assertThat(customType.getClassLoader()).isEqualTo(customTypeClassLoader);
1037+
try (var testClassLoader = TestClassLoader.forClassNamePrefix(customTypeName)) {
1038+
var customType = testClassLoader.loadClass(customTypeName);
1039+
assertThat(customType.getClassLoader()).isSameAs(testClassLoader);
10401040

10411041
var optional = findMethod(customType, methodName, nestedTypeName);
10421042
assertThat(optional).get().satisfies(method -> {
10431043
assertThat(method.getName()).isEqualTo(methodName);
10441044

10451045
var declaringClass = method.getDeclaringClass();
1046-
assertThat(declaringClass.getClassLoader()).isEqualTo(customTypeClassLoader);
1046+
assertThat(declaringClass.getClassLoader()).isSameAs(testClassLoader);
10471047
assertThat(declaringClass.getName()).isEqualTo(customTypeName);
10481048
assertThat(declaringClass).isNotEqualTo(CustomType.class);
10491049

10501050
var parameterTypes = method.getParameterTypes();
10511051
assertThat(parameterTypes).extracting(Class::getName).containsExactly(nestedTypeName);
10521052
Class<?> parameterType = parameterTypes[0];
10531053
assertThat(parameterType).isNotEqualTo(CustomType.NestedType.class);
1054-
assertThat(parameterType.getClassLoader()).isEqualTo(customTypeClassLoader);
1054+
assertThat(parameterType.getClassLoader()).isSameAs(testClassLoader);
10551055
});
10561056
}
10571057
}

0 commit comments

Comments
 (0)