Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make JUnit let me set variables in one test case and access them in other if they are in the same class

Tags:

junit

Let say I have a test class called MyTest.

In it I have three tests.

public class MyTest {

AnObject object;

@Before
public void setup(){
  object = new AnObject();
  object.setSomeValue(aValue);
}

@Test
public void testMyFirstMethod(){
    object.setAnotherValue(anotherValue);
    // do some assertion to test that the functionality works
    assertSomething(sometest);
}

@Test
public void testMySecondMethod(){
    AValue val = object.getAnotherValue();
    object.doSomethingElse(val);
    // do some assertion to test that the functionality works
    assertSomething(sometest);
}

Is there any way I can use the value of anotherValue, which is set with its setter in the first test, in the second test. I am using this for testing database functionality. When I create an object in the DB I want to get its GUID so I can use this to do updates and deletes in later test methods, without having to hardcode the GUID and therefore making it irrelevant for future use.

like image 629
Ankur Avatar asked Jan 08 '12 05:01

Ankur


People also ask

Is it possible to group the test cases in JUnit?

JUnit test suites help to grouping and executing tests in bulk. Executing tests separately for all test classes is not desired in most cases. Test suites help in achieving this grouping. In JUnit, test suites can be created and executed with these annotations.

Which methods are used in test class to initialize and release common objects?

You declare these objects as a private variable, and initialize them by overriding the setUp() or via the constructor. You can perform clean-up operations by overriding tearDown() . Each test method runs on its own TestCase instance with its own set of text fixtures.


1 Answers

You are introducing a dependency between two tests. JUnit deliberately does not support dependency between tests, and you can't guarantee the order of execution (except for test classes in a test suite, see my answer to Has JUnit4 begun supporting ordering of test? Is it intentional?). So you really want to have dependencies between two test methods:

  1. you have to use an intermediate static value
  2. as Cedric suggests, use TestNG, which specifically supports dependencies
  3. in this case, you can create a method to create the line, and call it from both methods.

I would personally prefer 3, because:

  1. I get independent tests, and I can run just the second test (in Eclipse or such like)
  2. In my teardown in the class, I can remove the line from the database, the cleanup. This means that whichever test I run, I always start off with the same (known) database state.

However, if your setup is really expensive, you can consider this to be an integration test and just accept the dependency, to save time.

like image 136
Matthew Farwell Avatar answered Nov 03 '22 23:11

Matthew Farwell