【问题标题】:Null Pointer Exception on getObjectValue() using mockito使用 mockito 的 getObjectValue() 上的空指针异常
【发布时间】:2014-09-12 01:52:23
【问题描述】:

代码片段

private AttributeCache attributeCache;
attributeCache = mock(AttributeCache.class);
ServiceAttribute serviceAttribute = new ServiceAttribute();
String serviceAttrId = "M";
when(attributeCache.get(serviceAttrId).getObjectValue()).thenReturn(serviceAttribute);

当方法抛出 Null 指针异常由于 getObjectValue(),当我删除 getObjectValue 时,它​​给了我一个将 serviceAttribute 类型更改为 Element 的错误?

任何更新!对于上述场景,我们如何使用 mockito?

在正常情况下,我们将对象投射如下

serviceAttribute = (ServiceAttribute) (attributeCache.get(serviceAttrId).getObjectValue());

【问题讨论】:

  • 模拟方法默认返回null(对于非数值类型的引用类型)。

标签: java mockito


【解决方案1】:

这里的问题是在您尝试模拟时调用attributeCache.get(serviceAttrId).getObjectValue()attributeCache.get(serviceAttrId) 部分将返回 null,它为您提供 NullPointerException。解决方案是这样的:

private AttributeCache attributeCache;
attributeCache = mock(AttributeCache.class);
ServiceAttribute serviceAttribute = new ServiceAttribute();
Attribute attribute = mock(Attribute.class);
when(attributeCache.get(Matchers.any(String.class)).thenReturn(attribute);
String serviceAttrId = "M";
when(attribute.getObjectValue()).thenReturn(serviceAttribute);

这是假设attributeCache.get(...) 返回Attribute 类型的东西;您必须将其替换为课程的实际类型。


编辑:我试图重现您在更改后的版本中遇到的错误,但没有成功。这是我的版本:

package com.stackoverflow.shahid.ghafoor;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MockStuff {

    public static void main(String[] args) {
        try {
            new MockStuff().run();
            System.out.println("Everything's fine");
            } catch(Exception e) {
                System.err.println("Caught an error:");
                e.printStackTrace();
        }
    }

    public MockStuff() {
    }

    public void run() {
            AttributeCache attributeCache;
        attributeCache = mock(AttributeCache.class);
            ServiceAttribute serviceAttribute = new ServiceAttribute();
        Attribute attribute = mock(Attribute.class);
            when(attributeCache.get(any(String.class))).thenReturn(attribute);
        String serviceAttrId = "M";
            when(attribute.getObjectValue()).thenReturn(serviceAttribute);
    }

    private class AttributeCache {
        Attribute get(String something) {
            return null;
            }
    }

    private class Attribute {
        ServiceAttribute getObjectValue() {
            return null;
        }
    }

    private class ServiceAttribute {

    }
}

当然有可能,您在这里遇到了 Mockito 的限制;如果是这样切换

Mockito.when(attribute.getObjectValue()).thenReturn(serviceAttribute)

Mockito.doReturn(serviceAttribute).when(attribute).getObjectValue()

可能会有所帮助,具体取决于问题所在。

【讨论】:

  • 感谢您的回复!现在再次在这一行出现异常when(attribute.getObjectValue()).thenReturn(serviceAttribute);
  • @ShahidGhafoor 你得到什么样的异常?
  • 这个org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: when(mock.getArticles()).thenReturn(articles);
  • @ShahidGhafoor 请更新您的问题以包含更改的代码和新的异常;现在我在猜测可能出了什么问题。但是一般来说,您尝试在那里模拟的内容存在问题。
  • 代码 sn-p,您输入的答案与我的代码相同。 :)
猜你喜欢
  • 2015-10-07
  • 1970-01-01
  • 2014-11-13
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
  • 2019-05-24
  • 2021-06-15
相关资源
最近更新 更多