3

When I try to execute the below simplified JUnit test, it succeeds but i am getting an error message: Notifications are not supported when all test-instances are created first!

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(BlockJUnit4ClassRunner.class)
@PrepareForTest({ A.class })
public class TestA extends TestB {
    @Test
    public void test() throws Exception {
        assertEquals(true, true);
    }
}

public class TestB {}
public class A {}

When I remove the @PowerMockRunnerDelegate(BlockJUnit4ClassRunner.class), or the extends TestB or the @PrepareForTest({ A.class }), the message disappears. Even an emtpy @PrepareForTest({ }) makes the error message appear.

I found this post, but it is not the same problem, because I am not using an extended BlockJUnit4ClassRunner.

I am trying to understand why this error message appears.

I am using the latest version of Powermock (1.6.6) and JUnit 4.12 and running the test using java8.

M. Abbas
  • 6,409
  • 4
  • 33
  • 46

1 Answers1

3

PowerMock extends jUnit events message and fire some additional event during test life circle. PowerMockTestListener could be used to listen all jUnit and additional PowerMock events.

One of this event required that an instance per test is created. A jUnit runner usually creates an new instance of test class per each test method in class, but some of them could create only one instance of class for all tests. When @PowerMockRunnerDelegate is used then delegated runner responsible for creating test instance.

If event "test start" is fired without “test created” then PowerMock detects that all test instances are created before tests are started and cannot fire its internal events and this message is printed to system.err. So, you have the same issue as it has been described in post.

PowerMock has only one implementation PowerMockTestListener at current moment - AnnotationEnabler. This implementation is used for integration with EasyMock and Mockito and supporting annotation like @TestSubject, @Mock and so on. As result, then you see this message in console it means that these features could not work properly. But for Mockito case it could be easier fixed with using MockitoAnnotations.initMocks(this).

Very interesting that if you remove ‘extends TestB’ the message is disappeared. I’ll check this case and investigate why it happen.

Artur Zagretdinov
  • 2,034
  • 13
  • 22