DependsOnGroups因循环依赖异常而失败

时间:2014-01-30 11:48:59

标签: java testng

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

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

2 个答案:

答案 0 :(得分:0)

也许你在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中定义的所有类都将按定义的顺序执行。

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

答案 1 :(得分:0)

删除extends IntegrationTestBase,它将起作用。