Ed Dunn
Ed Dunn

Reputation: 1172

Running single junit test in gradle passes but fails when entire test is run

I have an unusual issue dealing with a particular junit test within gradle. When I run the test as follows it passes

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

However when I run

gradlew test

it fails with the following 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)

This test runs fine in ant so I've checked all dependencies between ant and gradle and they all seem to match. Any additional suggestions would be greatly appreciated

Here is the source code of the test

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

Upvotes: 5

Views: 3740

Answers (2)

Ed Dunn
Ed Dunn

Reputation: 1172

I fixed it by adding the following to the test

XMLUnit.setIgnoreWhitespace(false);

Seems that XMLAssert is outdated and will have to overhaul these tests in the future to get everything current. For now I am in good shape.

Upvotes: 0

lance-java
lance-java

Reputation: 27986

My guess is that another test is being dirty and bleeding into this test. Things like System Properties or mutable static variables are likely causes. Those static XMLUnit mutator methods have a code smell to me

You could try the following which will give each test a "clean" jvm. Note this will slow things down

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

See Test.setForkEvery(...)

This is obviously a hack and it's best to find the offending test and fix it to clean up after itself in @After or @AfterTest. If you can't find the "dirty" test you could always initialize the "polluted" statics in @Before or @BeforeTest of the failing test

Upvotes: 2

Related Questions