zoukankan      html  css  js  c++  java
  • java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter

      今天想写个随笔,最近经常遇到使用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 136187300@qq.com 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("");
            }
    	}
    }
    

      

      

  • 相关阅读:
    CentOS中JAVA_HOME的环境变量设置
    Macserver服务更新经常使用的几个shell命令
    一个技术派创业者的反思
    巴斯卡三角形
    iOS中基于 Socket 的 C/S 结构网络通信(中)
    poj 3267 The Cow Lexicon (动态规划)
    Android入门:短信和拨打电话
    HDUOJ--4888--Redraw Beautiful Drawings【isap】网络流+判环
    Dynamics CRM 2015 New Feature (9): Services Changes
    Class 找出一个整形数组中的元素的最大值
  • 原文地址:https://www.cnblogs.com/xiaodebing/p/9164675.html
Copyright © 2011-2022 走看看