Do I need to focus on testing "public" methods or "private" methods in a JUnit test?

- most JUnit tests written for public methods or private methods? What do I need to focus on if I don't have much time?

+3


source to share


4 answers


You need to test everything that is part of the API. If you are not using reflection it means public / protected and package level methods.

This is clearly an opinion. But it is based on experience. Let me give my opinion further.

Ideally, you should practice test-driven development. In this practice, you:



  • First, write the tests, including the publicly available test subject methods that you want to have. This will help you develop a useful API.
  • Then you write enough code to pass the tests. This will leave you with 100% coverage and no extraneous code.
  • Then you are refactoring.

Until you get to the refactoring stage, you probably won't even have private methods.

+4


source


You are testing the behavior of the classes. Start with a public method. If you cover all the states of an object , chances are you include private methods. The goal of 100% coverage by the state (just the estimate is not realistically achievable in practical cases)



+1


source


Depends on your testing goal. Ideally, you want to test all of them with 100% coverage :)

0


source


I doubt you will see a lot of private methods floating around and as they say they will appear in a method where variables should not be changed outside / outside of special circumstances. That being said, the main difference between public and private is accessibility: the private can only be accessed within the class, while the public can be accessed outside the class.

0


source







All Articles