Quantcast
Viewing all articles
Browse latest Browse all 3

Organizing test code in Java

A short while back I started to work with Ruby on Rails. Incredible fun and inspiring. One of the (many) things I like is the distinction Rails creates of the different levels of test scopes. It has separate folders to store unit, functional, integration and performance tests.

Now in Java – and using the Maven default folder layout – you get one folder for storing your production code (src/main/java) and one for your test code (src/test/java). That got me to thinking to apply Rails’ way to structure your Java test suite. After a while it can be tricky to organize to test code, so I wanted to give it a shot with Java.

Please please, gimme the good stuff!

Yeah yeah, getting there. So, what you need to be doing is the following:

  1. Under the default src/test/java folder create a folder per test level you want. I have separated folders for unit, functional and integration tests
  2. Alter your Maven pom.xml to contain the following:
  3. [xml]


    src/test/java/unit

    org.codehaus.mojo
    build-helper-maven-plugin
    1.5


    add-test-source generate-sources
    add-test-source

    src/test/java/functionalsrc/test/java/integration





    [/xml]

    Now this tells Maven to have src/test/java/unit as the default spot to find test cases overriding the default location. The build-helper-maven-plugin adds the other test folders.

  4. Now you can add test classes into the appropriate location. If you run mvn test they will be contained in the test run. Also you can get your IDE (say Eclipse) to understand this separation. Just running mvn eclipse:eclipse will get you the following:

Image may be NSFW.
Clik here to view.

Conclusion

Squishing everything in a single test folder can get really messy. From the outside it’s hard to see what the test scope of a class is. Sometimes you see people storing this scope in the class name, or in the package definition. I don’t like that all too much. With the scheme using folders for organizing your tests it’s immediately clear what the scope of a tests is. I got some positive responses from different people. Hope this helps you too, let me know what you think!


Viewing all articles
Browse latest Browse all 3

Trending Articles