在junit中运行代码时的不同行为

时间:2011-08-19 09:34:40

标签: java spring ant junit

目前我正在开发一个弹簧应用程序。到目前为止,我使用Ant和Junit来运行代码。现在我尝试将测试代码移动到java类并使用java任务从Ant运行它,由于以下异常而失败:

 [java] javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found
 [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
 [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:764)
 [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:218)
 [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:132)
 [java]     at org.apache.tools.ant.taskdefs.Java.execute(Java.java:105)
 [java]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 [java]     at java.lang.reflect.Method.invoke(Method.java:597)
 [java]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
 [java]     at org.apache.tools.ant.Task.perform(Task.java:348)
 [java]     at org.apache.tools.ant.Target.execute(Target.java:357)
 [java]     at org.apache.tools.ant.Target.performTasks(Target.java:385)
 [java]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
 [java]     at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
 [java]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
 [java]     at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
 [java]     at org.apache.tools.ant.Main.runBuild(Main.java:758)
 [java]     at org.apache.tools.ant.Main.startAnt(Main.java:217)
 [java]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
 [java]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
 [java] Caused by: javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found
 [java]     at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:129)
 [java]     at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:849)
 [java]     at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:773)
 [java]     at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483)
 [java]     at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
 [java]     at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:73)
 [java]     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
 [java]     at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:156)
 [java]     at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
 [java]     at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:645)
 [java]     at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:160)
 [java]     at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:89)
 [java]     at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:59)
 [java]     at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:61)
 [java]     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136)
 [java]     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
 [java]     at org.postslurper.Main.init(Unknown Source)
 [java]     at org.postslurper.Main.<init>(Unknown Source)
 [java]     at org.postslurper.Main.main(Unknown Source)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 [java]     at java.lang.reflect.Method.invoke(Method.java:597)
 [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:217)
 [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:152)
 [java]     ... 21 more
 [java] --- Nested Exception ---
 [java] javax.xml.parsers.FactoryConfigurationError: Provider org.apache.xerces.jaxp.DocumentBuilderFactoryImpl not found
 [java]     at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:129)
 [java]     at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:849)
 [java]     at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:773)
 [java]     at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:483)
 [java]     at org.apache.log4j.LogManager.<clinit>(LogManager.java:127)
 [java]     at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:73)
 [java]     at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:242)
 [java]     at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:156)
 [java]     at org.apache.commons.logging.impl.SLF4JLogFactory.getInstance(SLF4JLogFactory.java:132)
 [java]     at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:645)
 [java]     at org.springframework.context.support.AbstractApplicationContext.<init>(AbstractApplicationContext.java:160)
 [java]     at org.springframework.context.support.AbstractRefreshableApplicationContext.<init>(AbstractRefreshableApplicationContext.java:89)
 [java]     at org.springframework.context.support.AbstractRefreshableConfigApplicationContext.<init>(AbstractRefreshableConfigApplicationContext.java:59)
 [java]     at org.springframework.context.support.AbstractXmlApplicationContext.<init>(AbstractXmlApplicationContext.java:61)
 [java]     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:136)
 [java]     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
 [java]     at org.postslurper.Main.init(Unknown Source)
 [java]     at org.postslurper.Main.<init>(Unknown Source)
 [java]     at org.postslurper.Main.main(Unknown Source)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 [java]     at java.lang.reflect.Method.invoke(Method.java:597)
 [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:217)
 [java]     at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:152)
 [java]     at org.apache.tools.ant.taskdefs.Java.run(Java.java:764)
 [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:218)
 [java]     at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:132)
 [java]     at org.apache.tools.ant.taskdefs.Java.execute(Java.java:105)
 [java]     at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 [java]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 [java]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 [java]     at java.lang.reflect.Method.invoke(Method.java:597)
 [java]     at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
 [java]     at org.apache.tools.ant.Task.perform(Task.java:348)
 [java]     at org.apache.tools.ant.Target.execute(Target.java:357)
 [java]     at org.apache.tools.ant.Target.performTasks(Target.java:385)
 [java]     at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1337)
 [java]     at org.apache.tools.ant.Project.executeTarget(Project.java:1306)
 [java]     at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
 [java]     at org.apache.tools.ant.Project.executeTargets(Project.java:1189)
 [java]     at org.apache.tools.ant.Main.runBuild(Main.java:758)
 [java]     at org.apache.tools.ant.Main.startAnt(Main.java:217)
 [java]     at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
 [java]     at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
 [java] Java Result: -1

执行以下行时抛出此异常,这是测试代码和java类都包含的主要逻辑。

context = new ClassPathXmlApplicationContext("services.xml");

以下代码段显示了两个进程是如何从ant启动的。两个目标都使用相同的类路径。

<target name="collect">
    <java classname="my.project.Main" classpathref="master-classpath">
    </java>
</target>

<target name="test">
    <property name="result.ext" value=".log"></property>
    <junit>
        <classpath refid="master-classpath"></classpath>
        <test name="my.project.test.TestMain" todir="out" outfile="dump">
            <formatter type="plain" extension="${result.ext}"/>
        </test>
    </junit>
    <fail if="test.trace">${test.trace}</fail>
</target> 

我还转储了jvm的属性(System.getproperties()):

java.runtime.name: Java(TM) SE Runtime Environment
sun.boot.library.path: /usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/amd64
java.vm.version: 16.3-b01
ant.library.dir: /usr/share/ant/lib
java.vm.vendor: Sun Microsystems Inc.
java.vendor.url: http://java.sun.com/
path.separator: :
java.vm.name: Java HotSpot(TM) 64-Bit Server VM
file.encoding.pkg: sun.io
user.country: DE
sun.java.launcher: SUN_STANDARD
sun.os.patch.level: unknown
java.vm.specification.name: Java Virtual Machine Specification
user.dir: /home/some-desktop/hm-repo/ssb-dev/trunk
java.runtime.version: 1.6.0_20-b02
java.awt.graphicsenv: sun.awt.X11GraphicsEnvironment
java.endorsed.dirs: /usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/endorsed
os.arch: amd64
java.io.tmpdir: /tmp
line.separator: 

java.vm.specification.vendor: Sun Microsystems Inc.
os.name: Linux
ant.home: /usr/share/ant
sun.jnu.encoding: UTF-8
java.library.path: /usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/amd64/server:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/amd64:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/../lib/amd64:/home/some-desktop/Dev/web2test/web2test-1.3/bin/:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
java.specification.name: Java Platform API Specification
java.class.version: 50.0
sun.management.compiler: HotSpot 64-Bit Server Compiler
os.version: 2.6.32-24-generic
user.home: /home/some-desktop
user.timezone: 
java.awt.printerjob: sun.print.PSPrinterJob
file.encoding: UTF-8
java.specification.version: 1.6
java.class.path: /usr/share/ant/lib/ant-launcher.jar:/usr/share/java/xmlParserAPIs.jar:/usr/share/java/xercesImpl.jar:/home/some-desktop/.ant/lib/ivy.jar:/home/some-desktop/.ant/lib/jsch.jar:/usr/share/ant/lib/ant-apache-resolver.jar:/usr/share/ant/lib/ant-apache-bcel.jar:/usr/share/ant/lib/ant-nodeps.jar:/usr/share/ant/lib/ant-swing.jar:/usr/share/ant/lib/ant-apache-oro.jar:/usr/share/ant/lib/ant-launcher.jar:/usr/share/ant/lib/ant-commons-logging.jar:/usr/share/ant/lib/ant-jsch.jar:/usr/share/ant/lib/ant-jdepend.jar:/usr/share/ant/lib/ant.jar:/usr/share/ant/lib/ant-javamail.jar:/usr/share/ant/lib/ant-antlr.jar:/usr/share/ant/lib/ant-commons-net.jar:/usr/share/ant/lib/ant-apache-log4j.jar:/usr/share/ant/lib/ant-apache-bsf.jar:/usr/share/ant/lib/ant-junit.jar:/usr/share/ant/lib/ant-trax.jar:/usr/share/ant/lib/junit.jar:/usr/share/ant/lib/ant-apache-regexp.jar:/usr/share/ant/lib/ant-jmf.jar:/usr/lib/jvm/java-6-sun-1.6.0.20/lib/tools.jar
user.name: some-desktop
java.vm.specification.version: 1.0
java.home: /usr/lib/jvm/java-6-sun-1.6.0.20/jre
sun.arch.data.model: 64
user.language: de
java.specification.vendor: Sun Microsystems Inc.
java.vm.info: mixed mode
java.version: 1.6.0_20
java.ext.dirs: /usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/ext:/usr/java/packages/lib/ext
sun.boot.class.path: /usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/resources.jar:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/rt.jar:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/jsse.jar:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/jce.jar:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/lib/charsets.jar:/usr/lib/jvm/java-6-sun-1.6.0.20/jre/classes
java.vendor: Sun Microsystems Inc.
file.separator: /
java.vendor.url.bug: http://java.sun.com/cgi-bin/bugreport.cgi
sun.io.unicode.encoding: UnicodeLittle
sun.cpu.endian: little
sun.cpu.isalist: 

我还计算了java属性的差异,并且Junit中唯一不同的行是

user.timezone: Europe/Berlin

有人有想法,有什么不对吗?

干杯,

GLA

1 个答案:

答案 0 :(得分:2)

很难确切地说出你遇到这个问题的原因,但是正如JB Nizet所说的那样与从ant继承的类路径有关。您会注意到您的错误来自于spring4中的log4j / slf4j。

当您使用fork =“false”从ant启动junit时,它使用ant定义的类路径,其中可能包含各种依赖项,包括不同版本的log4j,它们依赖于不同版本的xerces。

当你分叉时,你有一个更清洁的环境,所以它有效。

为了避免这类问题,通常最好分叉junit测试以避免将所有这些依赖项拖到项目中。不分叉可能导致其他问题进一步发展。

有关更多选项,请参阅Ant Junit Task