依赖性群体因循环依赖性异常而失败



我当前正在编写Integration测试,我必须在测试运行之前检查某些条件。

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的博客,我想将集成测试重构为以下内容:

@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配置文件中遇到了问题

例如,如果您具有这样的测试配置:

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

在这种情况下,我们可以收到环状依赖性,并作为结果错误。

删除 extends IntegrationTestBase,它将起作用。

最新更新