Struts2 action - test it or not?

2019-05-17 06:36发布

问题:

I'm currently working on one project, that uses Struts2 framework. We use separate component for DB accessing, which is well tested. At the same time, project, that we work on has a lot of Actions, that are not tested. In most of the actions we use at least one DB-service call. So on one hand these actions are pretty simple. I'm not sure - should unit tests be written for that or not?

I think that good practice is write unit tests always, but these actions are so simple and I'm under big pressure from management side right now. So, is it critical or not - to leave Struts2 actions without unit tests?

回答1:

Here are the three main reasons for writing unit tests.

  • It helps you know that your code works now.
  • It helps defend your code against regression errors, when functionality is added or changed in the future.
  • If you write unit tests before you write your code, it focuses your design process in a really good way - read up on TDD to learn more about the benefits of doing this.

So ask yourself whether any of these three reasons for writing unit tests apply here. If the answer is "no" for all three questions, then consider the cost of writing the unit tests, and of keeping them in your code base. Compare this cost with the possible benefit. Make an intelligent decision about whether you should be writing unit tests, and be prepared to defend that decision to your manager.

But don't carry a preconceived notion that "unit tests are always good, for every class". And don't carry the opposite notion - that "unit tests are always unnecessary". Neither is true.



回答2:

I'm in the same camp as Dhanji Prasanna who worked on Guice and Google Wave. Its not about 100% coverage, its about writing valuable tests that provide the right feedback for the right components in a way that aids development and protects against code regression.

For one of my Struts2 apps, we had very very complex data validation requirements. Thousands. We used the struts2-junit-plugin to test the action classes within their integrated context with Spring 3 IoC and Struts2 validation and a custom mechanism for populating mock requests with lots of different data scenarios. These tests were/are invaluable both during development and as a maintenance tool.

But for some of our simpler actions, I don't see much value coming back compared to the time spent to write them. But then, they don't take too long to write if they are very simple, either.

I have also seen cases where the 100% coverage notion led to 100% of classes having thoughtless, worthless tests being written for them. For my money, I vote for identifying the areas where the tests will provide the most value up front and focusing on doing those very well.



回答3:

Must write Unit Test for functions could be problematic, but in anyway maybe in a future will be validations in the Actions that would be good can test that. The time spend for test the actions must be a little bit and I would recommend do it, every tier in your app must have some functionally if not is a unnecessary and must review the architecture.