2013-07-08 68 views
0

我试图获得一个简单的Ant脚本运行,但我无法让它执行测试。我有很多问题,我很难确定从哪里开始。我将从我用几个非常简单的测试用例创建的小型“简化”项目开始。最终我想fork =“是”和forkmode =“一次”,但现在我在调试时关闭了它。java.lang.classnotfoundexception with ant + Junit

我的build.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<project name="Test1111" default="tests" basedir="."> 

    <property name="projectName" value="Test1111"/> 
    <property name="src" location="src"/> 
    <property name="bin" location="bin"/> 
    <property name="dist" location="dist"/> 
    <property name="lib" location="lib"/> 

    <path id="classpath"> 
     <fileset dir="${lib}"> 
      <include name="jmockit.jar"/> 
     </fileset> 
    </path> 

    <target name="init"> 
     <tstamp/> 
     <mkdir dir="${bin}"/> 
    </target> 

    <target name="compile" depends="init" description="compile the source"> 
     <javac srcdir="${src}" destdir="${bin}" includeantruntime="false"> 
      <classpath refid="classpath"/> 
     </javac> 
     <copy todir="${bin}"> 
      <fileset dir="${src}" excludes="**/*.java"/>    
     </copy> 
    </target> 

    <target name="tests" depends="compile" description="Run unit tests"> 
     <junit printsummary="withOutAndErr" fork="no" forkmode="once" haltonfailure="yes" failureproperty="junit.failure"> 
      <classpath path="lib/jmockit.jar"/> 
      <classpath path="lib/junit.jar"/> 
      <classpath path="lib/ant-junit.jar"/> 
      <classpath path="lib/hamcrest-core.jar"/> 
      <formatter type="plain" usefile="false"/> 
      <batchtest> 
       <fileset dir="${src}"> 
        <include name="**/*Test*"/> 
        <exclude name="**/AllTests.java"/> 
       </fileset> 
      </batchtest> 
     </junit> 
    </target> 

    <target name="clean" description="clean up"> 
     <delete dir="${bin}"/> 
    </target> 
</project> 

这里的蚂蚁控制台输出,与-verbose和-debug标志。

Apache Ant(TM) version 1.8.3 compiled on February 26 2012 
Apache Ant(TM) version 1.8.3 compiled on February 26 2012 
Setting ro project property: ant.file -> C:\Users\abanana\workspace\JMockitSimpleTest\build.xml 
Buildfile: C:\Users\abanana\workspace\JMockitSimpleTest\build.xml 
Adding reference: ant.projectHelper 
Adding reference: ant.parsing.context 
Adding reference: ant.targets 
parsing buildfile C:\Users\abanana\workspace\JMockitSimpleTest\build.xml with URI = file:/C:/Users/abanana/workspace/JMockitSimpleTest/build.xml 
Setting ro project property: ant.project.name -> Test1111 
Adding reference: Test1111 
Setting ro project property: ant.project.default-target -> tests 
Setting ro project property: ant.file.Test1111 -> C:\Users\abanana\workspace\JMockitSimpleTest\build.xml 
Setting ro project property: ant.file.type.Test1111 -> file 
Project base dir set to: C:\Users\abanana\workspace\JMockitSimpleTest 
+Target: 
+Target: init 
+Target: compile 
+Target: tests 
+Target: clean 
Adding reference: ant.LocalProperties 
parsing buildfile jar:file:/C:/eclipse/plugins/org.apache.ant_1.8.3.v201301120609/lib/ant.jar!/org/apache/tools/ant/antlib.xml with URI = jar:file:/C:/eclipse/plugins/org.apache.ant_1.8.3.v201301120609/lib/ant.jar!/org/apache/tools/ant/antlib.xml from a zip file 
Setting project property: projectName -> Test1111 
Setting project property: src -> C:\Users\abanana\workspace\JMockitSimpleTest\src 
Setting project property: bin -> C:\Users\abanana\workspace\JMockitSimpleTest\bin 
Setting project property: dist -> C:\Users\abanana\workspace\JMockitSimpleTest\dist 
Setting project property: lib -> C:\Users\abanana\workspace\JMockitSimpleTest\lib 
Adding reference: classpath 
Setting ro project property: ant.project.invoked-targets -> tests,clean,compile,init 
Adding reference: eclipse.ant.targetVector 
Build sequence for target(s) `tests' is [init, compile, tests] 
Complete build sequence is [init, compile, tests, clean, ] 
init: 
Setting project property: DSTAMP -> 20130708 
Setting project property: TSTAMP -> 1043 
Setting project property: TODAY -> July 8 2013 
    [mkdir] Skipping C:\Users\abanana\workspace\JMockitSimpleTest\bin because it already exists. 
compile: 
fileset: Setup scanner in dir C:\Users\abanana\workspace\JMockitSimpleTest\src with patternSet{ includes: [] excludes: [] } 
    [javac] simpletest\SimpleClass.java omitted as C:\Users\abanana\workspace\JMockitSimpleTest\bin\simpletest\SimpleClass.class is up to date. 
    [javac] simpletest\SimpleInterface.java omitted as C:\Users\abanana\workspace\JMockitSimpleTest\bin\simpletest\SimpleInterface.class is up to date. 
    [javac] simpletest\test\SimpleTest.java omitted as C:\Users\abanana\workspace\JMockitSimpleTest\bin\simpletest\test\SimpleTest.class is up to date. 
fileset: Setup scanner in dir C:\Users\abanana\workspace\JMockitSimpleTest\src with patternSet{ includes: [] excludes: [**/*.java] } 
    [copy] No sources found. 
    [copy] omitted as C:\Users\abanana\workspace\JMockitSimpleTest\bin is up to date. 
    [copy] bin omitted as C:\Users\abanana\workspace\JMockitSimpleTest\bin\bin is up to date. 
    [copy] simpletest omitted as C:\Users\abanana\workspace\JMockitSimpleTest\bin\simpletest is up to date. 
    [copy] simpletest\test omitted as C:\Users\abanana\workspace\JMockitSimpleTest\bin\simpletest\test is up to date. 
tests: 
    [junit] Found C:\Users\abanana\workspace\CIMP\lib\junit-4.11.jar 
    [junit] Found C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-launcher.jar 
    [junit] Found C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant.jar 
    [junit] Found C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-junit.jar 
    [junit] Found C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-junit4.jar 
fileset: Setup scanner in dir C:\Users\abanana\workspace\JMockitSimpleTest\src with patternSet{ includes: [**/*Test*] excludes: [**/AllTests.java] } 
    [junit] Using System properties {java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=C:\Program Files\Java\jdk1.7.0_21\jre\bin, java.vm.version=23.21-b01, ant.library.dir=C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=;, java.vm.name=Java HotSpot(TM) Client VM, file.encoding.pkg=sun.io, user.script=, user.country=US, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=Service Pack 1, java.vm.specification.name=Java Virtual Machine Specification, user.dir=C:\Users\abanana\workspace\JMockitSimpleTest, java.runtime.version=1.7.0_21-b11, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:\Program Files\Java\jdk1.7.0_21\jre\lib\endorsed, os.arch=x86, java.io.tmpdir=C:\Users\TTHORA~1\AppData\Local\Temp\, line.separator= 
    [junit] , java.vm.specification.vendor=Oracle Corporation, user.variant=, os.name=Windows 7, ant.home=C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609, sun.jnu.encoding=Cp1252, java.library.path=C:\eclipse;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\Java\jre7\bin;C:\Program Files\Java\jre7\bin\.;C:\Program Files\Java\jdk1.7.0_21\bin;;C:\eclipse;;.;C:\eclipse\configuration\org.eclipse.osgi\bundles\332\1\.cp, java.specification.name=Java Platform API Specification, java.class.version=51.0, sun.management.compiler=HotSpot Client Compiler, os.version=6.1, user.home=C:\Users\abanana, user.timezone=America/New_York, java.awt.printerjob=sun.awt.windows.WPrinterJob, file.encoding=UTF-8, java.specification.version=1.7, java.class.path=C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-antlr.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-apache-bcel.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-apache-bsf.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-apache-log4j.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-apache-oro.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-apache-regexp.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-apache-resolver.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-apache-xalan2.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-commons-logging.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-commons-net.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-jai.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-javamail.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-jdepend.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-jmf.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-jsch.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-junit.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-junit4.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-launcher.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-netrexx.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-swing.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-testutil.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant.jar;C:\Users\abanana\workspace\CIMP\lib\hamcrest-core-1.3.jar;C:\Users\abanana\workspace\CIMP\lib\junit-4.11.jar;C:\Users\abanana\workspace\CIMP\lib\tools.jar;C:\eclipse\configuration\org.eclipse.osgi\bundles\60\1\.cp\lib\antdebug.jar;C:\eclipse\configuration\org.eclipse.osgi\bundles\60\1\.cp\lib\remote.jar;C:\eclipse\configuration\org.eclipse.osgi\bundles\61\1\.cp\lib\remoteAnt.jar;C:\Program Files\Java\jdk1.7.0_21\lib\tools.jar;C:\eclipse\plugins\org.eclipse.swt.win32.win32.x86_3.100.1.v4236b.jar, user.name=TThorarensen, java.vm.specification.version=1.7, sun.java.command=org.eclipse.ant.internal.launching.remote.InternalAntRunner -debug -verbose -Dorg.eclipse.ant.core.ANT_PROCESS_ID=1373294617517 -Declipse.connect.port=56425 -Declipse.pdebuild.home=/C:/eclipse/plugins/org.eclipse.pde.build_3.8.2.v20121114-140810/./ -Declipse.pdebuild.scripts=/C:/eclipse/plugins/org.eclipse.pde.build_3.8.2.v20121114-140810/scripts/ -Declipse.pdebuild.templates=/C:/eclipse/plugins/org.eclipse.pde.build_3.8.2.v20121114-140810/templates/ -Dant.home=C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609 -logger org.eclipse.ant.internal.launching.remote.logger.RemoteAntBuildLogger -inputhandler org.eclipse.ant.internal.ui.antsupport.inputhandler.ProxyInputHandler -buildfile C:\Users\abanana\workspace\JMockitSimpleTest\build.xml tests clean compile init, java.home=C:\Program Files\Java\jdk1.7.0_21\jre, sun.arch.data.model=32, user.language=en, java.specification.vendor=Oracle Corporation, awt.toolkit=sun.awt.windows.WToolkit, java.vm.info=mixed mode, sharing, java.version=1.7.0_21, java.ext.dirs=C:\Program Files\Java\jdk1.7.0_21\jre\lib\ext;C:\Windows\Sun\Java\lib\ext, sun.boot.class.path=C:\Program Files\Java\jdk1.7.0_21\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_21\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_21\jre\classes, java.vendor=Oracle Corporation, file.separator=\, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, sun.io.unicode.encoding=UnicodeLittle, sun.cpu.endian=little, sun.desktop=windows, sun.cpu.isalist=pentium_pro+mmx pentium_pro pentium+mmx pentium i486 i386 i86} 
    [junit] Implicitly adding C:\Users\abanana\workspace\CIMP\lib\junit-4.11.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-launcher.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-junit.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-junit4.jar to CLASSPATH 
    [junit] Using CLASSPATH C:\Users\abanana\workspace\JMockitSimpleTest\lib\jmockit.jar;C:\Users\abanana\workspace\JMockitSimpleTest\lib\junit.jar;C:\Users\abanana\workspace\JMockitSimpleTest\lib\ant-junit.jar;C:\Users\abanana\workspace\JMockitSimpleTest\lib\hamcrest-core.jar;C:\Users\abanana\workspace\CIMP\lib\junit-4.11.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-launcher.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-junit.jar;C:\eclipse\plugins\org.apache.ant_1.8.3.v201301120609\lib\ant-junit4.jar 
Class org.apache.tools.ant.taskdefs.optional.junit.PlainJUnitResultFormatter loaded from parent loader (parentFirst) 
Finding class simpletest.test.SimpleTest 
    [junit] Running simpletest.test.SimpleTest 
    [junit] Testsuite: simpletest.test.SimpleTest 
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec 
    [junit]  Caused an ERROR 
    [junit] simpletest.test.SimpleTest 
    [junit] java.lang.ClassNotFoundException: simpletest.test.SimpleTest 
    [junit]  at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
    [junit]  at java.net.URLClassLoader$1.run(URLClassLoader.java:355) 
    [junit]  at java.security.AccessController.doPrivileged(Native Method) 
    [junit]  at java.net.URLClassLoader.findClass(URLClassLoader.java:354) 
    [junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:423) 
    [junit]  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    [junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    [junit]  at java.lang.ClassLoader.loadClass(ClassLoader.java:356) 
    [junit]  at java.lang.Class.forName0(Native Method) 
    [junit]  at java.lang.Class.forName(Class.java:266) 
    [junit]  at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32) 
    [junit]  at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424) 
    [junit]  at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138) 

BUILD FAILED 
C:\Users\abanana\workspace\JMockitSimpleTest\build.xml:34: Test simpletest.test.SimpleTest failed 
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.actOnTestResult(JUnitTask.java:1954) 
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:854) 
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.executeOrQueue(JUnitTask.java:1899) 
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTask.execute(JUnitTask.java:800) 
    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:601) 
    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106) 
    at org.apache.tools.ant.Task.perform(Task.java:348) 
    at org.apache.tools.ant.Target.execute(Target.java:392) 
    at org.apache.tools.ant.Target.performTasks(Target.java:413) 
    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399) 
    at org.apache.tools.ant.Project.executeTarget(Project.java:1368) 
    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41) 
    at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32) 
    at org.apache.tools.ant.Project.executeTargets(Project.java:1251) 
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424) 
    at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138) 

Total time: 351 milliseconds 

我在Windows上使用Eclipse并在Eclipse中运行构建脚本。我觉得我已经尝试过所有的东西,但是我希望这里的某个人能够认识到可能会失败的东西

编辑: SimpleTest.java:

package simpletest.test; 

import static org.junit.Assert.*; 

import org.junit.After; 
import org.junit.Before; 
import org.junit.Test; 
import mockit.*; 

import simpletest.SimpleClass; 
import simpletest.SimpleInterface; 

public class SimpleTest { 

    static final class SimpleImpl implements SimpleInterface { 
     public int doSomething() { 
      System.out.println("HELLO WORLD"); 
      return 1; 
     } 
    } 

    @Before 
    public void setUp() throws Exception { 
    } 

    @After 
    public void tearDown() throws Exception { 
    } 

    @Test 
    public void testSomeInterface() { 
     SimpleInterface si = new MockUp<SimpleInterface>() { 
      @Mock 
      int doSomething() 
      { 
       // intentionally left blank; 
       return 1; 
      } 

     }.getMockInstance(); 

     assertEquals(1, si.doSomething()); 
    } 

    @Test 
    public void testInterfaceAgain() { 
     final SimpleInterface thing = new SimpleImpl(); 
     /* 
     new NonStrictExpectations(SimpleImpl.class) {{ 
      thing.doSomething(); result = 1; 
     }}; 
     */ 
     final SimpleClass simple = new SimpleClass(); 

     simple.setSimpleInt(1); 

     assertEquals(simple.getSimpleInt(), thing.doSomething()); 
    } 

} 

编辑II:<pathelement path="${bin}"/>从classpath中失踪,似乎已经解决了这个问题(尽管依然没能到餐桌)。好'nuff。

回答

0

在您的simpletest.test.SimpleTest中有一个对Ant中找不到它的类的引用。你必须确保Ant知道在哪里寻找那个类。

[junit] java.lang.ClassNotFoundException: simpletest.test.SimpleTest 
[junit]  at java.net.URLClassLoader$1.run(URLClassLoader.java:366) 
+0

我该怎么做?我认为batchtest标签会照顾它?你能把我链接到相关的文档吗? – abanana

+0

请编辑您的问题并添加SimpleTest来源,以便我可以更好地了解问题。已添加 – sheidaei

+0

。 – abanana

0

尝试改变

<batchtest> 
    <fileset dir="${src}"> 
     <include name="**/*Test*"/> 
     <exclude name="**/AllTests.java"/> 
    </fileset> 
</batchtest> 

<batchtest> 
    <fileset dir="${bin}"> 
     <include name="**/*Test*"/> 
     <exclude name="**/AllTests.java"/> 
    </fileset> 
</batchtest> 

它看起来像ant脚本无法找到你的测试类,但<batchtest>任务应该能够采取.class文件作为资源。

+0

这是一个很好的主意,我应该想到以前,但不幸的是我得到相同的输出...让我回到你身边,我不得不检查一些东西。 – abanana

+0

我现在得到这个:http://mysticpaste.com/view/rJIp54J4Nm?2 最值得注意的是,它确实说了 '[junit]隐式添加C:\ Users \ abanana \ workspace \ ProjectX \ lib \ junit- 4.11.jar; C:\蚀\插件\ org.apache.ant_1.8.3.v201301120609 \ lib中\蚂蚁launcher.jar; C:\蚀\插件\ org.apache.ant_1.8.3.v201301120609 \ lib中\蚂蚁。罐; C:\蚀\插件\ org.apache.ant_1.8.3.v201301120609 \ lib中\蚂蚁的junit.jar; C:\蚀\插件\ org.apache.ant_1.8.3.v201301120609 \ lib中\蚂蚁junit4。罐子CLASSPATH' 我不知道如何关闭,但这可能是一个问题,不是? – abanana

+0

我想我已经想通了。你有一个静态的内部类,它可能被编译成类似'bin'目录中的'simpletest.test.SimpleTest $ SimpleImpl.class'。我猜测蚂蚁脚本正在尝试将它作为单元测试类运行。你可以尝试将它与任何引用一起评论,看看单元测试是否正确运行?如果是这样的话,那么你只需要修改蚂蚁任务来跳过内部类。 – austin

0

您应该只运行*Test类;最后你的额外通配符会造成严重破坏。

另外,你需要把bin放在junit类路径上;没有它,实际上没有任何类别定义可用联合。