How to test Java Classes using JUnit5 and Reflection API
Harit Himanshu

Harit Himanshu @harittweets

About: I am hands-on software professional with more than a decade of experience in building and shipping products at companies of small to large sizes. contributed in Advertising,CleanTech,Security,FinTech

Location:
Victoria, BC
Joined:
Sep 3, 2019

How to test Java Classes using JUnit5 and Reflection API

Publish Date: Apr 18 '20
5 4

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

Comments 4 total

  • Sergiy Yevtushenko
    Sergiy YevtushenkoApr 18, 2020

    What is the purpose of such a tests?

    • Harit Himanshu
      Harit HimanshuApr 24, 2020

      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

  • Stephen Nutbrown
    Stephen NutbrownApr 18, 2020

    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.

    • Harit Himanshu
      Harit HimanshuApr 24, 2020

      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

Add comment