【问题标题】:Can't return Class Object with Mockito无法使用 Mockito 返回类对象
【发布时间】:2013-06-03 04:47:41
【问题描述】:

我正在尝试编写一个单元测试,为此我正在为 Mockito 模拟编写一个 when 语句,但我似乎无法让 eclipse 认识到我的返回值是有效的。

这就是我正在做的事情:

Class<?> userClass = User.class;
when(methodParameter.getParameterType()).thenReturn(userClass);

.getParameterType()的返回类型是Class&lt;?&gt;,所以我不明白eclipse为什么说,The method thenReturn(Class&lt;capture#1-of ?&gt;) in the type OngoingStubbing&lt;Class&lt;capture#1-of ?&gt;&gt; is not applicable for the arguments (Class&lt;capture#2-of ?&gt;)。它提供了转换我的 userClass,但这只会使一些乱码 eclipse 说它需要再次转换(并且不能转换)。

这只是 Eclipse 的问题,还是我做错了什么?

【问题讨论】:

    标签: java junit mockito


    【解决方案1】:

    此外,解决此问题的一种更简洁的方法是使用 do 语法而不是 when。

    doReturn(User.class).when(methodParameter).getParameterType();
    

    【讨论】:

    • 这是最干净的解决方案。
    • 真棒......是的,最干净的解决方案。非常感谢。
    【解决方案2】:
    Class<?> userClass = User.class;
    OngoingStubbing<Class<?>> ongoingStubbing = Mockito.when(methodParameter.getParameterType());
    ongoingStubbing.thenReturn(userClass);
    

    Mockito.when 返回的 OngoingStubbing&lt;Class&lt;?&gt;&gt;ongoingStubbing 的类型不同,因为每个 '?'通配符可以绑定到不同的类型。

    要使类型匹配,您需要显式指定类型参数:

    Class<?> userClass = User.class;
    Mockito.<Class<?>>when(methodParameter.getParameterType()).thenReturn(userClass);
    

    【讨论】:

    • 零件:Mockito.&lt;Class&lt;?&gt;&gt;when 是关键。感谢您的回答。
    • 在我看来,使用显式键入是比当前投票的“正确”答案更优雅的解决方案
    【解决方案3】:

    我不确定您为什么会收到此错误。返回Class&lt;?&gt; 一定有什么特别之处。如果您返回 Class,您的代码编译得很好。这是对您正在做的事情的模拟,并且此测试通过了。我认为这对你也有用:

    package com.sandbox;
    
    import org.junit.Test;
    import org.mockito.invocation.InvocationOnMock;
    import org.mockito.stubbing.Answer;
    
    import static org.mockito.Mockito.*;
    
    import static junit.framework.Assert.assertEquals;
    
    public class SandboxTest {
    
        @Test
        public void testQuestionInput() {
            SandboxTest methodParameter = mock(SandboxTest.class);
            final Class<?> userClass = String.class;
            when(methodParameter.getParameterType()).thenAnswer(new Answer<Object>() {
                @Override
                public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                    return userClass;
                }
            });
    
            assertEquals(String.class, methodParameter.getParameterType());
        }
    
        public Class<?> getParameterType() {
            return null;
        }
    
    
    }
    

    【讨论】:

    • 是的,看来这一定是 eclipse 或 mockito 的问题。我能够实施您的建议,并且解决了这个问题,所以谢谢!
    • @CorayThan 这不是 Eclipse。这也不能在 Intellij 中编译。
    • 在 Intellij Idea 中相同
    • 原发帖人没有提到他使用的是什么 JDK,但我相信这是由于 Java 8 中更强大的类型检查,因此不会是由于 eclipse 或 Intellij。我在将工作单元测试从 Java 7 升级到 Java 8 时遇到了很多问题(尽管对原始类型充满了 @SuppressWarnings)。更多细节在这里:mail.openjdk.java.net/pipermail/compiler-dev/2013-November/…
    • 谢谢。今天的 lambdas 更好:when(methodParameter.getParameterType()).thenAnswer((Answer) invocationOnMock -> String.class);
    【解决方案4】:

    你可以简单地从类中删除))

    Class userClass = User.class;
    when(methodParameter.getParameterType()).thenReturn(userClass);
    

    【讨论】:

      【解决方案5】:

      我发现这里的代码示例与第一次在已接受答案的 SandBoxTest 中使用的 methodParameter.getParameterType() 的使用有些混淆。在我做了更多的挖掘之后,我发现another thread pertaining to this issue 提供了一个更好的例子。这个例子清楚地表明了我需要的 Mockito 调用是 doReturn(myExpectedClass).when(myMock).callsMyMethod(withAnyParams)。使用该表单可以让我模拟 Class 的返回。希望这篇笔记能帮助将来搜索类似问题的人。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-28
        • 1970-01-01
        • 2011-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多