java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter
阿新 • • 發佈:2018-06-11
就是 bsp intercept ssi != stack ktr tex ins
今天想寫個隨筆,最近經常遇到使用junit的時候報java.lang.NoClassDefFoundError,今天算是恍然大悟了,原來junit雖然在gradle裏面配置了,也在Project and External Dependencies中看到了junit的jar包,並能在這個junit的jar包裏面找到org/junit/runner/manipulation/Filter這個類,但是run as junit test的時候就偏偏要報java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter。
以為是gradle配置問題,testImplementation、implementation、api都不行
後來想想,出現這種情況無外乎gradle中引入的jar包(即Project and External Dependencies中的jar包)在run as junit test的時候並沒有被jvm加載,所以才會出現這種現象,解決辦法就是在build path 中add library,加入junit
下面附上在main裏面打出已加載的class:
package proxy; import java.lang.reflect.Field; import java.lang.reflect.Proxy; import java.util.Vector; import org.junit.Test; /** * Created by [email protected] on 2018年6月9日. */ public class TestProxy { @Test public void test1() { TestLog testLog = new TestLogImpl(); TestLogInterceptor testLogInterceptor = new TestLogInterceptor(); testLogInterceptor.setTarget(testLog); TestLog proxy = (TestLog)Proxy.newProxyInstance(testLog.getClass().getClassLoader() , testLog.getClass().getInterfaces(), testLogInterceptor); proxy.print(); } public static void main(String[] args) { TestLog testLog = new TestLogImpl(); TestLogInterceptor testLogInterceptor = new TestLogInterceptor(); testLogInterceptor.setTarget(testLog); TestLog proxy = (TestLog)Proxy.newProxyInstance(testLog.getClass().getClassLoader() , testLog.getClass().getInterfaces(), testLogInterceptor); proxy.print(); try { new TestProxy().printClass(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void printClass() throws Exception { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Class cla = classLoader.getClass(); while (cla != ClassLoader.class) cla = cla.getSuperclass(); Field field = cla.getDeclaredField("classes"); field.setAccessible(true); Vector v = (Vector) field.get(classLoader); for (int i = 0; i < v.size(); i++) { System.out.print(((Class)v.get(i)).getName()+","); if(i%10 == 0)System.out.println(""); } } }
java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter