5

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.

6 Answers 6

5

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.

1
  • Hmmmm ... I should use teardown more regularly!
    – Ankur
    Jan 16, 2012 at 13:38
3

You should use TestNG if you need this (and I agree it's fairly common in integration testing). TestNG uses the same instance to run your tests, so values stored in fields are preserved between tests, which is very useful when your objects are expensive to create (JUnit forces you to use statics to achieve the same effect, which should be avoided).

1
  • Thanks, I recently moved from TestNG to JUnit (although obviously wasn't an expert) so this will be an easy move.
    – Ankur
    Jan 16, 2012 at 13:33
3

First off, make sure your @Test 's run in some kind of defined order i.e. @FixMethodOrder(MethodSorters.NAME_ASCENDING) In the example below, I'm assuming that test2 will run after test1 To share a variable between them, use a ThreadLocal (from java.lang). Note that the scope of the ThreadLocal variable is to the thread, so if you are running multiple threads, each will have a copy of 'email' (the static in this case implies that its only global to the thread)

private static ThreadLocal<String> email = new ThreadLocal<String>();
@Test
public void test1 {
    email.set("[email protected]);
}

@Test
public void test2 {
   System.out.println(email.get());
}
2

You should not do that. Tests are supposed to be able to run in random order. If you want to test things that depend on one value in the database, you can do that in the @Before code, so it's not all repeated for each test case.

4
  • Wes I get what you're saying. But I think this is the one case where that's not true. Because I'd have to put potentially untested functionality into the @Before code.
    – Ankur
    Jan 8, 2012 at 6:29
  • 2
    Then make a function to call at the start of all of your tests. You don't want to make it so you can't run tests in parallel. Jan 8, 2012 at 8:56
  • Maintaining state is not mutually exclusive with running your tests in parallel, see how TestNG does it (and my answer). Jan 8, 2012 at 17:24
  • Thanks for the followup, I like the idea of putting the code in a separate method.
    – Ankur
    Jan 16, 2012 at 13:38
1

I have found nice solution, just add Before annotation to the previous test!

private static String email = null;
@Before
@Test
public void test1 {
    email = "[email protected]"
}

@Test
public void test2 {
   System.out.println(email);
}
3
  • This will not work, when there is already a method annotated with @Before in the same class.
    – Uwe Allner
    Nov 15, 2016 at 13:45
  • 1
    You can allways make it one, at least it is still a solution.
    – pg7812
    Nov 15, 2016 at 14:46
  • This worked for me but with JUnit5 you must use: @BeforeTestMethod Feb 14, 2020 at 3:45
0

If you, like me, googled until here and the answer didn't serve to you, I'll just leave this: Use @BeforeEach

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.