Skip to content

Commit e04ace8

Browse files
committed
Fix LinkageError with ExpectedException and FilteredClassPathRunner
Previously, when the ExpectedException JUnit rule was used with FilteredClassPathRunner a LinkageError would occur if any of ExpectedException's methods that take a Hamcrest Matcher were called. This was due to the FilteredClassLoader delegating loading of org.junit classes to its parent but not org.hamcrest classes. This resulted in JUnit classes loading one version of the Hamcrest class and the test class loading another. This commit ensures that both the JUnit classes and the test class use the same version of Hamcrest classes by also delegating the loading of org.hamcrest classes to FilteredClassLoader's parent.
1 parent 9a9c4c7 commit e04ace8

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

spring-boot/src/test/java/org/springframework/boot/testutil/FilteredClassPathRunner.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -264,7 +264,7 @@ private static final class FilteredClassLoader extends URLClassLoader {
264264

265265
@Override
266266
public Class<?> loadClass(String name) throws ClassNotFoundException {
267-
if (name.startsWith("org.junit")) {
267+
if (name.startsWith("org.junit") || name.startsWith("org.hamcrest")) {
268268
return this.junitLoader.loadClass(name);
269269
}
270270
return super.loadClass(name);

spring-boot/src/test/java/org/springframework/boot/testutil/FilteredClassPathRunnerTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,10 +16,13 @@
1616

1717
package org.springframework.boot.testutil;
1818

19+
import org.junit.Rule;
1920
import org.junit.Test;
21+
import org.junit.rules.ExpectedException;
2022
import org.junit.runner.RunWith;
2123

2224
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.hamcrest.CoreMatchers.isA;
2326

2427
/**
2528
* Tests for {@link FilteredClassPathRunner}
@@ -33,6 +36,9 @@ public class FilteredClassPathRunnerTests {
3336
private static final String EXCLUDED_RESOURCE = "META-INF/services/"
3437
+ "javax.validation.spi.ValidationProvider";
3538

39+
@Rule
40+
public ExpectedException thrown = ExpectedException.none();
41+
3642
@Test
3743
public void entriesAreFilteredFromTestClassClassLoader() {
3844
assertThat(getClass().getClassLoader().getResource(EXCLUDED_RESOURCE)).isNull();
@@ -44,4 +50,10 @@ public void entriesAreFilteredFromThreadContextClassLoader() {
4450
.getResource(EXCLUDED_RESOURCE)).isNull();
4551
}
4652

53+
@Test
54+
public void testsThatUseHamcrestWorkCorrectly() {
55+
this.thrown.expect(isA(IllegalStateException.class));
56+
throw new IllegalStateException();
57+
}
58+
4759
}

0 commit comments

Comments
 (0)