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
- #645: Initial draft of jacocoInit fix — committed to andreicristianpetcu/powermock by deleted user 8 years ago
- #645: Initial draft of jacocoInit fix — committed to andreicristianpetcu/powermock by deleted user 8 years ago
- #645: Added test for MockRepository — committed to andreicristianpetcu/powermock by deleted user 8 years ago
- #645: Added test for ClasspathClassFinder — committed to andreicristianpetcu/powermock by deleted user 8 years ago
- #645: Implemented Classpath Class Finder — committed to andreicristianpetcu/powermock by deleted user 8 years ago
- #645: getAllClassesFromClasspathImplementin an interface — committed to andreicristianpetcu/powermock by deleted user 8 years ago
- #645 better logging for class path item — committed to andreicristianpetcu/powermock by deleted user 8 years ago
- Issue #707 PowerMock should ignore and keep without changes synthetic method/fields when modifies class (#710) Also fixed issues: #645 #422 — committed to powermock/powermock by thekingn0thing 8 years ago
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:
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 😃