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
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Hi @djohn98390536 , it means that `node` is null in this case. Please, check why your node is null and mock or stub it.
Views
Replies
Total Likes
Hi @konstantyn_diachenko i am getting node value it not null but still getting the above error
Views
Replies
Total Likes
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?
Views
Replies
Total Likes
Hi @djohn98390536
Where did you set the property value to node?
check with code?
node.setProperty("outputPath","somevale")
Views
Replies
Total Likes
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.
@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());
Views
Replies
Total Likes
I think you got a reply here: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/how-to-write-junit-for-the...
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies