views:

31

answers:

1

When it comes to GUI programming (at least for web) I feel that often the only thing that would be useful to unit test is some of the private methods*. While unit testing makes perfect sense for back-end code, I feel it doesn't quite fit the GUI classes.

What is the best way to add automatic testing of these?


* Why I think the only methods useful to test is private:

Often when I write GUI classes they don't even have any public methods except for the constructor. The public methods if any is trivial, and the constructor does most of the job calling private methods.

They receive some data from server does a lot of trivial output and feeds data to the constructor of other classes contained inside it, adding listeners that calls a (more or less directly) calls the server... Most of it pretty trivial (the hardest part is the layout: css, IE, etc.) but sometimes I create some private method that does some advanced tricks, which I definitely do not want to be publicly visible (because it's closely coupled to the implementation of the layout, and likely to change), but is sufficiently complicated to break. These are often only called by the constructor or repeatedly by events in the code, not by any public methods at all.

I'd like to have a way to test this type of methods, without making it public or resorting to reflection trickery.

(BTW: I'm currently using GWT, but I feel this applies to most languages/frameworks I've used when coding for GUI)

+1  A: 

Instead of insisting that everything must be private, you should consider adopting one of the many UI-related design patterns, such as MVC, MVP or MVVM.

You UI would delegate all logic to testable UI/Presentation-specific classes with public members. These implementations don't pollute your core Domain Model because they are still UI-specific and can be implemented in separate libraries.

You can unit test such a Presentation library to your heart's content, and also use it to perform Subcutaneous Tests.

Mark Seemann
Ok, nice answer. But I find it strange that unit testing should require that you violate the principle of information hiding.
Stein G. Strindhaug
I'm not aware of that principle, but unit testing fits perfectly with SOLID principles; particularly SRP and OCP.
Mark Seemann