8

Which frameworks would you recommend for writing unit tests for multithreading code in Java? Like, for example, when you have a program that is supposed to execute a bunch of threads with several allowed paths of executions and certain excluded paths (like "executable unit 1, 2 and 3 can run in parallel, at least two should be run in parallel, and 4 always has to run after 3").

andersoj
  • 22,406
  • 7
  • 62
  • 73
Tim van Beek
  • 385
  • 3
  • 12

4 Answers4

3

I suggest reading about JUnit @Rule to use them for concurrency tests.

nobeh
  • 9,784
  • 10
  • 49
  • 66
2

I recommend ContiPerf2 since it is a very easy to use framework. Although I do not think it will provide the possibility to declare execution pathes or something like that.

Omnaest
  • 3,096
  • 1
  • 19
  • 18
1

You can use parametrized JUnit tests, as described here: http://junit.org/junit/javadoc/4.5/org/junit/runners/Parameterized.html

But you will need to program thread logic yourself, for example by using different thread pools.

You can take a look at pathfinder (http://javapathfinder.sourceforge.net/) if you want to prove that your code is thread safe.

hgrey
  • 3,033
  • 17
  • 21
1

This thread describes some general approaches to testing concurrent code.

Designing a Test class for a custom Barrier

For specific frameworks, tempus-fugit has some JUnit rules to run things in parallel but as suggested above, you're tests will likely have to include more logic.

Community
  • 1
  • 1
Toby
  • 9,523
  • 8
  • 36
  • 59