I am getting this error when I try to mock JTextField
.
java.lang.LinkageError: loader constraint violation in interface itable
initialization: when resolving method
"javax.swing.text.JTextComponent$1.dropLocationForPoint(
Ljavax/swing/text/JTextComponent;Ljava/awt/Point;)Ljavax/
swing/TransferHandler$DropLocation;" the class loader (instance of
org/powermock/core/classloader/MockClassLoader) of the current
class, javax/swing/text/JTextComponent$1, and the class loader
(instance of <bootloader>) for interface
sun/swing/SwingAccessor$JTextComponentAccessor have different Class
objects for the type mponent$1.dropLocationForPoint(Ljavax/swing/text/
JTextComponent;Ljava/awt/Point;)Ljavax/swing/TransferHandler$
DropLocation; used in the signature
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at javassist.runtime.Desc.getClassObject(Desc.java:43)
I tried to use this:
@PowerMockIgnore( {"javax.management.*","javax.security.*","javax.ws.*"})
Nothing worked.
Based on the scenario of the cited issue I have come with a workaround: the problem occurs in the static initializer of javax.swing.text.JTextComponent
so we might suppress that code using @SuppressStaticInitializationFor("javax.swing.text.JTextComponent")
.
Here is a working test case (using Powermock 1.6.4):
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mock;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
import org.powermock.modules.junit4.PowerMockRunner;
@RunWith(PowerMockRunner.class)
@PrepareForTest(JOptionPane.class)
@SuppressStaticInitializationFor("javax.swing.text.JTextComponent")
public class PowerMockIssue {
@Test
public void powermockTest() {
final JTextArea textArea = mock(JTextArea.class);
when(textArea.getText()).thenReturn("test");
Assert.assertEquals("test", textArea.getText());
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments