I'm faced with a problem on how to cover methods of my innner classes. I have a small GUI (swing) application with several buttons and defined action listeners in form of inner classes. Now I want to write tests for each of the action listener methods defined in these inner classes but when I'm creating a new JUnit using Eclipse I don't have a option to cover methods of inner classes but the only public methods of the Class where those inners are defined. Please advice.
问题:
回答1:
Conceptually, inner methods are not generally tested with unit tests, because they are considered an implementation detail. You should be testing the behavior of your class by testing its public methods.
That said... http://www.artima.com/suiterunner/private.html
I'm not sure why you need inner classes...They're not used anywhere else? Why not just make them public classes outside of your main class?
回答2:
This means your code is not testable. Move your logic in separate classes, and unit-test them. (They can be package-private, for example, so that they are still not accessible to the outside world)
If your inner classes are logically inner - i.e. they represent some internal functioning of your class - don't unit test them, unit-test the parent class' methods.
回答3:
An instance of an inner class can only be created by its outer class. You have to write wrapper functions in the outer class or to make the inner class a normal one.
回答4:
AFAIK, GUI components are hard to unit test. If you really want to test them, maybe you could pull out the logic code into methods in the outer class and perform a method call in the inner class for the action. If your inner class actions are GUI updates, I'm not sure how they can be effectively tested.