简体   繁体   中英

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?

any update! How we can use mockito with respect to above scenerio?

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; the part attributeCache.get(serviceAttrId) will return null which gives you the 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 ; 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; 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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