0

我试图让一个简单的 Ant 脚本运行,但我无法让它执行测试。我有很多问题,我几乎不知道从哪里开始。我将从使用几个非常简单的测试用例创建的小型“简化”项目开始。最终我想 fork="yes" 和 forkmode="once" 但现在我在调试时把它关掉了。

我的 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 标志的 Ant 控制台输出。

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());
    }

}

编辑二: <pathelement path="${bin}"/>从类路径中丢失,似乎已经解决了这个问题(尽管仍然无法分叉)。好'nuff。

4

3 回答 3

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文件作为资源。

于 2013-07-08T15:09:04.423 回答
0

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

[junit] java.lang.ClassNotFoundException: simpletest.test.SimpleTest
[junit]     at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
于 2013-07-08T14:56:05.013 回答
0

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

另外,还需要放在binjunit classpath上;没有它, junit实际上没有任何类定义可用。

于 2013-08-06T20:24:56.430 回答