1

I have a JUNIT test that runs with PowerMockRunner.class.
Now, I am adding a new test, however, I want to run both the test concurrently.
I know I could do this using @RunWith(ConcurrentTestRunner.class) or just by creating a concurrent executor myself.
Is there anything available with PowerMock to do this?

Sample Code:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassWithStaticMethod.class)
@SuppressStaticInitializationFor("ClassWithStaticMethod")
public class PowerMockTest {

    @org.junit.Test
    public void myTest() {
     ...
    }

    //This new Test
    @org.junit.Test
    public void myNewTest() {
     ...
    }

}
Meritor
  • 166
  • 11
  • And just for the record: you are aware that writing code that makes use of static ... well, creates hard-to-test code; and that besides using Powermock, the reasonable alternative is to rework your production to not use static? See https://www.youtube.com/playlist?list=PLD0011D00849E1B79 for further guidance into that topic. – GhostCat Sep 21 '16 at 13:26
  • Thanks for the information. – Meritor Sep 21 '16 at 16:29
  • well.. I partially agree here, I understand why shouldn't we use static. However, I really don't like to make changes in production code design, just for make the code easy to test. – Meritor Sep 21 '16 at 16:35

2 Answers2

0

You can run PowerMock with other jUnit runners with using @PowerMockRunnerDelegate. More information here. I'm not sure if it will work with ConcurrentTestRunner. But you may try.

Artur Zagretdinov
  • 2,034
  • 13
  • 22
-1

Maybe an answer you didn't expect: don't do that - do not run tests in parallel.

You see, the main purpose of unit tests is to help you identifying and fixing bugs in your production code as soon as possible. Therefore you want to have your test code to be "as simple" as possible to get you there.

Meaning: running your tests in parallel will for sure make things more complicated. Worst case, you create something "flaky", because of some hidden race condition, your tests sometimes work in parallel, but in rare situations, running in parallel causes fails.

Beyond that: unit tests should be running very fast in the first place. So there shouldn't be much gain from running test in parallel!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • yes, not the answer for the question, however, this at least make me think on that direction. – Meritor Sep 21 '16 at 16:37
  • > "running your tests in parallel will for sure make things more complicated" I am sorry, I don't agree here. It will be complicated, if my test is not well designed. >"because of some hidden race condition, your tests sometimes work in parallel, but in rare situations, running in parallel causes fails." Shouldn't we test this? >"Unit tests should be running very fast in the first place" Agree, that's the reason I want to make it run parallel. Event 1 second matters here, if I go sequential it hits my over all build time. – Meritor Sep 21 '16 at 16:45
  • If you are concerned about such issues then think about better ways to enforce /find race conditions. Just running tests in parallel has small chances of being helpful and worth the effort. – GhostCat Sep 21 '16 at 18:33
  • Agree, and we have such test cases already written.. however, the question is not to make parallel test execution to test race condition. Apologies, the discussion is going in different direction. Can we discuss about this somewhere else? – Meritor Sep 21 '16 at 23:54
  • Not this week; too busy. If you are still interested, send me a comment next monday. – GhostCat Sep 22 '16 at 07:13