vijayjcp / powermock

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

Verification of instance methods in final system classes is not possible in the Mockito API extension #297

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
    @Test
    public void mockingURLWorks() throws Exception {
        URL url = mock(URL.class);
        URLConnection urlConnectionMock = mock(URLConnection.class);

        when(url.openConnection()).thenReturn(urlConnectionMock);

        URLConnection openConnection = url.openConnection();

        assertSame(openConnection, urlConnectionMock);
        verify(url).openConnection(); // This causes the problem because url is NOT a mock.
    }

Possible solution: We could create a replica class which we mock and return 
which delegates all instance calls to the final system class instance.

Original issue reported on code.google.com by johan.ha...@gmail.com on 2 Dec 2010 at 2:14

GoogleCodeExporter commented 8 years ago
Does it support currently?

Original comment by datangli...@gmail.com on 18 Mar 2011 at 10:08

GoogleCodeExporter commented 8 years ago
This is not implemented yet and unfortunately it's not prioritized either. But 
that could change if there are many people interested in the fix.

Original comment by johan.ha...@gmail.com on 18 Mar 2011 at 10:15

GoogleCodeExporter commented 8 years ago

Original comment by johan.ha...@gmail.com on 10 Apr 2011 at 12:48

GoogleCodeExporter commented 8 years ago
I have a similar problem with mocking UUID classes.

UUID uuid = org.powermock.api.mockito.PowerMockito.mock(UUID.class) returns an 
actual UUID instance and not a mock.

Hope priority can be reaised for this issue.

Original comment by marceloverdijk on 2 Nov 2011 at 8:34

GoogleCodeExporter commented 8 years ago
Please increase its priority. I'm developing Intellij IDEA plugin, and this bug 
prevents me from testing a lot of methods (IDEA api uses static stuff a lot :-( 
).

Original comment by m.jedy...@gmail.com on 19 Nov 2011 at 4:36

GoogleCodeExporter commented 8 years ago
It should be possible to verify methods in the IDEA api. The issue here is only 
for final system classes (i.e. classes in the "java.*" package (e.g. 
java.lang)). 

Original comment by johan.ha...@gmail.com on 19 Nov 2011 at 5:15

GoogleCodeExporter commented 8 years ago
A lot of static methods in IDEA api has inside calls to java.lang. 

I'm getting:
java.lang.VerifyError: (class: javax/swing/plaf/metal/MetalLookAndFeel, method: 
getLayoutStyle signature: ()Ljavax/swing/LayoutStyle;) Wrong return type in 
function
    at javax.swing.UIManager.setLookAndFeel(UIManager.java:554)
    at javax.swing.UIManager.initializeDefaultLAF(UIManager.java:1317)
    at javax.swing.UIManager.initialize(UIManager.java:1406)
    at javax.swing.UIManager.maybeInitialize(UIManager.java:1394)
    at javax.swing.UIManager.getUI(UIManager.java:980)
    at javax.swing.JLabel.updateUI(JLabel.java:256)
    at javax.swing.JLabel.<init>(JLabel.java:145)
    at javax.swing.JLabel.<init>(JLabel.java:216)
    at com.intellij.openapi.util.IconLoader.<clinit>(IconLoader.java:50)
    at com.intellij.openapi.ui.DialogWrapper.<clinit>(DialogWrapper.java:202)
I thought that it's because of this issue, maybe I'm wrong?

Original comment by m.jedy...@gmail.com on 26 Nov 2011 at 1:56

GoogleCodeExporter commented 8 years ago
But can't you mock the IDEA classes instead? Or are you writing some sort of 
integration test?

Original comment by johan.ha...@gmail.com on 28 Nov 2011 at 7:20

GoogleCodeExporter commented 8 years ago
Sorry, I made wrong assumptions and didn't express myself clearly.
In my production code I'm instantiating 
com.intellij.ide.util.PackageChooserDialog class (I can't use any dependency 
injection framework, so I separated it to a factory). In my test I want to mock 
instantiation of it using 'whenNew'. In order to do that, I need to return a 
mock of this class, but when I'm trying to mock it 
(Mockito.mock(PackageChooserDialog.class)) it throws the stack from above. When 
I'm using Mockito alone (without Powermock with PrepareForTest annotation), 
mock is created correctly (but I cannot use 'whenNew').
Should be probably reported as a new issue?  

Original comment by m.jedy...@gmail.com on 30 Nov 2011 at 12:36