peter
peter

Reputation: 8473

Race condition when running junit test in parallel

I am wondering would it have some race condition if I run my tests in parallel and the two tests (below) share an instance variable? So my test class runs with SpringJunit4ClassRunner, and I have 2 tests method a() and b(), the variable state will be modified or reassigned from each test, and the doSomethingWithState() would use the variable state and pass it to the testing method. I know with maven-surefire-plugin you can run it at the method level that both a() and b() will get assigned to a thread and run it in parallel.

@RunWith(SpringJUnit4ClassRunner.class)
public class TestA {

     private Object state;

     @Test
     public void a() {
          stateObjectA();
          doSomethingWithState();
          assertion();
     } 

     @Test
     public void b() {
          stateObjectB();
          doSomethingWithState();
          assertion();
     }

     private void stateObjectA() {
          // do some mocking and setup state
     }

     private void stateObjectB() {
          // do some mocking and setup state
     }

     private void doSomethingWithState() {
          // use the state object and feed into the testing method
     }
}

Upvotes: 3

Views: 4909

Answers (2)

Sebastian
Sebastian

Reputation: 118

You must take into account two things:

  • If you require to use instance variables, you instantiate them before loading (Junit4 provides @Before, @After annotations, and @BeforeClass and @AfterClass for static variables).

  • Junit doesn't guarantee you that it will run the test cases in the same order every time, so each test must be coded isolated from the rest.

Another obvious point is that you must not think tests based on the results of the others. Take this into account when mocking stuff for integration tests, perhaps tests will begin to fail randomly and you won't know why.

Upvotes: 0

GhostCat
GhostCat

Reputation: 140427

I guess the only reasonable answer is: depends ... on your exact context and code base.

The essence if race conditions is: you have more than one thread manipulating ("writing") to shared data. Your lengthy question boils down to exactly such a setup. So there is high potential for race conditions in the setup that you described above.

And then: it doesn't make any difference if you are talking about methods in production code or methods called by some testing framework. Because race conditions do not care about that. They only "care" about more than one thread writing to shared data.

That is all that matters here!

Upvotes: 2

Related Questions