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?

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

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.

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

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

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?

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.

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Tuesday in Confluence

We want to see the templates you've created in Confluence!

Hi Community, Jessica here from the Confluence Product Marketing team!  July’s community challenge is all about sharing pictures  — and as an extension of our first post on what ...

387 views 17 9
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you