WelcomeTo
WelcomeTo

Reputation: 20591

Is it required to test public inner classes?

Should I test public inner classes public methods ? Or I should test outer class public methods, which uses inner class (same approach as testing private methods)?

Upvotes: 2

Views: 151

Answers (4)

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79875

You should test that your classes meet all of their requirements, regardless of how those requirements have been implemented. Do NOT write a test for each method; rather, work out what tests you need based on what requirements have been implemented, and whether there are different cases that are covered by each requirement. Whether you have written inner classes, private methods and so forth is kind of irrelevant to the testing process.

Upvotes: 0

Guillaume
Guillaume

Reputation: 22822

Nothing is "required". You need to make sensible testing, but focus on functional testing, not lines coverage: everything your code does should be tested one way or another.

If there's any business logic in your inner class methods, then it may help testing these directly (and turning the inner to a top level class). Depending on what it does, testing the outside may be enough, if you do that you introduce coupling in your test coverage, but as the class is an inner class the coupling is already there anyway.

Upvotes: 2

Goran Jovic
Goran Jovic

Reputation: 9518

If the outer class method merely delegates the call to the inner class one, you may test just one of them. It doesn't really matter which one. In this case you may want to rethink your design though - excessive delegation implies bad design in my experience.

If the outer class method does something else which is significant other than just calling the inner class method, then you should test them both separately like this:

  • Test just inner class method
  • Test just outer class method with inner class method mocked (if possible)
  • Test outer class method

Upvotes: 3

Matthew Farwell
Matthew Farwell

Reputation: 61715

You need to test everything that is accessible from outside your class. This includes public methods of inner classes (whether or not the classes themselves are public or private).

If you're exposing a method, you need to test it.

Upvotes: 3

Related Questions