在gradle过程中运行单个junit测试,但在运行整个测试时失败

时间:2018-02-23 19:27:36

标签: java gradle junit

我在gradle中处理特定的junit测试有一个不寻常的问题。当我按如下方式运行测试时,它会通过

gradlew test --tests com.compsci.ic.xbrl.SchemaRefTest

然而,当我跑

gradlew test

使用以下stacktrace

失败
java.lang.IllegalStateException: Undeclared namespace prefix in DOM input: link
    at net.sf.saxon.dom.NodeWrapper.getURI(NodeWrapper.java:531)
    at net.sf.saxon.dom.NodeWrapper.getNameCode(NodeWrapper.java:412)
    at net.sf.saxon.om.Navigator.copy(Navigator.java:523)
    at net.sf.saxon.dom.NodeWrapper.copy(NodeWrapper.java:827)
    at net.sf.saxon.om.Navigator.copy(Navigator.java:516)
    at net.sf.saxon.dom.NodeWrapper.copy(NodeWrapper.java:827)
    at net.sf.saxon.om.StrippedNode.copy(StrippedNode.java:448)
    at net.sf.saxon.instruct.CopyOf.processLeavingTail(CopyOf.java:397)
    at net.sf.saxon.instruct.Template.applyLeavingTail(Template.java:175)
    at net.sf.saxon.instruct.ApplyTemplates.applyTemplates(ApplyTemplates.java:343)
    at net.sf.saxon.Controller.transformDocument(Controller.java:1735)
    at net.sf.saxon.Controller.transform(Controller.java:1559)
    at org.custommonkey.xmlunit.Transform.transformTo(Transform.java:211)
    at org.custommonkey.xmlunit.Transform.getResultDocument(Transform.java:233)
    at org.custommonkey.xmlunit.XMLUnit.stripWhiteSpaceUsingXSLT(XMLUnit.java:514)
    at org.custommonkey.xmlunit.XMLUnit.getWhitespaceStrippedDocument(XMLUnit.java:507)
    at org.custommonkey.xmlunit.Diff.getWhitespaceManipulatedDocument(Diff.java:182)
    at org.custommonkey.xmlunit.Diff.getManipulatedDocument(Diff.java:203)
    at org.custommonkey.xmlunit.Diff.<init>(Diff.java:155)
    at org.custommonkey.xmlunit.Diff.<init>(Diff.java:145)
    at org.custommonkey.xmlunit.Diff.<init>(Diff.java:109)
    at org.custommonkey.xmlunit.Diff.<init>(Diff.java:101)
    at org.custommonkey.xmlunit.Diff.<init>(Diff.java:93)
    at org.custommonkey.xmlunit.XMLAssert.assertXMLEqual(XMLAssert.java:228)
    at org.custommonkey.xmlunit.XMLAssert.assertXMLEqual(XMLAssert.java:179)
    at com.compsci.ic.xbrl.SchemaRefTest.testSerialize(SchemaRefTest.java:34)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy1.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:108)
    at sun.reflect.GeneratedMethodAccessor42.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:146)
    at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:128)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
    at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
    at java.lang.Thread.run(Thread.java:748)

这个测试在ant中运行良好,所以我检查了ant和gradle之间的所有依赖关系,它们似乎都匹配。任何其他建议将不胜感激

以下是测试的源代码

package com.compsci.ic.xbrl;

import java.io.IOException;
import java.net.URI;

import org.custommonkey.xmlunit.XMLAssert;
import org.custommonkey.xmlunit.XMLUnit;
import org.junit.Test;
import org.xml.sax.SAXException;

import com.compsci.ic.xbrl.xstream.URIConverter;
import com.thoughtworks.xstream.XStream;

public class SchemaRefTest {
    @Test
    public void testSerialize() throws SAXException, IOException {
        SchemaRef ref = new SchemaRef();

        ref.setHref(URI.create("http://example.com"));

        XStream xstream = new XStream();

        xstream.registerConverter(new URIConverter());
        xstream.processAnnotations(SchemaRef.class);

        XMLUnit.getTestDocumentBuilderFactory().setNamespaceAware(false);       
        XMLUnit.getControlDocumentBuilderFactory().setNamespaceAware(false);

        XMLAssert.assertXMLEqual(
            "<link:schemaRef xlink:type='simple' xlink:href='http://example.com'/>",
            xstream.toXML(ref));
    }
}

2 个答案:

答案 0 :(得分:1)

我的猜测是,另一项测试正在变脏,并且正在进行此测试。系统属性或可变静态变量等可能是原因。那些静态XMLUnit mutator方法给我带来了代码味道

您可以尝试以下操作,这将为每个测试提供一个“干净”的jvm。注意这会减慢速度

apply plugin: 'java' 
test {
    forkEvery = 1
}

请参阅Test.setForkEvery(...)

这显然是一个黑客攻击,最好找到有问题的测试,然后在@After或@AfterTest中修复它以进行清理。如果你找不到“脏”测试,你总是可以在失败测试的@Before或@BeforeTest中初始化“污染”静态

答案 1 :(得分:0)

我通过在测试中添加以下内容来修复它

XMLUnit.setIgnoreWhitespace(false);

似乎XMLAssert已过时,将来必须彻底检查这些测试以使所有内容保持最新状态。现在我状态很好。