简体   繁体   中英

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)?

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM