nickylin / powermock

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

ClassCastException when mocking multiple interface with mockito #488

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
The following test case can not pass with PowerMock 1.4.11 + Mockito 1.8.5 or 
PowerMock 1.5.4 + Mockito 1.9.5. ClassCastException occurs.

    @Test
    public void test_mock_object_with_multiple_interfaces() throws Exception {
        Runnable runnable = PowerMockito.mock(Runnable.class, Mockito.withSettings().extraInterfaces(Iterator.class));
        @SuppressWarnings("unchecked")
        Iterator<Object> iterator = (Iterator<Object>) runnable;
        PowerMockito.when(iterator.next()).thenReturn("test1", "test2");
        PowerMockito.doThrow(new IllegalStateException()).when(runnable).run();

        Assert.assertEquals("test1", iterator.next());
        Assert.assertEquals("test2", iterator.next());
        try {
            runnable.run();
            Assert.fail("Exception expected.");
        } catch (IllegalStateException e) {
        }
    }

  If using Mockito API as below with Mockito 1.8.5, it can pass, but with Mockito 1.9.5 it fails with MethodNotFoundException. It seems a bug of Mockito.

    @Test
    public void test_mock_object_with_multiple_interfaces() throws Exception {
        Runnable runnable = Mockito.mock(Runnable.class, Mockito.withSettings().extraInterfaces(Iterator.class));
        @SuppressWarnings("unchecked")
        Iterator<Object> iterator = (Iterator<Object>) runnable;
        Mockito.when(iterator.next()).thenReturn("test1", "test2");
        Mockito.doThrow(new IllegalStateException()).when(runnable).run();

        Assert.assertEquals("test1", iterator.next());
        Assert.assertEquals("test2", iterator.next());
        try {
            runnable.run();
            Assert.fail("Exception expected.");
        } catch (IllegalStateException e) {
        }
    }

Original issue reported on code.google.com by gavinyan...@gmail.com on 21 Mar 2014 at 2:06

GoogleCodeExporter commented 9 years ago
Does this test pass with vanilla Mockito?

Original comment by szcze...@gmail.com on 8 Oct 2014 at 7:17