-2

The Testng exception displays the cyclic dependencies please explain

package test.depends;
import org.testng.annotations.Test
public class SimpleDependencyTes {
@Test
public void testOne() {
    System.out.println("The first method");
}
@Test(dependsOnMethods= {"testOne","testTwo"})
public void testTwo() {
    System.out.println("The Second method");
}
}

Error is as follows:

org.testng.TestNGException: The following methods have cyclic dependencies:SimpleDependencyTes.testTwo()[pri:0, instance:test.depends.SimpleDependencyTes@1774679]
at org.testng.internal.Graph.topologicalSort(Graph.java:149)
at org.testng.internal.MethodHelper.topologicalSort(MethodHelper.java:261)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:317)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:64)
at org.testng.TestRunner.initMethods(TestRunner.java:438)
at org.testng.TestRunner.init(TestRunner.java:271)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.<init>(TestRunner.java:192)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
YaDav MaNish
  • 1,260
  • 2
  • 12
  • 20

3 Answers3

1

Your testTwo lists itself as a dependency:

@Test(dependsOnMethods= {"testOne","testTwo"})
public void testTwo() {

By definition dependsOnMethods means that

(testTwo) will start execution only after all the tests it depends on executed successfully

(source). So if you set testTwo as quoted above, you are saying that testTwo cannot start until testTwo executed successfully. Which is impossible. Instead, you may want to make testTwo dependent on testOne only:

@Test(dependsOnMethods= {"testOne"})
public void testTwo() {

And some other testThree can be dependent on both:

@Test(dependsOnMethods= {"testOne","testTwo"})
public void testThree() {
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
0

Circular dependency

Circular dependency is a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as mutually recursive.

Circular dependency can occur in two cases where:

  • A method testOne() references another method testTwo() and at the same time testTwo() is trying to reference testOne().
  • A method testTwo() is trying to reference itself i.e. testTwo().

Here you can find a detailed discussion on What is a circular dependency and how can I solve it?


dependsOnMethods

dependsOnMethods is used to create a List of methods on which this method depends on.


dependsOnMethods with @Test annotation

There are two kinds of dependencies:

  • Hard dependencies: All the methods you depend on must have run and succeeded for you to run. If at least one failure occurred in your dependencies, the method will not be invoked and marked as a SKIP in the report.
  • Soft dependencies: The method always be run after the methods you depend on, even if some of them have failed. This is useful when you just want to make sure that your test methods are run in a certain order but their success doesn't really depend on the success of others. A soft dependency is obtained by adding "alwaysRun=true" in your @Test annotation.

  • An example:

    @Test
    public void initial_test_method() {}
    
    @Test(dependsOnMethods = { "serverStartedOk" })
    public void test_method1() {}
    

    In this example, test_method1() is declared as depending on method initial_test_method(), which guarantees that initial_test_method() will always be invoked first.


What went wrong

In your program, the @Test annotation for testTwo() contains testTwo() (itself) as one of the dependsOnMethods. Hence you see the error.


Solution

If you remove the method testTwo() from the List of dependsOnMethods your program will be perfect.

package test.depends;
import org.testng.annotations.Test
public class SimpleDependencyTes {
    @Test
    public void testOne() {
        System.out.println("The first method");
    }
    @Test(dependsOnMethods= {"testOne"})
    public void testTwo() {
        System.out.println("The Second method");
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
-1

You have defined Dependencies on two method: testOne and testTwo , In which testTwo is itself, Which does not mean. You can not define Dependency on method itself. You can define dependency on any other methods. So you need to remove testTwo dependency.

@Test(dependsOnMethods= {"testOne"})
public void testTwo() {
    System.out.println("The Second method");
}

You can define these two dependency for any third @Test method, like:

@Test(dependsOnMethods= {"testOne","testTwo"})
public void testThree() {
    System.out.println("The third method");
}
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51