gpt4 book ai didi

java - DependsOnGroups 因循环依赖异常而失败

转载 作者:行者123 更新时间:2023-11-29 08:53:37 24 4
gpt4 key购买 nike

我目前正在编写集成测试,我必须在我的测试运行之前检查一些条件。

public class TheClassWhichContainsIT {

@Test(dependsOnGroups = { "init" })
public void thisIsTheFirstTest() {
System.out.printf("Test");
}

@Test(groups = "init")
public void checkIfWeAreOnWindows() {
throw new SkipException("Not on windows");
}

}

如果抛出 SkipException,上面的示例将按预期工作,而不是将测试标记为已跳过,而不是我喜欢的失败。

所以基于Cedric Beust's blog我想将我的集成测试重构为以下内容:

@Test(dependsOnGroups = { "init" })
public class TheClassWhichContainsIT extends IntegrationTestBase {

public void thisIsTheFirstTest() {
System.out.printf("Test");
}
}

@Test(groups = "init")
public class IntegrationTestBase {

public void checkIfWeAreOnWindows() {
throw new SkipException("ignore");
}

}

但是如果我尝试运行我的测试,我会得到以下信息:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
16:test (default-test) on project testngtest: Execution default-test of goal org
.apache.maven.plugins:maven-surefire-plugin:2.16:test failed: There was an error
in the forked process
[ERROR] org.testng.TestNGException:
[ERROR] The following methods have cyclic dependencies:
[ERROR] TheClassWhichContainsTest.thisIsTheFirstTest()[pri:0, instance:de.akdb.k
m.tests.TheClassWhichContainsTest@3603820e]
[ERROR]
[ERROR] at org.testng.internal.Graph.topologicalSort(Graph.java:148)
[ERROR] at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:26
1)
[ERROR] at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:317)
[ERROR] at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.
java:59)
[ERROR] at org.testng.TestRunner.initMethods(TestRunner.java:481)
[ERROR] at org.testng.TestRunner.init(TestRunner.java:235)
[ERROR] at org.testng.TestRunner.init(TestRunner.java:205)
[ERROR] at org.testng.TestRunner.<init>(TestRunner.java:153)
[ERROR] at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRu
nner.java:522)
[ERROR] at org.testng.SuiteRunner.init(SuiteRunner.java:157)
[ERROR] at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
[ERROR] at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
[ERROR] at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
[ERROR] at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
[ERROR] at org.testng.TestNG.run(TestNG.java:1057)
[ERROR] at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.ja
va:91)
[ERROR] at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeSing
leClass(TestNGDirectoryTestSuite.java:128)
[ERROR] at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(Tes
tNGDirectoryTestSuite.java:112)
[ERROR] at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider
.java:113)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameCla
ssLoader(ForkedBooter.java:200)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(Fork
edBooter.java:153)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:
103)
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutio
nException

最佳答案

可能是你的testng配置文件有问题

例如,如果您有这样的 testng 配置:

<test name="some-tests" preserve-order="true">
<groups>
<define name="test-group">
<include name="init"/>
</define>
<run>
<include name="test-group"/>
</run>
</groups>

<classes>
<class name="TheClassWhichContainsIT"/>
<class name="IntegrationTestBase"/>
</classes>
</test>

属性preserve-order="true" 表示在标签classes 中定义的所有类将按照定义的顺序执行。

在这种情况下,我们可能会收到循环依赖并导致错误。

关于java - DependsOnGroups 因循环依赖异常而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21455690/

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