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

  

  

java.lang.NoClassDefFoundError: org/junit/runner/manipulation/Filter的更多相关文章

  1. 就没有我遇不到的报错!java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/filter/Filter

    本来准备用HBase的Bulkload将HDFS的HFile文件导入到HBase的myuser2表中,用的是yarn jar的命令 yarn jar /export/servers/hbase-1.2 ...

  2. java.lang.NoClassDefFoundError: org/junit/rules/TestRule

    错误原因:通过定位发现是找不到TestRule这个类,检查项目引用的Junit版本为4.7,发现TestRule是在Junit版本4.10后添加的新特性 解决方法:把junit版本由4.7改成4.10

  3. 【Junit】JUnit-4.12使用报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing错误

    下载了最新的JUnit版本,是4.12,结果尝试使用发现总是报java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing这样的错误, 上网查 ...

  4. 【JUnit 报错】java.lang.NoClassDefFoundError: org/apache/logging/log4j/message/Message

    使用JUnit的时候,报错:java.lang.NoClassDefFoundError: org/apache/logging/log4j/message/Message 原因是因为项目中导入的架包 ...

  5. Exception starting filter struts2 java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor

    按教程,使用Convention插件进行配置 教程中说只要加入struts2-convention-plugin-2.3.4.1.jar这个jar包就可以使用. 按照这种方法部署后,启动tomcat报 ...

  6. SSH整合后tomcat启动报错SEVERE: Exception starting filter struts2 java.lang.NoClassDefFoundError: org/objectweb/asm/ClassVisitor

    错误信息:  SEVERE: Exception starting filter struts2 java.lang.NoClassDefFoundError: org/objectweb/asm/C ...

  7. HTTP Status 500 - javax.servlet.ServletException: java.lang.NoClassDefFoundError: junit/framework/Test解决方法

    java代码 package webViewer; import java.io.*; import junit.framework.Test; import com.aspose.words.*; ...

  8. Java AOP nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice || Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#0' 两个异常解决办法

    贴出applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  9. 异常:Caused by: java.lang.NoClassDefFoundError: Could not initialize class net.sf.log4jdbc.Properties

    参考文章: 使用Log4jdbc-log4j2监听MyBatis中运行的SQL和Connection 使用 log4jdbc格式化输出SQL,maven配置如下: <dependency> ...

随机推荐

  1. [Intellij IDEA]_eclipse项目导入

    http://www.cnblogs.com/lindp/p/4484390.html

  2. POI (Apache POI)

    Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 基本功能 编辑 结构: HSSF - 提供读写Mi ...

  3. PAT 1007 素数对猜想

    https://pintia.cn/problem-sets/994805260223102976/problems/994805317546655744 让我们定义 d~n~ 为:d~n~ = p~ ...

  4. liuyan

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 临时关闭Mysql ONLY_FULL_GROUP_BY

    /** * @author lcc807@ikoo8.com * * 临时关闭Mysql ONLY_FULL_GROUP_BY */ function closeSqlFullMode(){ DB:: ...

  6. iptables防火墙配置

    iptables防火墙配置 一.防火墙简介 1.功能: 1)通过源端口,源IP地址,源MAC地址,包中特定标记和目标端口,IP,MAC来确定数据包是否可以通过防火墙 2)分割内网和外网[附带的路由器的 ...

  7. 线性代数的本质与几何意义 03. 矩阵与线性变换 (3blue1brown 咪博士 图文注解版)

    首先,恭喜你读到了咪博士的这篇文章.本文可以说是该系列最重要.最核心的文章.你对线性代数的一切困惑,根源就在于没有真正理解矩阵到底是什么.读完咪博士的这篇文章,你一定会有一种醍醐灌顶.豁然开朗的感觉! ...

  8. Java微信二次开发(三)

    各种类型消息的封装 第一步:找到com.wtz.message.response包,新建类Image.java package com.wtz.message.response; /** * @aut ...

  9. html 文档类型

    <!doctype>用来声明html的版本,浏览器只有知道html的版本后才能正确显示文档,<!DOCTYPE>本身不是一个标签,而是一个声明.

  10. 删除文件,copy文件bat

    由于IntelliJ IDEA Build Artifacts速度实在是无法忍受,特整理了一个bat脚本方便maven install后更新jar文件到部署包 del /F /S /Q D:\Idea ...