Thoppan / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

Cannot suppress JPanel constructor when extending class #383

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a class like this:

import javax.swing.JPanel;
import java.awt.LayoutManager;

final class MyPanel extends JPanel {

    MyPanel(final LayoutManager layoutManager) {
        super(layoutManager);
    }
}

I have a test like this:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.LayoutManager;

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyPanel.class)
public final class MyPanelTest {

    @Test
    public void test() {
        PowerMock.suppress(PowerMock.constructor(JPanel.class, LayoutManager.class));
        new MyPanel(new BorderLayout());
    }
}

I get the following exception:

org.powermock.reflect.exceptions.ConstructorNotFoundException: Failed to lookup 
constructor with parameter types [ org.powermock.core.IndicateReloadClass ] in 
class java.awt.Container.
    at org.powermock.reflect.internal.WhiteboxImpl.getConstructor(WhiteboxImpl.java:258)
    at org.powermock.core.MockGateway.constructorCall(MockGateway.java:214)
    at javax.swing.JComponent.<init>(JComponent.java)
    at javax.swing.JPanel.<init>(JPanel.java)
    at com.mathworks.activationclient.view.snuoptions.MyPanel.<init>(MyPanel.java:10)
    at com.mathworks.activationclient.view.snuoptions.MyPanelTest.test(MyPanelTest.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:312)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:296)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:284)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:209)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:102)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:42)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NoSuchMethodException: 
java.awt.Container.<init>(org.powermock.core.IndicateReloadClass)
    at java.lang.Class.getConstructor0(Class.java:2706)
    at java.lang.Class.getDeclaredConstructor(Class.java:1985)
    at org.powermock.reflect.internal.WhiteboxImpl.getConstructor(WhiteboxImpl.java:250)
    ... 35 more

I expect the test to pass with no exception.

What version of the product are you using? On what operating system?

I am using PowerMock 1.4.10 with Java 1.6.0_17 on Windows 7 x64.

Original issue reported on code.google.com by seangc...@gmail.com on 30 Apr 2012 at 3:46

GoogleCodeExporter commented 9 years ago
Try using Whitebox.newInstance(MyPanel.class) instead. The reason is probably 
because MyPanel cannot be bytecode manipulated correctly because it's a 
standard Java class.

Original comment by johan.ha...@gmail.com on 13 Jul 2012 at 7:15