Expand my Community achievements bar.

SOLVED

while writing junit getting below error

Avatar

Level 4

Node node = session.getNode(path);

node.getProperty("outputPath").setValue("somepath");

 

Junit

Property property=mock(Property.class);

lenient().when(node.getProperty("outputPath")).thenReturn(property);

 

error

java.lang.NullPointerException: Cannot invoke "javax.jcr.Property.setValue(String)" because the return value of "javax.jcr.Node.getProperty(String)" is null

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

 

Firstly, you should not mock a node or a property. Instead, you should load them using AEMContext. The less you mock, the better your test will be. In this scenario, I don't think mocking is the correct approach. Instead, use something like context.load() to create the node and allow the test to run, letting the testing framework is intended to do. Here some references: https://wcm.io/testing/aem-mock/usage-content-loader-builder.html

 

Secondly, the error clearly indicates that the getProperty() method is returning null. In other words, the property "outputPath" does not exist. This could mean the property is not correctly mocked, or the "node" under test is not the same one you're using in your "lenient" method. Since I don’t see your full mock setup and lenient method seems OK, I suspect the error is that the "node" under test (when the test runs) is not the same one you're using to mock the getProperty() method. To fix this, you should mock a "node" and return that mocked "node" as the result of session.getNode(path).

 

Hope this helps.



Esteban Bustamante

View solution in original post

7 Replies

Avatar

Level 7

Hi @djohn98390536 , it means that `node` is null in this case. Please, check why your node is null and mock or stub it.

Avatar

Level 4

Hi @konstantyn_diachenko  i am getting node value it not null but still getting the above error

Hi @djohn98390536 , I am sorry for confusing. Error means property that you get from the node is null.

 

Can you please provide full mock setup for this test?

Avatar

Community Advisor

Hi @djohn98390536 

Where did you set the property value to node?

 

check with code?

node.setProperty("outputPath","somevale")



Arun Patidar

Avatar

Correct answer by
Community Advisor

Hi,

 

Firstly, you should not mock a node or a property. Instead, you should load them using AEMContext. The less you mock, the better your test will be. In this scenario, I don't think mocking is the correct approach. Instead, use something like context.load() to create the node and allow the test to run, letting the testing framework is intended to do. Here some references: https://wcm.io/testing/aem-mock/usage-content-loader-builder.html

 

Secondly, the error clearly indicates that the getProperty() method is returning null. In other words, the property "outputPath" does not exist. This could mean the property is not correctly mocked, or the "node" under test is not the same one you're using in your "lenient" method. Since I don’t see your full mock setup and lenient method seems OK, I suspect the error is that the "node" under test (when the test runs) is not the same one you're using to mock the getProperty() method. To fix this, you should mock a "node" and return that mocked "node" as the result of session.getNode(path).

 

Hope this helps.



Esteban Bustamante

Avatar

Level 4

@EstebanBustamante thanks now its working fine i was using the wrong session.  How to write junit for this 

wfsession.complete(workItem, (wfsession.getRoutes(workItem,false)).get(0));

wfsession.suspendWorkflow(workItem.getWorkflow());