具有复杂泛型类层次结构的ClassCastException和NoSuchMethodException

时间:2014-05-02 21:41:33

标签: java generics jmockit

以下测试类包含我项目的复杂类层次结构的简化版本,该版本基于泛型。所有3项测试genericsFailure_NoSuchMethod()genericsFailure_ClassCast1()genericsFailure_ClassCast2()都可以通过 JMockit 1.6 完成,但是从 JMockit 1.7和1.8 开始从JMockit中获取不同种类的ClassCastExceptionNoSuchMethodException

package tests;

import mockit.Cascading;
import mockit.Mocked;
import mockit.NonStrictExpectations;

import org.testng.annotations.Test;

public class JMockitTests
{
    public static interface IAppBound<APP extends IApp<?, ?>>
    {
        APP getApp();
    }

    public static interface IBase<APP extends IApp<APP, ?>> extends IAppBound<APP>
    {

    }

    public static interface IApp<APP extends IApp<APP, ?>, BACK extends IBack<APP>> extends IBase<APP>
    {
        BACK getBack();
    }

    public static interface IBack<APP extends IApp<APP, ?>> extends IBase<APP>
    {

    }

    public static abstract class BaseImpl<APP extends AppImpl<APP, ?>> implements IBase<APP>
    {
        private APP app;

        @Override
        public APP getApp()
        {
            return app;
        }
    }

    public static class AppImpl<APP extends AppImpl<APP, ?>, BACK extends BackImpl<APP>> extends BaseImpl<APP> implements IApp<APP, BACK>
    {
        private BACK    b;

        @Override
        public BACK getBack()
        {
            return b;
        }
    }

    public static class BackImpl<APP extends AppImpl<APP, ?>> extends BaseImpl<APP> implements IBack<APP>
    {

        private Registry<APP, BackImpl<APP>>    reg;

        public Registry<APP, BackImpl<APP>> getRegistry()
        {
            return reg;
        }

        @Override
        public APP getApp()
        {
            return super.getApp();
        }
    }

    public static class Registry<APP extends AppImpl<APP, ?>, BACK extends BackImpl<APP>>
    {
        public BACK getBack(APP app)
        {
            return null;
        }

        public BACK getBack(BACK app)
        {
            return null;
        }

        public BACK getBack(String app)
        {
            return null;
        }
    }

    public static class SpecificApp extends AppImpl<SpecificApp, BackImpl<SpecificApp>>
    {

    }

    @Test
    public void genericsFailure_NoSuchMethod(@Mocked @Cascading AppImpl<?, ?> app, @Mocked @Cascading IBack<?> back)
    {
        new NonStrictExpectations()
        {
            {
                back.getApp();
                result = app;
            }
        };
    }

    @Test
    public void genericsFailure_ClassCast1(@Mocked @Cascading BackImpl<SpecificApp> back)
    {
        SpecificApp app = new SpecificApp();

        new NonStrictExpectations()
        {
            {
                back.getRegistry().getBack(app);
                result = back;
            }
        };
    }

    @Test
    public void genericsFailure_ClassCast2(@Mocked @Cascading BackImpl<SpecificApp> back)
    {
        SpecificApp app = new SpecificApp();

        new NonStrictExpectations()
        {
            {
                back.getApp();
                result = app;
            }
        };
    }
}

我认为JMockit在处理泛型的过程中发生了一些变化,或者我完全忽略了JMockit的基本概念。可以肯定的是,我再次咨询了version historydocumentation,但没有任何事情引起我的注意。

我必须提一下,我已经用 JDK8和JDK7 对此进行了测试,但问题仍然存在。我正在运行TestNG(6.8.6)作为eclipse插件。

以下是TestNG的测试结果:

FAILED: genericsFailure_ClassCast1(tests.JMockitTests$BackImpl@883ce7)
java.lang.ClassCastException: java.lang.Object cannot be cast to tests.JMockitTests$BackImpl
    at tests.JMockitTests$Registry.getBack(JMockitTests.java)
    at tests.JMockitTests$2.<init>(JMockitTests.java:113)
    at tests.JMockitTests.genericsFailure_ClassCast1(JMockitTests.java:110)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:483)

FAILED: genericsFailure_ClassCast2(tests.JMockitTests$BackImpl@a7e666)
java.lang.ClassCastException: sun.reflect.generics.reflectiveObjects.TypeVariableImpl cannot be cast to java.lang.Class
    at tests.JMockitTests$BackImpl.getApp(JMockitTests.java)
    at tests.JMockitTests$3.<init>(JMockitTests.java:127)
    at tests.JMockitTests.genericsFailure_ClassCast2(JMockitTests.java:124)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:483)

FAILED: genericsFailure_NoSuchMethod(tests.JMockitTests$AppImpl@4f6be2, tests.$Impl_IBack@aac163)
java.lang.RuntimeException: java.lang.NoSuchMethodException: tests.JMockitTests$IBack.getApp()
    at tests.JMockitTests$1.<init>(JMockitTests.java:99)
    at tests.JMockitTests.genericsFailure_NoSuchMethod(JMockitTests.java:96)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:483)
Caused by: java.lang.NoSuchMethodException: tests.JMockitTests$IBack.getApp()
    at java.lang.Class.getDeclaredMethod(Class.java:2117)
    ... 4 more

如果有人能够批准这个问题,或者可以指出我的解决方案,我将感激不尽。

编辑:我已在项目上打开issue (issue 350)以进一步跟踪此问题。

1 个答案:

答案 0 :(得分:1)

我很惊讶这些测试在JMockit 1.6中有效,因为它在级联模拟过程中对泛型的明确支持没有多少。

无论如何,自1.7以来的变化与issue 333有关,而{{3}}仍然是开放的。不过,这两个问题都将在JMockit 1.9中得到解决。