1 package top.infra.test.classloader;
2
3 import org.junit.runner.Description;
4 import org.junit.runner.Runner;
5 import org.junit.runner.notification.RunNotifier;
6 import org.junit.runners.BlockJUnit4ClassRunner;
7
8
9
10
11
12 public class ClassLoaderChangerRunner extends Runner {
13
14 private final ClassLoader currentClassLoader;
15 private final ClassLoader customClassLoader;
16 private final BlockJUnit4ClassRunner delegate;
17
18 public ClassLoaderChangerRunner(
19 final ClassLoader currentClassLoader,
20 final ClassLoader customClassLoader,
21 final BlockJUnit4ClassRunner delegate
22 ) {
23 this.currentClassLoader = currentClassLoader;
24 this.customClassLoader = customClassLoader;
25 this.delegate = delegate;
26 }
27
28 @Override
29 public Description getDescription() {
30 return this.delegate.getDescription();
31 }
32
33 @Override
34 public void run(final RunNotifier runNotifier) {
35 try {
36 Thread.currentThread().setContextClassLoader(this.customClassLoader);
37 this.delegate.run(runNotifier);
38 } finally {
39 Thread.currentThread().setContextClassLoader(this.currentClassLoader);
40 }
41 }
42 }