while writing junit getting below error | Community
Skip to main content
djohn98390536
New Participant
November 12, 2024
Solved

while writing junit getting below error

  • November 12, 2024
  • 3 replies
  • 1226 views

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

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by EstebanBustamante

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.

3 replies

EstebanBustamante
EstebanBustamanteAccepted solution
New Participant
November 12, 2024

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
djohn98390536
New Participant
November 13, 2024

@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());

EstebanBustamante
New Participant
November 13, 2024
arunpatidar
New Participant
November 12, 2024

Hi @djohn98390536 

Where did you set the property value to node?

 

check with code?

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

Arun Patidar
konstantyn_diachenko
New Participant
November 12, 2024

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

Kostiantyn DiachenkoCheck out AEM VLT Intellij plugin
djohn98390536
New Participant
November 12, 2024

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

konstantyn_diachenko
New Participant
November 12, 2024

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?

Kostiantyn DiachenkoCheck out AEM VLT Intellij plugin