2

i would like to execute junit5 tests in combination with ant, specifically junitlauncher

here is my project structure:

enter image description here

and here is my build file:

<?xml version="1.0" encoding="UTF-8"?>
<project default="test" name="test">
    <property name="src.dir" value="${basedir}/src" />
    <property name="output.dir" value="${basedir}/build" />

    <!-- PATH DEFINITIONS -->
    <path id="junit5.classpath">
        <fileset dir="${basedir}/lib/" />
    </path>
    <path id="test.classpath">
        <path refid="junit5.classpath" />
        <fileset dir="${output.dir}">
            <include name="**/*.class" />
        </fileset>
    </path>

    <target name="clean">
        <delete dir="${output.dir}" />
    </target>

    <target name="compile" depends="clean">
        <mkdir dir="${output.dir}" />
        <mkdir dir="${output.dir}/report" />
        <javac srcdir="${src.dir}" destdir="${output.dir}" includeantruntime="false">
            <!-- test libs -->
            <classpath>
                <path refid="junit5.classpath" />
            </classpath>
        </javac>
    </target>

    <target name="test" depends="compile">
        <pathconvert property="classpathProp" refid="test.classpath"/>
        <echo>Classpath is ${classpathProp}</echo>
        <echo>Running junit tests...</echo>
        <junitlauncher>
            <classpath refid="test.classpath" />
            <test name="test.SomeClassTest" />
        </junitlauncher>
    </target>
</project>

when i try to run the test-task, i can see the classpath:

Classpath is 
/Users/some-username/Documents/workspace/test/lib/junit-jupiter-api-5.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-jupiter-engine-5.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-jupiter-migrationsupport-5.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-jupiter-params-5.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-platform-commons-1.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-platform-engine-1.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-platform-launcher-1.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-platform-runner-1.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-platform-suite-api-1.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit-vintage-engine-5.3.0-SNAPSHOT.jar:
/Users/some-username/Documents/workspace/test/lib/junit.jar:
/Users/some-username/Documents/workspace/test/lib/org.apiguardian_1.0.0.v20180327-1502.jar:
/Users/some-username/Documents/workspace/test/lib/org.hamcrest.core_1.3.0.v20180420-1519.jar:
/Users/some-username/Documents/workspace/test/lib/org.opentest4j_1.0.0.v20180327-1502.jar:
/Users/some-username/Documents/workspace/test/build/test/SomeClass.class:
/Users/some-username/Documents/workspace/test/build/test/SomeClassTest.class:
/Users/some-username/Documents/workspace/test/build/test/TestSuite.class

followed by:

BUILD FAILED /Users/some-username/Documents/workspace/test/build.xml:36: java.lang.NoClassDefFoundError: org/junit/platform/launcher/core/LauncherFactory

you can find the eclipse project archive here: https://drive.google.com/file/d/161rYTopmqM3JMqv8kvM6VjJbG1sm-3k2/view?usp=sharing

how can i make it work? what am i missing?

rome
  • 476
  • 1
  • 9
  • 19
  • 3
    Did you follow the instructions noted here https://ant.apache.org/manual/Tasks/junitlauncher.html ? Especially the lines starting with: "Put all these relevant jars along with the ant-junitlauncher.jar in ANT_HOME/lib directory OR Leave ant-junitlauncher.jar in the ANT_HOME/lib directory and include all other relevant jars in the classpath by passing them as a -lib option, while invoking Ant"... Have a look at https://github.com/junit-team/junit5-samples/tree/master/junit5-jupiter-starter-ant as well – Sormuras Aug 06 '18 at 18:13
  • thx for the input. indeed, thx for the hint. didn't see that, though i read the doc. i will check it and post feedback – rome Sep 02 '18 at 17:12

2 Answers2

0

I got the same problem, worked by hand over the libs direct in the ant call, like "ant -lib lib/".

Jharry
  • 1
  • ```ant -lib /myHome/junitLibs/``` option pointing to my directory containing all jars did work for me. The nested classpath tag in the junitlauncher tag containing the same directory or to the fileset with all jars of the directory does not work at all. – lolung Feb 19 '19 at 13:56
0

Junit5 tests require different ant tags. Check with https://ant.apache.org/manual/Tasks/junitlauncher.html

this is my ant code that works :

<target name="runTests" depends="compile,jar">      
    <junitlauncher printsummary="yes" haltonfailure="no">           
        <classpath refid="classpath.base" />  
        <classpath>
            <!-- the test classes themselves -->
            <pathelement location="${build}"/>
        </classpath>
        
        <!-- test by test - class -->           
        <!--<test name="TestPackage.ConcatTest" haltonfailure="no" /> -->   
        
        <!-- run all found tests in dir with classes -->           
        <testclasses outputdir="../report">
                        <fileset dir="${build}"/>
                        <listener type="legacy-brief" sendSysOut="true"/>
                        <listener type="legacy-xml" sendSysErr="true" 
sendSysOut="true"/>

         </testclasses>
    </junitlauncher>
</target>