Search code examples
javaeclipseunit-testingjunitmoreunit

Map different source directories to different test subdirectories


I have an Eclipse Project with JUnit and moreUnit installed. I have two source folders:

src/
demo/

I have a test directory that, via moreUnit and CTRL-J, allows me to quickly create a unit test class but only for packages within src/.

I would like to map the test directory to the above source directories so that when I create a new Unit test, it goes automatically to one of two packages within test: src or demo.

Example of file hierarchy:

src/
    my_package
        MyClass.java

demo/
    my_package
        MyOtherClass.java

And corresponding (desired) test layout:

test/
     src
         my_package
             MyClassTest.java
     demo
         my_package
             MyOtherClassTest.java

Within test, src and demo can be packages or subdirectories, it doesn't matter: as long as moreUnit is able to create the test class in right "branch".


Solution

  • Hopefully you've found the answer to your question by now, but if you haven't, you can try this.

    Create the project that will contain your tests. You will need to set it up so that this project can see your production (non-test) project. Do this as follows. Right-click on the test project and select "Properties" at the bottom of the list. In the left bar, click "Java Build Path."

    In the main portion of the window, select "Projects" at the top, and then click "Add." Select your production project.

    At this point, any tests you write in this project will be able to access the classes in your production project. This is a one-way dependency; you don't want your production code to require access to your tests!

    Now, let's tell MoreUnit where to look for associated tests. Right-click on your production project, which contains your src and demo packages. Click "Properties" at the bottom of the list. In the left bar, expand "MoreUnit" and click "Java."

    Check the box next to "Use project specific settings," and make sure "Test source folder" is selected. Click the "Add" button.

    From here, you can select the test project you want to associate with your production project. Click "Apply."

    Now, when you Ctrl + J, MoreUnit should create the test in the appropriate test project. It is smart enough to also create a package with the appropriate name within the test project, so your tests should be organized the way you want them. :)