我正在尝试运行一个简单的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>
这是Ant控制台的输出,带有-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:UsersabananaworkspaceJMockitSimpleTestbuild.xml
Buildfile: C:UsersabananaworkspaceJMockitSimpleTestbuild.xml
Adding reference: ant.projectHelper
Adding reference: ant.parsing.context
Adding reference: ant.targets
parsing buildfile C:UsersabananaworkspaceJMockitSimpleTestbuild.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:UsersabananaworkspaceJMockitSimpleTestbuild.xml
Setting ro project property: ant.file.type.Test1111 -> file
Project base dir set to: C:UsersabananaworkspaceJMockitSimpleTest
+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:UsersabananaworkspaceJMockitSimpleTestsrc
Setting project property: bin -> C:UsersabananaworkspaceJMockitSimpleTestbin
Setting project property: dist -> C:UsersabananaworkspaceJMockitSimpleTestdist
Setting project property: lib -> C:UsersabananaworkspaceJMockitSimpleTestlib
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:UsersabananaworkspaceJMockitSimpleTestbin because it already exists.
compile:
fileset: Setup scanner in dir C:UsersabananaworkspaceJMockitSimpleTestsrc with patternSet{ includes: [] excludes: [] }
[javac] simpletestSimpleClass.java omitted as C:UsersabananaworkspaceJMockitSimpleTestbinsimpletestSimpleClass.class is up to date.
[javac] simpletestSimpleInterface.java omitted as C:UsersabananaworkspaceJMockitSimpleTestbinsimpletestSimpleInterface.class is up to date.
[javac] simpletesttestSimpleTest.java omitted as C:UsersabananaworkspaceJMockitSimpleTestbinsimpletesttestSimpleTest.class is up to date.
fileset: Setup scanner in dir C:UsersabananaworkspaceJMockitSimpleTestsrc with patternSet{ includes: [] excludes: [**/*.java] }
[copy] No sources found.
[copy] omitted as C:UsersabananaworkspaceJMockitSimpleTestbin is up to date.
[copy] bin omitted as C:UsersabananaworkspaceJMockitSimpleTestbinbin is up to date.
[copy] simpletest omitted as C:UsersabananaworkspaceJMockitSimpleTestbinsimpletest is up to date.
[copy] simpletesttest omitted as C:UsersabananaworkspaceJMockitSimpleTestbinsimpletesttest is up to date.
tests:
[junit] Found C:UsersabananaworkspaceCIMPlibjunit-4.11.jar
[junit] Found C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-launcher.jar
[junit] Found C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant.jar
[junit] Found C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-junit.jar
[junit] Found C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-junit4.jar
fileset: Setup scanner in dir C:UsersabananaworkspaceJMockitSimpleTestsrc 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 FilesJavajdk1.7.0_21jrebin, java.vm.version=23.21-b01, ant.library.dir=C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609lib, 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:UsersabananaworkspaceJMockitSimpleTest, java.runtime.version=1.7.0_21-b11, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, java.endorsed.dirs=C:Program FilesJavajdk1.7.0_21jrelibendorsed, os.arch=x86, java.io.tmpdir=C:UsersTTHORA~1AppDataLocalTemp, line.separator=
[junit] , java.vm.specification.vendor=Oracle Corporation, user.variant=, os.name=Windows 7, ant.home=C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609, sun.jnu.encoding=Cp1252, java.library.path=C:eclipse;C:WindowsSunJavabin;C:Windowssystem32;C:Windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem;C:WindowsSystem32WindowsPowerShellv1.0;C:Program FilesMicrosoft SQL Server100ToolsBinn;C:Program FilesMicrosoft SQL Server100DTSBinn;C:Program FilesJavajre7bin;C:Program FilesJavajre7bin.;C:Program FilesJavajdk1.7.0_21bin;;C:eclipse;;.;C:eclipseconfigurationorg.eclipse.osgibundles3321.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:Usersabanana, 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:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-antlr.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-apache-bcel.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-apache-bsf.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-apache-log4j.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-apache-oro.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-apache-regexp.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-apache-resolver.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-apache-xalan2.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-commons-logging.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-commons-net.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-jai.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-javamail.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-jdepend.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-jmf.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-jsch.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-junit.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-junit4.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-launcher.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-netrexx.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-swing.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-testutil.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant.jar;C:UsersabananaworkspaceCIMPlibhamcrest-core-1.3.jar;C:UsersabananaworkspaceCIMPlibjunit-4.11.jar;C:UsersabananaworkspaceCIMPlibtools.jar;C:eclipseconfigurationorg.eclipse.osgibundles601.cplibantdebug.jar;C:eclipseconfigurationorg.eclipse.osgibundles601.cplibremote.jar;C:eclipseconfigurationorg.eclipse.osgibundles611.cplibremoteAnt.jar;C:Program FilesJavajdk1.7.0_21libtools.jar;C:eclipsepluginsorg.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:eclipsepluginsorg.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:UsersabananaworkspaceJMockitSimpleTestbuild.xml tests clean compile init, java.home=C:Program FilesJavajdk1.7.0_21jre, 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 FilesJavajdk1.7.0_21jrelibext;C:WindowsSunJavalibext, sun.boot.class.path=C:Program FilesJavajdk1.7.0_21jrelibresources.jar;C:Program FilesJavajdk1.7.0_21jrelibrt.jar;C:Program FilesJavajdk1.7.0_21jrelibsunrsasign.jar;C:Program FilesJavajdk1.7.0_21jrelibjsse.jar;C:Program FilesJavajdk1.7.0_21jrelibjce.jar;C:Program FilesJavajdk1.7.0_21jrelibcharsets.jar;C:Program FilesJavajdk1.7.0_21jrelibjfr.jar;C:Program FilesJavajdk1.7.0_21jreclasses, 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:UsersabananaworkspaceCIMPlibjunit-4.11.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-launcher.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-junit.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-junit4.jar to CLASSPATH
[junit] Using CLASSPATH C:UsersabananaworkspaceJMockitSimpleTestlibjmockit.jar;C:UsersabananaworkspaceJMockitSimpleTestlibjunit.jar;C:UsersabananaworkspaceJMockitSimpleTestlibant-junit.jar;C:UsersabananaworkspaceJMockitSimpleTestlibhamcrest-core.jar;C:UsersabananaworkspaceCIMPlibjunit-4.11.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-launcher.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-junit.jar;C:eclipsepluginsorg.apache.ant_1.8.3.v201301120609libant-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:UsersabananaworkspaceJMockitSimpleTestbuild.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());
}
}
EDIT II:<pathelement path="${bin}"/>
在类路径中丢失,似乎已经解决了问题(尽管仍然无法分叉)。很好。
simpletest.test.simpletest中有一个对类的引用,Ant找不到它。您必须确保Ant知道在哪里可以查找该类。
[junit] java.lang.ClassNotFoundException: simpletest.test.SimpleTest
[junit] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
尝试更改
<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
文件作为资源。
您应该只运行*Test
类;你的额外通配符在最后会造成严重破坏。
此外,您需要将bin
放在junit类路径上;如果没有它,junit实际上就没有任何可用的类定义。