Unit test package protected classes without mock

526 views Asked by At

I am currently using Java for an application and I want to write unit tests for this application. I have some classes in different packages in the application and some of these classes have the access modifier /* package protected */, i.e no access modifier. I would very much like to keep this but how is this handled for tests, since they are not located in the same package? Will this complicate things? If the alternative is to use some mock libraries, it's just not worth it and I'll make my classes public.

I'm using Android studio and the application is an Android app, by the way.

1

There are 1 answers

0
GhostCat On BEST ANSWER

As important as mocking is to write useful unit tests; you better stay away from "mocking" package structure problems.

Probably you should first read some more basic material regarding unit test. It is a very basic convention, that the packages for your unit tests perfectly resemble the package of the corresponding class under test.

Meaning: your production code might be under src/my/package/A.java and your test code could reside under test/my/package/ATest.java

See Java unit tests, directory layout for more background