今天想写个随笔,最近经常遇到使用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. Leetcode 712. 两个字符串的最小ASCII删除和

    题目描述: https://leetcode-cn.com/problems/minimum-ascii-delete-sum-for-two-strings/ 解题思路: 也是典型的dp问题.利用二 ...

  2. [2017BUAA软件工程]第0次个人作业

    第一部分: 结缘计算机 1. 你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 有时候我也问自己这个问题,是因为认识的人中有人从事这个工作并且做得很好而产生了艳羡?是因为家长一次次催逼,想 ...

  3. [转帖]2016年时的新闻:ASP.NET Core 1.0、ASP.NET MVC Core 1.0和Entity Framework Core 1.0

    ASP.NET Core 1.0.ASP.NET MVC Core 1.0和Entity Framework Core 1.0 http://www.cnblogs.com/webapi/p/5673 ...

  4. Jfrog 与 jenkins Sonarqube的 测试样例 (From jfrog 培训)

    1. 获取jfrog 仓库地址 sonar的地址 jenkins的地址. 本次例子的地址为: jenkins地址:http://10.24.101.99:8080/ artifactory地址: ht ...

  5. pandas函数应用

    1.管道函数 #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/5/24 15:03 # @Author : zhang cha ...

  6. Visual Studio 2017各版本安装包离线下载、安装全解析

    关于Visual Studio 2017各版本安装包离线下载.更新和安装的方法以及通过已下载版本减少下载量的办法 微软最近发布了正式版Visual Studio 2017并公开了其下载方式,不过由于V ...

  7. 【Linux】Centos7 解压zip文件

    如果输入unzip无反应那么请安装相应软件包 yum install -y unzip 语法 unzip [参数] [文件] 参数 -c:将解压缩的结果显示到屏幕上,并对字符做适当的转换: -f:更新 ...

  8. CF-Contest339-614

    614A-Link/Cut Tree 比较水,注意64位int仍然可能溢出. #include <cstdio> #include <algorithm> #include & ...

  9. java BigDecimal加减乘除 与 保留两位小数

    BigDecimal bignum1 = new BigDecimal("10"); BigDecimal bignum2 = new BigDecimal("5&quo ...

  10. 【BZOJ2285】[SDOI2011]保密(分数规划,网络流)

    [BZOJ2285][SDOI2011]保密(分数规划,网络流) 题面 BZOJ 洛谷 题解 首先先读懂题目到底在干什么. 发现要求的是一个比值的最小值,二分这个最小值\(k\),把边权转换成\(t- ...