user1834274
user1834274

Reputation: 11

I wonder if there is something wrong with my ant junit task

I'm writing my build.xml. However, it seems there is something go wrong with my junit task. when I run my junit task. I can build success, but the junit report only show that run 1 test with error. But I have more than 10 tests. So I wonder if there is something run with my junit task. Here is my code.

<property name="src.dir"    value="src"/>
<property name="bin.dir"    value="bin"/>
<property name="dest.dir"   value="dest"/>
<property name="test.dir"   value="test/>
<property name="lib.dir"    value="lib"/>
<path id="classpath">
    <pathelement location="${lib.dir}/junit-4.11.jar"/>
    <pathelement location="${lib.dir}/ant-junit4.jar"/>
</path>

<target name="test" depends="compile">
    <junit printsummary="yes" haltonfailure="no">
            <classpath>
                <pathelement location="${bin.dir}"/>
                <path refid="classpath"/>
            </classpath>
            <formatter type="plain" usefile="false"/>   
                <batchtest fork="yes">
                    <fileset dir="${test.dir}" includes="*Test*.java"/>
                </batchtest>
            </junit>
        </target>

I cannot figure out what is wrong so could somebody help me out?

Upvotes: 0

Views: 243

Answers (1)

David W.
David W.

Reputation: 107040

And what is happening? Do you get any error messages?

You usually need to do the following:

  • Compile your normal code. The resulting *.class files should be placed inside a directory such as target/classes or build/classes. Use the destdir parameter of the <javac> task to do this.
  • Compile your JUnit tests.
    • In your classpath, you need all of the jars you needed to compile your normal classes
    • You need a reference to the destdir where your normal classes were compiled to.
    • You need the JUnit jar.
    • These should be compiled to a different directory from your normal jars. Normally, this is target/test-classes or build/test-classes.

Once you've compiled the JUnit tests, you may run them. You can use the <junit> task like you did.

  • You need to make sure that the includeantruntime parameter is set to true
  • You should also set fork to true.
  • You need the same classpath (with all three elements you had) when you compiled the test classes. More jars might be needed, but usually not.
  • You run the tests on the compiled JUnit test classfiles (the ones you saved to target/test-classes or build/test-classes. In your example, you're trying to run them against the source.

I use the Maven standards for my directory layout. That means my Java source is under src/main/java while my JUnit Java files are under src/test/java. Any XML or properties or other none source files needed are stored in src/main/resources. The regular source is compiled to target/classes while the Junit sources are compiled to target/test-classes.

This makes it easy to compile your code and test code separately without worrying about **/test/**, **/Test/**, **/JUnit/** exceptions in directory compiling since everything is separate.

Hope this helps.

Upvotes: 1

Related Questions