Ant JUnit作业无法运行Spring的测试 - 找不到Spring类运行器

时间:2014-03-18 17:49:18

标签: java spring ant junit ivy

我有一个带有Ivy依赖管理库的Eclipse Spring项目(本地缓存中有jars)。我用@RunWith(SpringJUnit4ClassRunner.class)注释编写了一些JUnit测试类,我可以在Eclipse中运行它们没有任何问题。不幸的是,我无法将它们作为Ant工作运行。我有以下例外:

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)

看起来Ant并没有将Ivy的缓存放入类路径中。我甚至尝试将Ivy的缓存目录放入类路径中,但没有结果。我该做什么?

我对常春藤的依赖:

<dependencies>
    <dependency org="org.springframework" name="spring-core" rev="3.2.8.RELEASE"/>
    <dependency org="org.springframework" name="spring-jdbc" rev="3.2.8.RELEASE"/>  
    <dependency org="org.springframework" name="spring-tx" rev="3.2.8.RELEASE"/>
    <dependency org="org.springframework" name="spring-test" rev="3.2.8.RELEASE"/>
    <dependency org="mysql" name="mysql-connector-java" rev="5.1.29"/>
    <dependency org="junit" name="junit" rev="4.11"/>
</dependencies>

我的Ant构建文件:

<?xml version="1.0"?>
<project name="FooBar" basedir="."
    xmlns:ivy="antlib:org.apache.ivy.ant">

    <property name="dir.jar" value="build/jar"/>
    <property name="dir.classes" value="build/classes"/>
    <property name="dir.classes.tests" value="build/tests/classes"/>

    <ivy:cachepath pathid="ivy.classpath" />

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

    <!-- ================================= 
          target: clean              
         ================================= -->
    <target name="clean">
        <delete dir="${dir.jar}"/>
        <delete dir="${dir.classes}"/>
        <delete dir="${dir.classes.tests}"/>
    </target>

    <!-- ================================= 
          target: resolve              
         ================================= -->
    <target name="resolve">
        <ivy:cleancache/>
        <ivy:resolve/>
        <ivy:cachepath pathid="ivy.classpath" />
    </target>

    <!-- - - - - - - - - - - - - - - - - - 
          target: build                      
         - - - - - - - - - - - - - - - - - -->
    <target name="build">
        <mkdir dir="${dir.classes}"/>
        <javac srcdir="src" destdir="${dir.classes}" 
            includeantruntime="false" 
            classpathref="ivy.classpath" />
    </target>

    <!-- - - - - - - - - - - - - - - - - - 
          target: build-tests                      
         - - - - - - - - - - - - - - - - - -->
    <target name="build-tests">
        <mkdir dir="${dir.classes.tests}"/>
        <javac srcdir="test" destdir="${dir.classes.tests}" 
            includeantruntime="false">
            <classpath refid="ivy.classpath"/>
            <classpath refid="classpath"/>
        </javac>
    </target>

    <!-- ================================= 
          target: build-all              
         ================================= -->
    <target name="build-all" depends="clean, build, build-tests" description="Builds all" />

   <!-- ================================= 
          target: clean-test-all              
         ================================= -->
    <target name="clean-test-all" depends="clean, build-all" description="Runs all JUnit tests">
        <junit printsummary="yes" haltonfailure="yes">
            <classpath>
              <dirset dir="${user.home}/.ivy2/cache">
                <include name="**" />
              </dirset>
              <pathelement location="ivy.classpath"/>
              <pathelement location="${dir.classes}"/>
              <pathelement location="${dir.classes.tests}"/>
            </classpath>

            <batchtest fork="yes">
              <fileset dir="junit">
                <include name="**/*Test.java"/>
              </fileset>
            </batchtest>
        </junit>
    </target>   
</project>

1 个答案:

答案 0 :(得分:0)

请改为尝试:

<junit printsummary="yes" haltonfailure="yes">
    <classpath>
      <dirset dir="${user.home}/.ivy2/cache">
        <include name="**" />
      </dirset>
      <path refid="ivy.classpath"/>  <!-- ivy.classpath is already a path -->
      <pathelement location="${dir.classes}"/>
      <pathelement location="${dir.classes.tests}"/>
    </classpath>

    <batchtest fork="yes">
      <fileset dir="junit">
        <include name="**/*Test.java"/>
      </fileset>
    </batchtest>
</junit>

&#34; ivy.path&#34;已经是一个路径,所以通过引用传递它。