powermock / powermock

PowerMock is a Java framework that allows you to unit test code normally regarded as untestable.
Apache License 2.0
4.16k stars 586 forks source link

initializationError occured when run a single method as JUnit test with PowerMockRunner #528

Open johanhaleby opened 9 years ago

johanhaleby commented 9 years ago

From liubao2...@gmail.com on July 31, 2014 10:16:33

What steps will reproduce the problem? 1.In Eclipse, select one method of class TestClassUnderTest, then right click and select "Run As > JUnit Test". What is the expected output? What do you see instead? In JUnit View : dummy[Runner JUnit4] Unrooted TestsRunner JUnit4 initialization error It works fine in the following situations:

  1. Select the whole class in Project Explorer and then run as JUnit Tests;
  2. Leave only one test method in the test class;
  3. In test classes without PowerMockRunner. What version of the product are you using? On what operating system? Eclipse : Kepler Service Release 1, Build id: 20130919-0819 JUnit Testing Framework plug-in : 4.11.0.v201303080030 PDE JUnit Runtime Support plugin: 3.4.300.v20130422-2046 JUnit : 4.11 powermock-core、powermock-module-junit4 : 1.5.5 Please provide any additional information below.

Attachment: TestClassUnderTest.java ClassUnderTest.java

Original issue: http://code.google.com/p/powermock/issues/detail?id=508

johanhaleby commented 9 years ago

From swhite00...@gmail.com on December 10, 2014 13:41:27

I have seen this before. In my case I had -XX:-UseSplitVerifier in my maven plugin in the pom. However, this is in eclipse, I had to create a special run configuration that would test the single method with -XX:-UseSplitVerifier as a vm arguement.

johanhaleby commented 9 years ago

From jordangl...@gmail.com on December 13, 2014 08:50:23

Also seeing this problem.

Eclipse: Luna Service Release 1 (4.4.1) Build id: 20140925-1800
JUnit Testing Framework plug-in : 4.11.0.v201303080030 PDE JUnit Runtime Support plugin: 3.4.300.v20130422-2046 JUnit : 4.11 powermock-core, powermock-module-junit4 : 1.5.4

@swhite00, not sure if you are suggesting that this VM argument should fix this issue or not, but it does not in my case.

johanhaleby commented 9 years ago

From matthias...@gmail.com on February 05, 2015 08:26:12

I faced the same problem. Forget about -XX:-UseSplitVerifier.

Solution: Put the @PrepareForTest(ClassUnderTest.class) annotation to the class-level of the test class as described at https://code.google.com/p/powermock/wiki/MockStatic .

As soon as you have changed this everything works as expected.

biogerm commented 9 years ago

Hi Johan,

Can you please provide the link to the explanation? The link attached seems not working?

BR Ryan

johanhaleby commented 9 years ago

It wasn't my explanation (I just migrated the issues to github that's why my name is all over the place). The link to the documentation is here though: https://github.com/jayway/powermock/wiki/MockStatic

github-karen commented 8 years ago

Hello, @johanhaleby! I encountered the same issue, it seems it's caused by adding the @RunWith annotation at the test class-level. I put the @PrepareForTest(ClassUnderTest.class) annotation to the class-level but it does NOT work. Moreover, I want to find a solution that does NOT need to put the @PrepareForTest(ClassUnderTest.class) at the class level, because it causes another issue according to my local test with the EclEmma Java Code Coverage. As this issue is still in open status. Is it still an unsolved issue? Do you find a solution on it? Thank you in advance~

BTW, my local environment is listed in the below :

chandrasekhar4u commented 8 years ago

Is it necessary to place both @PrepareForTest(ClassUnderTest.class) at class level and PowerMockito.mockStatic(ClassUnderTest.class) at method level.?

Or by placing @PrepareForTest(ClassUnderTest.class) only will suffices mocking static method or class?

If it's required to use both @PrepareForTest(ClassUnderTest.class) and PowerMockito.mockStatic(ClassUnderTest.class), then is there any way to keep PowerMockito.mockStatic(ClassUnderTest.class) at Class level (by using @beforeClass) instead of method level.

Could you please some one answer for my queries?

thekingn0thing commented 8 years ago

Is it necessary to place both @PrepareForTest(ClassUnderTest.class) at class level and PowerMockito.mockStatic(ClassUnderTest.class) at method level.?

Yes, it is necessary to place both, because they have different semantic.

By using @PrepareForTest(ClassUnderTest.class) you just let know PowerMock that class has to be modified during class loading. Why could it be needed? There are several reasons: mock static, mock constructor call, mock private method, and suppressing fields and methods.

By using PowerMockito.mockStatic(ClassUnderTest.class) you create a mock for class, and after this call, all static methods of class are mocked and you can specify required behaviour.

chandrasekhar4u commented 8 years ago

One more clarification, shall we use PowerMockito.mockStatic(ClassUnderTest.class) at class level, I mean by using @beforeClass we can call every time before test class executes.? When I try this some times I am facing initialization exception. is it mandatory to use PowerMockito.mockStatic(ClassUnderTest.class) at the method where we are mocking the static method of particular class? ( I'm able to use @before but not @beforeClass)

thekingn0thing commented 8 years ago

It isn't mandatory to use PowerMockito.mockStatic(ClassUnderTest.class) at the method. You can configure all mock for static methods on you setUp method (method with annotation @Before.