Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerMockIgnore fails to ignore super classes of the test class #1134

Open
cytng opened this issue Jun 20, 2022 · 4 comments
Open

PowerMockIgnore fails to ignore super classes of the test class #1134

cytng opened this issue Jun 20, 2022 · 4 comments

Comments

@cytng
Copy link

cytng commented Jun 20, 2022

I'm developing a plugin for ElasticSearch. When I used @PowerMockIgnore to ignore the super test class org.apache.lucene.util.LuceneTestCase, it didn't work.

The LuceneTestCase was added into prepareForTestClasses at org.powermock.core.classloader.MockClassLoaderFactory#createForClass(line: 66), and then was added into classes to modify when constructing MockClassLoaderConfiguration.

It is PACKAGES_TO_BE_DEFERRED not deferPackages to determine whether ignored or not. I have no idea how to make it. Could you provide the way to ignore super test classes?

@cytng
Copy link
Author

cytng commented Jun 20, 2022

I'd hold one LuceneTestCase instance to avoid this problem. Any better ways to solve it?

@cytng
Copy link
Author

cytng commented Jun 21, 2022

I got it! Let's talk about why I need to ignore those super classes. Here are serval internal annotations in LuceneTestCase, which were also added into classes to modify and attached with PowerMockModified. The extra PowerMockModified interface do lead to AnnotationFormatErrors thrown at sun.reflect.annotation.AnnotationInvocationHandler.(AnnotationInvocationHandler.java:53) . Maybe disable to mock annotations is the most valuable solution.

image

@cytng
Copy link
Author

cytng commented Jun 21, 2022

Fix the bug above in the following way.

--- a/powermock-core/src/main/java/org/powermock/tests/utils/impl/PrepareForTestExtractorImpl.java
+++ b/powermock-core/src/main/java/org/powermock/tests/utils/impl/PrepareForTestExtractorImpl.java
@@ -120,6 +120,7 @@ public class PrepareForTestExtractorImpl extends AbstractTestClassExtractor {
     private void addInnerClassesAndInterfaces(Set<String> all, Class<?> classToMock) {
         Class<?>[] declaredClasses = classToMock.getDeclaredClasses();
         for (Class<?> innerClass : declaredClasses) {
+            if (innerClass.isAnnotation()) continue;
             all.add(innerClass.getName());
         }
     }

@cytng
Copy link
Author

cytng commented Jun 21, 2022

How about ignoring super classes with @PrepareOnlyThisForTest(testClass)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant