[Solved] java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter

Today, I want to write an essay. Recently, I often report java.lang.noclassdeffounderror when using JUnit. Today, I suddenly realized that although JUnit is configured in gradle, I can see JUnit’s jar package in project and external dependencies, and I can find the class org/JUnit/Runner/manipulation/filter in JUnit’s jar package, However, when running as JUnit test, you should report Java. Lang. NoClassDefFoundError: org/JUnit/Runner/manipulation/filter

I think it’s a gradle configuration problem, but testimplementation, implementation and API are not good

Later on, I think that the jar package introduced in gradle (that is, the jar package in project and external dependencies) was not loaded by the JVM during run as JUnit test. That’s why this phenomenon appears. The solution is to add library and JUnit in build path

The loaded class in main is attached below

package proxy;

import java.lang.reflect.Field;
import java.lang.reflect.Proxy;
import java.util.Vector;

import org.junit.Test;


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("");
        }
	}
}

Similar Posts: