Applying TDD on MVC
I am struggling to find good content of MVC and TDD. I just want to learn how to effectively write unit tests for the MVC design pattern. I would appreciate any insights or links to useful resources. Which parts of the MVC are ideal for unit testing? Any benefits on unit testing the vie开发者_高级运维w, and if so how?
It's best to apply the unit tests to all three - the model, the view, and the controller. This is straightforward for the model and the controller, but can be difficult for the view portion. The MVP pattern makes this somewhat easier, since the view in MVP is typically thinner than in MVC.
To complement the unit tests, you'll want some integration testing - a broad topic, covering everything from testing the interaction of a handful of objects to testing a complete system. Automated acceptance tests can be helpful, too.
As far as whether to unit test any given of the M, V, or C: It boils down to the economics involved in achieving the level of quality you need to deliver. Typically, it's cheaper in the long run (think regression testing) to have each part unit tested than it is to manually re-test all the components. In fact, far and away cheaper. Think of the cost involved in manually testing re-testing hundreds of classes, when you could have written units tests along the way.
Specifically for the view, still thinking economics and quality, if that layer is very thin, it might be that Selenium or similar tools can give you the automation you want for testing the view.
Ruby on Rails has both MVC and unit testing baked in (I know your question is about Java, but at a high level the approach should be largely the same).
Unit tests are pretty much exclusively to do with testing the Model (The M in MVC).
Then there are functional tests which test the Controller (the C).
If you want to test the V, then a tool such as Selenium is a very good bet.
This is a good article about testing in Rails: http://guides.rubyonrails.org/testing.html
精彩评论