powermock: jacoco offline instrumentation incompatibility with powermock byte-code manipulation when using @SuppressStaticInitializationFor

Hi, there is a test project: https://github.com/astafev/jacoco-offline-powermock-junit

The problem: if use @SuppressStaticInitializationFor(“StaticClass”) and execute mockStatic(StaticClass.class) in the test method, it’s impossible to instantiate StaticClass, the exception:

java.lang.NullPointerException
    at StaticClass.<init>(StaticClass.java:1)

The problem occurs only when StaticClass is patched with jacoco offline instrumentation.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Fixed by ignoring all synthetic methods/fields, as it was recommended by Jacoco team.

@andreicristianpetcu You may check source code of ClassPathScanningCandidateComponentProvider. It’s the Spring class which scans file system and looking for class file, so as result it can find not loaded classes.

But, I’m not sure that we really need feature. For us will be enough that we have list of filter in a properties file or hard-coded in some class constants.

there’s a trick to solve the problem with current version:

PowerMockito.mockStatic(clazz, new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            if (invocation.getMethod().getName().equals("$jacocoInit")) {
                return CALLS_REAL_METHODS.answer(invocation);
            }
            return RETURNS_DEFAULTS.answer(invocation);
        }
    }
);

Probably it can be also done also by overriding default answer in mocktio (via global config). Then existing code with mockStatic won’t be changed.

@andreicristianpetcu I don’t think we should bring in Reflections for this. Reflections contains a lot more stuff than this and in some sense it tries to solve the same problems as powermock-reflect. I think we could take inspiration from Reflections perhaps but imho we should avoid third-party dependencies as a much as possible.

@andreicristianpetcu I think this is a good issue to start 😃