Reputation: 709
I have two classes Parameter and ParameterList. The ParameterList class is an ArrayList made up of Parameters. There is a method getParameterByKey that finds the parameter in the list by key value. I don't want to mock Parameter.getValueString() since I want the value to be different depending on the key and that would require mocks for each key/value. Any input is appreciated.
I want to say go get the key "A431" and return "BOSTON" (etc) but I am getting "java.lang.NullPointerException".
when(mockedParmList.getParameterByKey("A431").getValueString()).thenReturn("BOSTON");
when(mockedParmList.getParameterByKey("A432").getValueString()).thenReturn("NYC")
Upvotes: 0
Views: 88
Reputation: 486
You have to mock the call to mockedParmList.getParameterByKey(). Otherwise, it's returning null, and then you're calling getValueString() on it, which gets you to the NPE.
when(mockedParmList.getParameterByKey("A431")).thenReturn(new Parameter(...))
Either you can use an actual Parameter value (probably), or you can mock the Parameter that you're going to return.
Upvotes: 1