title: Ant JUnit job fails to run Spring's tests - Spring class runner not found
body: <p>I have an Eclipse Spring project with Ivy dependency management library (with jars in local cache). I wrote some JUnit test classes with @RunWith(SpringJUnit4ClassRunner.class) annotation, and I can run them in Eclipse without any problem. Unfortunately I can't run them as Ant jobs. I got following exception:</p>
  
  <pre><code>Type org.springframework.test.context.junit4.SpringJUnit4ClassRunner not present
  java.lang.TypeNotPresentException: Type org.springframework.test.context.junit4.SpringJUnit4ClassRunner not present
      at com.sun.proxy.$Proxy4.value(Unknown Source)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
      at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:32)
      at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:424)
      at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:138)
  Caused by: java.lang.ClassNotFoundException: org.springframework.test.context.junit4.SpringJUnit4ClassRunner
      at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:270)
      at java.lang.Class.initAnnotationsIfNecessary(Class.java:3178)
      at java.lang.Class.getAnnotation(Class.java:3137)
  </code></pre>
  
  <p>It looks like Ant had not put Ivy's cache into classpath. I tried even putting directory of Ivy's cache into classpath, but with no results. What I should do? </p>
  
  <p>My Ivy dependences:</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency org="org.springframework" name="spring-core" rev="3.2.8.RELEASE"/&gt;
      &lt;dependency org="org.springframework" name="spring-jdbc" rev="3.2.8.RELEASE"/&gt;  
      &lt;dependency org="org.springframework" name="spring-tx" rev="3.2.8.RELEASE"/&gt;
      &lt;dependency org="org.springframework" name="spring-test" rev="3.2.8.RELEASE"/&gt;
      &lt;dependency org="mysql" name="mysql-connector-java" rev="5.1.29"/&gt;
      &lt;dependency org="junit" name="junit" rev="4.11"/&gt;
  &lt;/dependencies&gt;
  </code></pre>
  
  <p>And my Ant build file:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;project name="FooBar" basedir="."
      xmlns:ivy="antlib:org.apache.ivy.ant"&gt;
  
      &lt;property name="dir.jar" value="build/jar"/&gt;
      &lt;property name="dir.classes" value="build/classes"/&gt;
      &lt;property name="dir.classes.tests" value="build/tests/classes"/&gt;
  
      &lt;ivy:cachepath pathid="ivy.classpath" /&gt;
  
      &lt;path id="classpath"&gt;
          &lt;pathelement location="${dir.classes}" /&gt;
      &lt;/path&gt;
  
      &lt;!-- ================================= 
            target: clean              
           ================================= --&gt;
      &lt;target name="clean"&gt;
          &lt;delete dir="${dir.jar}"/&gt;
          &lt;delete dir="${dir.classes}"/&gt;
          &lt;delete dir="${dir.classes.tests}"/&gt;
      &lt;/target&gt;
  
      &lt;!-- ================================= 
            target: resolve              
           ================================= --&gt;
      &lt;target name="resolve"&gt;
          &lt;ivy:cleancache/&gt;
          &lt;ivy:resolve/&gt;
          &lt;ivy:cachepath pathid="ivy.classpath" /&gt;
      &lt;/target&gt;
  
      &lt;!-- - - - - - - - - - - - - - - - - - 
            target: build                      
           - - - - - - - - - - - - - - - - - --&gt;
      &lt;target name="build"&gt;
          &lt;mkdir dir="${dir.classes}"/&gt;
          &lt;javac srcdir="src" destdir="${dir.classes}" 
              includeantruntime="false" 
              classpathref="ivy.classpath" /&gt;
      &lt;/target&gt;
  
      &lt;!-- - - - - - - - - - - - - - - - - - 
            target: build-tests                      
           - - - - - - - - - - - - - - - - - --&gt;
      &lt;target name="build-tests"&gt;
          &lt;mkdir dir="${dir.classes.tests}"/&gt;
          &lt;javac srcdir="test" destdir="${dir.classes.tests}" 
              includeantruntime="false"&gt;
              &lt;classpath refid="ivy.classpath"/&gt;
              &lt;classpath refid="classpath"/&gt;
          &lt;/javac&gt;
      &lt;/target&gt;
  
      &lt;!-- ================================= 
            target: build-all              
           ================================= --&gt;
      &lt;target name="build-all" depends="clean, build, build-tests" description="Builds all" /&gt;
  
     &lt;!-- ================================= 
            target: clean-test-all              
           ================================= --&gt;
      &lt;target name="clean-test-all" depends="clean, build-all" description="Runs all JUnit tests"&gt;
          &lt;junit printsummary="yes" haltonfailure="yes"&gt;
              &lt;classpath&gt;
                &lt;dirset dir="${user.home}/.ivy2/cache"&gt;
                  &lt;include name="**" /&gt;
                &lt;/dirset&gt;
                &lt;pathelement location="ivy.classpath"/&gt;
                &lt;pathelement location="${dir.classes}"/&gt;
                &lt;pathelement location="${dir.classes.tests}"/&gt;
              &lt;/classpath&gt;
  
              &lt;batchtest fork="yes"&gt;
                &lt;fileset dir="junit"&gt;
                  &lt;include name="**/*Test.java"/&gt;
                &lt;/fileset&gt;
              &lt;/batchtest&gt;
          &lt;/junit&gt;
      &lt;/target&gt;   
  &lt;/project&gt;
  </code></pre>