tags:

views:

40

answers:

6

Hi,

Currently I am creating a TestCase for one work flow and a workflow have different steps. The results of one step is important since , second step needs to know value of step1's execution.

class TestCaseWorkFlow1 extends TestCase {

  private static String resultOfStep1 = null;

  public void testStep1(){

    if(failed){
        resultOfStep1 = "failed";
    }
  }

  public void testStep2(){

    if(resultOfStep1 != null ) {  
         //First test failed.
    }else {
    }

  }

}

Currently we are using static variable to pass information between testmethods .

What is the best solution for this scenario ?

Please help.

Thanks J

A: 

The conventional wisdom is that the best solution is to have all the steps recomputed from scratch for each test, usually in a private helper method, so that you don't depend on the order in which JUnit executes your tests (which is not actually defined to be in textual order). You should probably do this, unless some of the steps take a truly horrible amount of time to recompute.

Kilian Foth
+1  A: 

Your tests should really be independent of each other, so think if there's a way to do that. For example, you know what the outcome of a successful step1 is, so just provide that as input in step2 using a mock object or other approach. Or see if there's a way to break the sequential coupling of the steps so that each can be run independently.

Graham Lee
A: 

I agree with Kilian and Graham. Tests should be independent and self-contained. I.e. each test should run alone the same way is it runs with other tests.

But if you have some good reason for such dependency between tests, maybe try TestNG. In TestNG you can specify tests dependent on each other.

amorfis
+1  A: 

It seems that you are trying to achieve something beyond unit testing... which is perfectly fine. Try using other tools that are built on top of jUnit and provide you with mechanisms for testing behavior rather than small units of code. One of tools that I've been successfully using for some time is jBehave. You'll end up describing test scenarios using plain text scripts executed by methods which match scenario steps.

Boris Pavlović
A: 

As others have noted, you should make your tests independent of each other. One way to achieve this is to set up test fixtures correctly for each test.

However, the tests in your example look like they test different phases of the same scenario. So I would consider unifying all into a single test method, if that scenario is not too complicated.

Péter Török
+1  A: 

This does not sound like a unit test to me. That is fine, but JUnit may not be the best tool. Can you upgrade to JUnit 4? Assuming yes -

  1. Write a JUnit runner to ensure the order of your unit tests
  2. Pass state via static variables.
  3. Use Assume to validate that step one has worked.
mlk