4

I have a package private implementation of an Interface, the methods defined in that Interface can be accessed through a public factory. I was just wondering, how should I unit test that class as it cannot be accessed outside the package, or should I be testing it through the factory?

Thanks

-Abidi

Abidi
  • 7,846
  • 14
  • 43
  • 65

1 Answers1

10

The usual convention is to put the unit test of a class in the same package as the class under test. This allows the unit test to access package-private and protected methods of the class, and thus to test them.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thank you for your answer. If I do that, Would "maven test" execute tests from src directory? – Abidi Oct 08 '11 at 14:42
  • 5
    Putting them in the same package doesn't mean putting them in the same source directory. Just have the same tree of packages (and thus directories) in your main/src/java directory and in your test/src/java directory. – JB Nizet Oct 08 '11 at 14:45
  • @JB Nizet, Thanks for this answer. I was stumbling on this as Guice manual asked for package private and I was wondering how to test those classes. – kaushik Jul 15 '13 at 11:43