gpt4 book ai didi

java - 在 gradle 中运行单个 junit 测试通过但在运行整个测试时失败

转载 作者:搜寻专家 更新时间:2023-11-01 03:16:27 24 4
gpt4 key购买 nike

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

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)

这个测试在 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));
}
}

最佳答案

我的猜测是另一个测试被污染并渗入了这个测试。系统属性或可变静态变量之类的东西可能是原因。那些静态的 XMLUnit 变体方法对我来说有代码的味道

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

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

参见 Test.setForkEvery(...)

这显然是一个 hack,最好找到有问题的测试并修复它,以便在 @After 或 @AfterTest 中自行清理。如果您找不到“脏”测试,您总是可以在失败测试的@Before 或@BeforeTest 中初始化“污染”静态变量

关于java - 在 gradle 中运行单个 junit 测试通过但在运行整个测试时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48954923/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com