我需要在指定的目录中加载一些测试类来运行它们包含的junit测试
似乎我在方法Class.forName()
和URLClassLoader
。
我在路径为
的文件夹中有一些Junit测试类
C:\Documents\Java\Project\src\tests\net\sourceforge
和编译的类
C:\Documents\Java\Project\target\tests-classes\net\sourceforge
运行Class
我需要JunitCore.run()
的实例,但当我尝试使用Class.forName()
方法时,我会得到NoClassDefFoundException
或ClassNotFoundException
。
我目前所做的是迭代target\tests-classes\
中的所有文件并尝试加载这些类。
目录树结构类似于
SOURCEFILES
- Java
- Project
- src
- tests
- net
- sorceforge
- Test1.java
- Test2.java
- directory1
- Test3.java
- Test4.java
- directory2
- Test5.java
BINARIES
- Java
- Project
- target
- tests-classes
- net
- sorceforge
- Test1.class
- Test2.class
- directory1
- Test3.class
- Test4.class
- directory2
- Test5.class
以下是我的代码方法:
private void runJunitTest(JUnitCore junit, ArrayList<File> directories, String path) {
File f = new File(path); //path first call is usually "C:\Documents\Java\Project\src\tests\"
File bin = new File(txtClassTest.getText()); //bin path is "C:\Documents\Java\Project\target\tests-classes\"
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
if(files[i].isFile()){
String fullname = getFullyQualifiedName(files[i].getAbsolutePath(), txtTest.getText()); //getFullyQualifiedName return the fully qualified name of a class eg. net.sourceforge.Test1
try {
URLClassLoader urlClassloader = URLClassLoader.newInstance(new URL[] { bin.toURI().toURL() });
Class cl = Class.forName(fullname, false, urlClassloader);
Result result = junit.run(cl);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
}
else{
directories.add(files[i]);
}
}
if(!directories.isEmpty()){
String dir = directories.get(0).getAbsolutePath();
directories.remove(0);
runJunitTest(junit, directories,dir);
}
}
}
我还尝试更改bin
和path
变量以潜入内部或走到路径的外部,但结果却大致相同。
编辑
拥有bin
:...\target\test-classes\
以下是您提出的例外情况:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: net/sourceforge/pmd/ReportListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.net.FactoryURLClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at elaborationSystem.Interface$ButtonHandler.runJunitTest(Interface.java:386)
at elaborationSystem.Interface$ButtonHandler.runJunitTest(Interface.java:402)
at elaborationSystem.Interface$ButtonHandler.runJunitTest(Interface.java:402)
at elaborationSystem.Interface$ButtonHandler.runJunitTest(Interface.java:402)
at elaborationSystem.Interface$ButtonHandler.actionPerformed(Interface.java:364)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: net.sourceforge.pmd.ReportListener
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.net.FactoryURLClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 55 more