I would like to stop and mark as failed too long running junit tests (executed within Maven 3 build). I am aware of three ways of doing it:

1) Using Test annotation with timeout parameter:

public void testWithTimeout() {

2) Using Rule annotation:

public Timeout globalTimeout = new Timeout(100);

3) Configuring maven-surefire-plugin using following options:


The clue is 1) and 2) requires to change each test (it hurts when you have many thousands). 3) solution is not acceptable as within many modules first test starts the context which is used by the test - tests performance would drastically decrease.

Do you have any other ideas how to achieve that? Any tricky solution (not involving patching JUnit :))?

AFAIK you can attach rules to Test suites. So all you would need to do is "group" your tests logically into suites based on whether they need a timeout or not and then apply a timeout rule to the suite.

Or you can create a parent class for performance sensitive tests with a particular timeout rule and make sure all tests which need to follow a particular timeout extend this parent class.

Not ideal but much better than adding timeouts to each and every test...