简体   繁体   English

使用Mockito的getObjectValue()空指针异常

[英]Null Pointer Exception on getObjectValue() using mockito

Code Snippet 代码段

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

When method throwing Null pointer exception due to getObjectValue(), when I remove getObjectValue, it gave me an error that change type of serviceAttribute to Element? 当方法由于getObjectValue()而引发Null指针异常时,当我删除getObjectValue时,它给了我一个错误,该错误将serviceAttribute的类型更改为Element?

any update! 任何更新! How we can use mockito with respect to above scenerio? 关于上述场景,我们如何使用Mockito?

in normal scenario we cast the object as follow 在正常情况下,我们将对象投射如下

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

The problem here is in calling attributeCache.get(serviceAttrId).getObjectValue() in your attempt to mock; 这里的问题是在尝试模拟时调用attributeCache.get(serviceAttrId).getObjectValue() the part attributeCache.get(serviceAttrId) will return null which gives you the NullPointerException . 该部件attributeCache.get(serviceAttrId)将返回null ,这将为您提供NullPointerException A solution would be something like this: 解决方案将是这样的:

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);

This is assuming that attributeCache.get(...) returns something of the type Attribute ; 这是假设attributeCache.get(...)返回的Attribute类型为Attribute you'll have to replace it with the actual type of course. 您必须将其替换为实际的课程类型。


EDIT: I've tried to reproduce the error you get in the changed version without any success. 编辑:我试图重现您在更改的版本中获得的错误,但没有成功。 Here's my version: 这是我的版本:

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 {

    }
}

It is of course possible, that you've run into a limitation of Mockito here; 当然,您可能在这里遇到了Mockito的限制; if so switching from 如果是这样从

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

to

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

might help, depending on what exactly the problem is. 可能会有所帮助,具体取决于问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM