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:
- java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
- junitjava.lang.Exception: No tests found matching
- The jdbc driver imported from idea into MySQL appears “Java. Lang. classnotfoundexception: com. Mysql. CJ. JDBC. Driver”
- [Solved] Mybatis Codes Error: java.lang.ClassNotFoundException: Cannot find class: com.mysql.jdbc.Driver
- [How to Solve] Hive: Unsupported major.minor version 51.0
- [Solved] Cannot resolve symbol ‘SpringJUnit4ClassRunner’
- [Java. Lang. unsupported classversionerror] version inconsistency error
- signer information does not match signer information of other classes in the same package
- [Solved] org.hibernate.MappingException: Unknown entity: com.huangliusong.entity.Person
- [Solved] Hadoop running jar package error: xception in thread “main” java.lang.ClassNotFoundException: Filter