0

I have two test classes written using junit

  1. TestClass1
  2. TestClass2

If we need to ensure that TestClass2 is run only after TestClass1 in executed. How do we achieve this?

Nandish A
  • 1,645
  • 5
  • 26
  • 41
  • 2
    Why? Tests should be independent. – Dave Newton Jul 10 '13 at 16:32
  • 1
    Generally I will advice you not to make dependencies like this. If two tests depend on each other I think a better solution will be to make 1 test instead, with criterias from both tests. – Vegard Jul 10 '13 at 16:34
  • You should find the answer here: http://stackoverflow.com/questions/9528581/specifying-order-of-execution-in-junit-test-case – Ian Fairman Jul 10 '13 at 16:34
  • I wanted to use the data created in DB by TestClass1 , else I need to repeat the code in TestClass2 – Nandish A Jul 10 '13 at 16:34
  • by doing that it is as I see it one test, not two. If you want to use the same data in two tests, init the database in a `@Before` method and make two independant `@Test`'s – Vegard Jul 10 '13 at 16:37
  • Thanks for your feedbacks. This definitely helped – Nandish A Jul 10 '13 at 16:52

1 Answers1

0

You can achieve this using the Junit test suite. When you specify the list of classes to be executed in a test suite, you're defining an array, and these test classes will be executed in order, except when you're doing parallel execution.

Note that JUnit does not guarantee the order of execution of tests within a class.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136