Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Private class unit tests

How to unit test private (means with package visibility) classed in java?

I have a package, only one class is public in this package, other classes are private. How to cover other classed with unit tests? I would like to include unit tests in resting jar.

like image 405
Sergey Pereverzov Avatar asked Aug 29 '13 09:08

Sergey Pereverzov


2 Answers

Create the unit test class in the same package.

For example, If com.example.MyPrivateClass located in src/main/java/com/example/MyPrivateClass.java

Then the test class will be in same package com.example.MyPrivateClassTestCase and will be located in src/test/java/com/example/MyPrivateClassTestCase.java

like image 178
Kowser Avatar answered Sep 27 '22 23:09

Kowser


As indicated in @Kowser's answer, the test can be in the same package.

In Eclipse, and I assume other IDEs, one can have classes in different projects but in the same package. A project can be declared to depend on another project, making the other project's classes available. That permits a separate unit test project that depends on the production project and follows its package structure, but has its own root directory.

That structure keeps the test code cleanly separated from production code.

like image 35
Patricia Shanahan Avatar answered Sep 27 '22 22:09

Patricia Shanahan