Expand my Community achievements bar.

how to write junit for this code

Avatar

Level 4

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();
   
}
5 Replies

Avatar

Community Advisor

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

Avatar

Level 8

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

Avatar

Level 4

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

Avatar

Level 8

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

Avatar

Level 5

Hi @djohn98390536 ,

Usually, if it's hard to cover logic in private methods it means that code requires refactoring, because of complexity, nesting, etc.

However, for example, if you test OSGi component that has interface and implementation (either it impl package or in internal), you can change private to package-private access modifier. It will allow you to write more tests for your method.