Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How can i make use of mockito to resolve a null pointer exception in my test junit method which are concerned with license of a plugin?

Neeraj Bodhe
Contributor
September 2, 2015

I mock ThirdPartyPluginLicenseStorageManager and PluginLicense.class using mockito in a junit test.

But while calling the internal methods of them it throws null pointer exception.

How can i overcome them as i dont see a way of setting the values in the mock confluence license objects

 

Few lines in my macro stub

@Mock
private ThirdPartyPluginLicenseStorageManager licenseManager;

@Before
public void setUp() throws Exception {
   super.setUp();
   exampleMacro = new Macro(licenseManager);
   PluginLicense pluginLicense = Mockito.mock(PluginLicense.class);
}

@Test
public void test() throws Exception {

   when(pluginLicense.getError().isDefined()).thenReturn(Boolean.FALSE); // Null pointer Exception
   when(licenseManager.getLicense().isDefined()).thenReturn(Boolean.TRUE); // Null pointer Exception

}

 

 

Few lines in my macro code which are throwing null pointer exception

private final ThirdPartyPluginLicenseStorageManager licenseManager;

public Macro(ThirdPartyPluginLicenseStorageManager licenseManager){
        this.licenseManager = licenseManager;
}

PluginLicense pluginLicense = licenseManager.getLicense().get();
if(licenseManager.getLicense().isDefined()) {
	if(pluginLicense.getError().isDefined()) {
	
	}
}

 

 

2 answers

2 votes
Panos
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 3, 2015

In the mock code you showed, in the setup method you mock a pluginLicense object. In test method you use another object.

Move this

PluginLicense pluginLicense = Mockito.mock(PluginLicense.class);

in the global declaration just below your licenseManager and delete any other pluginLicense that you have declared somewhere else.

Neeraj Bodhe
Contributor
September 3, 2015

Did that @Mock private PluginLicense pluginLicense; Same null pointer exception error.

Panos
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 4, 2015

PluginLicense pluginLicense = licenseManager.getLicense().get(); is this mocked?

Neeraj Bodhe
Contributor
September 4, 2015

i am new to mockito Can you just give me an example of how to mock this statement as there is no setter method like licenseManager.setLicense() to it. How to mock this?

1 vote
Panos
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 4, 2015

Generally the idea is like... when(mockedObject.method()).thenReturn(something) right? In your occassion, licenseManager.getLicense() returns an Option<PluginLicense> so the mock should be:

when(licenseManager.getLicense()).thenReturn(Option.option(pluginLicense));

This is untested though smile

 

In mockito you dont do setters. Lets say you have an interface method that with parameter string X returns object T and another method that with object T, returns string X.

the you would

Mockito.when(myInterface.myMethod(anyString())).thenReturn(T); //Notice the anyString() in this case i dont care what the param is.

the second case would be

Mockito.when(myInterface.myOtherMethod(T)).thenReturn(X);

To mock correctly i suggest you a) read documentation ofc and b) have the documentation of the method signatures on hand.

Geoff Williams
Contributor
August 29, 2019

This was helpful but I had to use PowerMock instead of Mockito because the `isDefined` method is final :(

 

I ended up making my own Mock version of PluginLicenseManager with a method like this:

 

@Override
public Option<PluginLicense> getLicense() {
Option<PluginLicense> option = PowerMockito.mock(Option.class);

PluginLicense pluginLicense = PowerMockito.mock(PluginLicense.class);
Option<PluginLicense> optionError = PowerMockito.mock(Option.class);
PowerMockito.doReturn(error).when(optionError).isDefined();
PowerMockito.doReturn(optionError).when(pluginLicense).getError();

PowerMockito.doReturn(licensed).when(option).isDefined();
PowerMockito.doReturn(pluginLicense).when(option).get();

return option;
}

 

error and license being simple booleans defined as part of the class.

 

Also had to make sure my test runner was annotated:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Option.class)

 

Still get some strange warnings from the depths of PowerMock but it works...

 

WARNING: Illegal reflective access by org.powermock.reflect.internal.WhiteboxImpl (file:/home/geoff/.m2/repository/org/powermock/powermock-reflect/2.0.2/powermock-reflect-2.0.2.jar) to method java.lang.Object.clone()
WARNING: Please consider reporting this to the maintainers of org.powermock.reflect.internal.WhiteboxImpl
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

 HTH

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, loom, loom for training, loom for teaching, video training, async learning, online education, screen recording, loom tutorials, loom use cases, atlassian learning, team training tools, instructional video, virtual training tools

🛗 Elevate Your Training and Enablement with Loom

Join us June 26, 11am PT for a webinar with Atlassian Champion Robert Hean & Loom’s Brittany Soinski. Hear tips, stories, and get your burning questions answered. Learn how Loom makes training and enablement easier. Don’t miss it!

Register today
AUG Leaders

Atlassian Community Events