eclipse 运行juni 测试 ClassNotFoundException

本文解决了在 Eclipse 中使用 Maven 进行 JUnit 测试时遇到的 ClassNotFound 异常问题。通过在 Maven 命令中添加 test-compile 参数来确保测试类能够被正确编译。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

down vote

在eclipse 运行 基于maven的Junit test:

Class not found com.myproject.server.MyTestjava.lang.ClassNotFoundException: com.myproject.server.MyTest at java.net.URLClassLoader$1.run(URLClassLoader.java:366) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)

I've tried adding JUNIT library in the classpath although I'm using maven, and the Junit library is in the POM dependency.

I already have tried cleaning the project and created a new Junit test case using the Junit plugin for Eclipse, still getting the same error.

 

解决方法: 改为命令mvn clean compile test-compile

下面是原因:

I realize this is a bit belated of a response, but I was running into the same issue and thought I would contribute the solution I found.

This appears to occur because only the source code is compiling when you use "mvn clean compile" (I'm using maven 3.1.0 so I'm not sure if it always behaved like this). If you run "mvn test", the test code will compile as well, but then it runs the tests (which may not be immediately desirable if you're trying to run them through Eclipse. The way around this is to add test-compile to your mvn command sequence whenever you do a "mvn clean". For example, you would run "mvn clean compile test-compile".

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值