DEV Community

Harit Himanshu
Harit Himanshu

Posted on

How to test Java Classes using JUnit5 and Reflection API

I have created a project which is intended to demonstrate the following using Java, Maven and JUnit5

  • If the Class exists in project.
  • If the class contains a method with a specific name
  • If a method is public, private
  • If a method contains parameters with specific types
  • If a method returns a specific type

And there could be more additions to such a list.

This project makes use of ReflectionUtils class from Junit5 and Java's Reflection API

The codebase and tests are available at GitHub

Top comments (4)

Collapse
 
siy profile image
Sergiy Yevtushenko •

What is the purpose of such a tests?

Collapse
 
harittweets profile image
Harit Himanshu •

This is so that a learner can make progress on a certain set of tasks and verify their progress. The audience is developers learning java and creating their first project

Collapse
 
nutterzuk profile image
Stephen Nutbrown •

This sounds like a bad idea. Your tests should be checking that your unit of code meets it's external facing contracts; it should not need to assess the internal private methods and fields.

Collapse
 
harittweets profile image
Harit Himanshu •

That depends on the use case. This is so that a learner can make progress on a certain set of tasks and verify their progress. The audience is developers learning java and creating their first project