I think we need to give some guidelines for anyone who would like to
participate in writing unit tests for Struts. I'd like to start this process
by giving you my views on the subject and discuss it. When we have a final
agreement, we can put a web page on the struts web site.

Here are some of the rules/guidelines that I can see so far :

1) Put the test sources in jakarta-struts/src/test. For example if you want
to write a unit test for class org.apache.struts.action.ServletAction, you
woud put the test class in jakarta-struts/src/test/org/apache/struts/action

2) Name of the test class. For any XXX class to test, the associated test
class should be named TestXXX. I'm sure others will prefer XXXTest. I don't
really care, we just need to standardize on one.

3) Any helper class and resources for the test should be put in the same
directory as the test class. For example, if my test class accepts several
xml files, I would put them where I put my TestXXX class and I would use
Class.getResourceAsStream() to load them (thus removing the need to hard
code the path of the resource). Again, this is a proposition. Another
solution would be to put them in the jakara-struts/conf/test directory.

4) All test suites should be gathered in a single TestAll.java (or
AllTest.java) test class that will located in
jakarta-struts/src/test/org/apache/struts. The build.xml target for unit
tests will use this file to run all the tests.

And then general good practice rules (See this article for more details
http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit.html) :

5) Test cases should be completely independent from one another. Don't
assume the order in which your test cases will run.
6) Avoid hard coding paths in your test cases
7) do not use the test case constructor to set up a test case
8) Avoid writing test cases with side-effects
9) Keep tests small and fast
10) Name test cases properly
11) Ensure that tests are time independent
12) Consider locale when writing tests
13) Always test for error conditions
14) Utilize properly assert/fails and exception mechanism (i.e. don't put a
ty/catch around a test case for which you don't expect an exception)
15) Document tests in javadoc. This is very important as unit tests are a
very good way to document the code.

Comments ? (I'm sure I'll get plenty ... :)

Vincent.

Reply via email to