how to write junit for this code | Community
Skip to main content
djohn98390536
Level 4
November 12, 2024
Solved

how to write junit for this code

  • November 12, 2024
  • 2 replies
  • 1052 views

I am not able to write the junit for private method

Node metaNode = session.getNode(metadataNode);
getPropeties(metaNode, generatedPath, workItem, wfsession);//till this its working
 
private void getPropeties(Node metadataNode,String generatedPath,WorkItem workItem,WorkflowSession wfsession) throws ValueFormatException, PathNotFoundException, RepositoryException, WorkflowException {
 
String prop1=metadataNode.getProperty("prop1").getValue().toString();
   
}
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 daniel-strmecki

Hi @daniel-strmecki while writing  junit for public method this private method not getting covered and coverage is getting down. if u hv any example please share


Hi @djohn98390536,

then you should write an additional test case for your public method that covers the private method as well. Private methods serve no purpose if they are not used in any public methods. The best practice in unit and integration testing is to test the APIs you expose, therefore, your public methods.

 

Good luck,

Daniel

2 replies

Prince_Shivhare
Community Advisor
Community Advisor
November 12, 2024

Please try PowerMockito.
It helps to write the Junit for Private Or Static.

daniel-strmecki
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
November 12, 2024

Hi @djohn98390536,

PowerMockito should not be used anymore as it is deprecated and not maintained. You shouldn't write any unit tests for private methods; instead, focus on writing tests for the public methods that internally use the private methods.

 

Good luck,

Daniel

djohn98390536
Level 4
November 12, 2024

Hi @daniel-strmecki while writing  junit for public method this private method not getting covered and coverage is getting down. if u hv any example please share

kautuk_sahni
Community Manager
Community Manager
November 25, 2024

@djohn98390536 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!

Kautuk Sahni