0
votes

Another classic ant junit problem. Like the other questions I found on the topic, it is probably some subtle classpath error but none of the suggested solutions have worked for me. Here is my test code, which runs just fine in Eclipse --

package trial;

import java.net.MalformedURLException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import trial.CommonCode;  //this is a library of methods, since the test runs I'm not including it here

public class BaseTests {
    WebDriver driver;
    CommonCode myCommon;

    @Before 
    public void startup() throws MalformedURLException, InterruptedException{
        myCommon=new CommonCode();
        driver=myCommon.driver;
    }

    @After
    public void cleanup(){
        driver.quit();
    }   

    @Test
    public void deleteLists(){
        System.out.println("Hi Mom!");
        myCommon.loginLibrary("GAL");

    }
}

And here's my build.xml (class files are in ${workdir}\bin\trial and source files are in ${workdir}\src\trial)--

<project default="main" basedir=".">

    <property name="testbin" value="bin\trial"/>
    <property name="src.dir" value="src\trial"/>

    <path id="path.class">
      <pathelement location="c:\Java\selenium-2.8.0" /> 
      <pathelement location="C:\eclipse\plugins\org.junit_4.8.2.v4_8_2_v20110321-1705"/>
        <pathelement location="C:\eclipse\plugins\org.apache.ant_1.8.2.v20110505-1300"/>
      <fileset dir="c:\Java\selenium-2.8.0" includes="*.jar" /> 
      <fileset dir="C:\eclipse\plugins\org.junit_4.8.2.v4_8_2_v20110321-1705" includes="*.jar"/>
        <fileset dir="C:\eclipse\plugins\org.apache.ant_1.8.2.v20110505-1300\lib" includes="*.jar"/>
      <path refid="src.dir" /> 
      </path>

    <path id="src.dir">
      <pathelement location="${src.dir}" /> 
      </path>

    <path id="test.dir">
        <pathelement location="${testbin}"/>
    </path>    

    <target name="main" depends="compile" description="main target">
        <echo> Building now </echo>
    </target>

    <target name="compile" description="compilation target" >
        <javac srcdir="${src.dir}" destdir="${testbin}" classpathref="path.class" debug="on" verbose="true"/>
    </target>

    <target name="runtest" >
            <junit fork="no" printsummary="true" showoutput="true">
                <classpath>
                    <path refid="path.class"/>
                    <path refid="test.dir"/>
                    <path refid="src.dir"/>
                </classpath>
                <formatter type="brief" usefile="false" />
                <batchtest fork="yes">
                    <fileset dir="${testbin}">                  
                        <include name="*Tests.class"/>
                    </fileset>
                </batchtest>

            </junit>

        </target>

</project>

I get the same error whether I run the ant task runtest from the command line or from eclipse ant pane:

runtest:
    [junit] Running BaseTests
    [junit] Testsuite: BaseTests
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Null Test:  Caused an ERROR
    [junit] BaseTests (wrong name: trial/BaseTests)
    [junit] java.lang.NoClassDefFoundError: BaseTests (wrong name: trial/BaseTests)
    [junit]     at java.lang.ClassLoader.defineClass1(Native Method)
    [junit]     at java.lang.ClassLoader.defineClassCond(Unknown Source)
    [junit]     at java.lang.ClassLoader.defineClass(Unknown Source)
    [junit]     at java.security.SecureClassLoader.defineClass(Unknown Source)
    [junit]     at java.net.URLClassLoader.defineClass(Unknown Source)
    [junit]     at java.net.URLClassLoader.access$000(Unknown Source)
    [junit]     at java.net.URLClassLoader$1.run(Unknown Source)
    [junit]     at java.security.AccessController.doPrivileged(Native Method)
    [junit]     at java.net.URLClassLoader.findClass(Unknown Source)
    [junit]     at java.lang.ClassLoader.loadClass(Unknown Source)
    [junit]     at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    [junit]     at java.lang.ClassLoader.loadClass(Unknown Source)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Unknown Source)
    [junit] Test BaseTests FAILED

So it is finding the correct junit test (trial.BaseTests) but then claiming it has the wrong name. Any suggestions gratefully received, especially debugging hints so I can avoid this problem in the future.

2

2 Answers

1
votes

First of all, your src dir should be src and your testbin should be bin. These are the roots of your package tree.

Next, the classpath is supposed to contain jar files and directories containing the compiled classes, and not the source files. So the classpath of junit should contain the testdir, and not the src dir.

Finally, you should use forward slashes and not backslashes, even on Windows, and you should also use the location attribute rather than the value attribute when defining properties holding file or directory paths.

0
votes

It looks like your file paths are incorrect. They should match the package names.

Your test class is trial.BaseTests. You are telling JUnit to execute all tests under bin/trial. So, it searchs for all files under bin/trial, and it finds BaseTests.class, which should be under trial/BaseTests.class.

Change your testbin from 'bin/trial' to 'bin':

<property name="testbin" value="bin"/>