Jijoy
Jijoy

Reputation: 12744

Passing information between test methods in a junit testcase

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

Upvotes: 4

Views: 3897

Answers (7)

Franck
Franck

Reputation: 522

in this basic example, the variable is changed in the test A, and can be used in the test B

public class BasicTest extends ActivityInstrumentationTestCase2 {
    public BasicTest() throws ClassNotFoundException {
        super(TARGET_PACKAGE_ID, launcherActivityClass);        
    }

    public static class MyClass {    
        public static String myvar = null;              
        public void set(String s) {
            myvar = s;
        }               
        public String get() {
            return myvar;
        }
    }

    private MyClass sharedVar;

    @Override
    protected void setUp() throws Exception {
        sharedVar = new MyClass();
    }

    public void test_A() {
        Log.d(S,"run A");
        sharedVar.set("blah");
    }

    public void test_B() {
        Log.d(S,"run B");       
        Log.i(S,"sharedVar is: " + sharedVar.get());        
    }

}

output result is:

run A

run B

sharedVar is: blah

Upvotes: 0

Michael Lloyd Lee mlk
Michael Lloyd Lee mlk

Reputation: 14661

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.

Upvotes: 3

Péter Török
Péter Török

Reputation: 116306

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.

Upvotes: 0

Boris Pavlović
Boris Pavlović

Reputation: 64642

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.

Upvotes: 1

amorfis
amorfis

Reputation: 15770

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.

Upvotes: 0

user23743
user23743

Reputation:

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.

Upvotes: 2

Kilian Foth
Kilian Foth

Reputation: 14386

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.

Upvotes: 1

Related Questions