1
votes

I have created an Ant target to compile my code.

<target name="test" depends="compile">
    <junit>
        <classpath>
            <pathelement path="${basedir}\..\SwaCore\lib\junit.jar"/>
        </classpath>
        <classpath>
            <pathelement path="${build}"/>
        </classpath>
        <test name="BinarySearchTest" />
    </junit>
</target>

<target name="compile">
    <mkdir dir="${build}"/>
    <javac
        srcdir="${basedir}\src;${basedir}\..\SwaCore\src;${basedir}\..\SwaShared\src;${basedir}\..\SwaDictionary\src;${basedir}\..\SwaSuggestion\src;${basedir}\..\SwaServerSharedCore\src;${basedir}\..\SwaPrediction\src;"
        destdir="${build}"
        debug="true"
        debuglevel="lines,vars,source">
        <classpath>
            <pathelement path="${basedir}\..\SwaCore\lib\junit.jar"/>
            <pathelement path="${basedir}\..\SwaSuggestion\lib\ssce.jar"/>
            <pathelement path="C:\Java\javamail-1.4.1\mail.jar"/>
            <pathelement path="C:\Java\commons-net-2.0\commons-net-ftp-2.0.jar"/>
            <pathelement path="${basedir}\WebContent\WEB-INF\lib\gson-2.2.1.jar"/>
            <pathelement path="${tomcatLibs}\servlet-api.jar"/>
        </classpath>
    </javac>
</target>

The compile works fine, all my classes are in the folder pointed to by ${build}. I assume this ${build} will be the classpath for my JUnit test case. I then have a test target that I want to run a JUnit test Called BinarySearchTest. When I run this I get the simple error

Test BinarySearchTest FAILED

I know my Unit test is ok because it tests nothing.

public class BinarySearchTest extends TestCase
{
    public void test()
    {       
    }
}

Even if I put the following in my test target I still get the same error

<test name="AtestThatDoesNotExistTest" />

result =

Test AtestThatDoesNotExistTest FAILED

I would have expected a classNotFound error so there is obviously something fundamentally wrong with my Ant XML

JUnit jar is being picked up ok because if it's not in the classpath I will get the error

The <classpath> for <junit> must include junit.jar if not in Ant's own classpath

Update I modified my target to include

        <formatter type="brief" usefile="false" />
        <test name="com.swaserver.junit.BinarySearchTest" />

test:
[junit] Testsuite: com.swaserver.junit.BinarySearchTest
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 1.878 sec
[junit] ------------- Standard Output ---------------
[junit] Initializing Binary search class
[junit] Binary search class initialized
[junit] ------------- ---------------- ---------------

BUILD SUCCESSFUL Total time: 2 seconds

Now I can see that my test is actually run. However I get a class not found unless I specify the fully qualified class name for my unit test. Why would this be?

1
run with verbose (-v) or get detailed output from junit, this will tell you more exactly which exception was thrown - oers
can you get some stacktrace in someway - Gnanz
see original question updated - MayoMan
Well, this question is solved: you always have to use the "full name" of the class when referring to it, no matter where. This is true for main classes (java com.mypackage.MyClass), for test classes, for persistence classes, for ... - Gilberto Torrezan
That's grand, just thought it was a bit long winded having to specify the full name - MayoMan

1 Answers

0
votes

You may try

    <classpath>
        <pathelement path="${basedir}\..\SwaCore\lib\junit.jar"/>
        <pathelement path="${build}"/>
    </classpath>

instead of

    <classpath>
        <pathelement path="${basedir}\..\SwaCore\lib\junit.jar"/>
    </classpath>
    <classpath>
        <pathelement path="${build}"/>
    </classpath>