【问题标题】:JUnit with Ant returns "No runnable methods"带有 Ant 的 JUnit 返回“没有可运行的方法”
【发布时间】:2014-04-09 00:46:05
【问题描述】:

我的头撞墙了 - 我有一个基本上是空的测试文件(测试我的构建文件比什么都重要

几个月前我的构建文件损坏了 - 由于某种原因开始一个新项目并没有修复它,所以我不得不从头开始制作一个,最初它只需要支持构建,但现在它需要测试 - 而且它不工作:(

package com.rcdc.questiondatabase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
public class RCDCServiceTest {

  public RCDCServiceTest() {
  }

  @BeforeClass
  public static void setUpClass() {
  }

  @AfterClass
  public static void tearDownClass() {
  }

  @Before
  public void setUp() {
  }

  @After
  public void tearDown() {
  }

// TODO add test methods here.
  // The methods must be annotated with annotation @Test. For example:
  //
  @Test
  public void testHelloTest() {
    if(true){
      assertTrue(true);
    }
  }
}

我的构建文件:

<?xml version="1.0" encoding="UTF-8"?>

<project name="INCAP" default="default" basedir=".">
    <description>Builds, tests, and runs the project INCAP.</description>

  <property name="build.dir" value="war/WEB-INF/classes"/>
  <property name="dist.dir" value="dist"/>
  <property name="test.dir" value="test"/>
  <property name="test.build.dir" value="testbuild"/>
  <path id="project.classpath">
    <pathelement path="war/WEB-INF/classes" />
    <fileset dir="war/WEB-INF/lib">
      <include name="**/*.jar" />
    </fileset>
    <pathelement path="${java.home}/../lib/tools.jar:${libs.jsp-compiler.classpath}:${libs.jsp-compilation.classpath}"/>
  </path>
  <path id="project.test.classpath">
    <pathelement path="war/WEB-INF/classes" />
    <fileset dir="war/WEB-INF/lib">
      <include name="**/*.jar" />
    </fileset>
    <pathelement path="${java.home}/../lib/tools.jar:${libs.jsp-compiler.classpath}:${libs.jsp-compilation.classpath}"/>
  </path>

   <target name="copyjars" description="Copies the App Engine JARs to the WAR.">
  </target>
  <target name="clean">
    <delete dir="war/WEB-INF/classes"/>
  </target>
  <target name="compile" depends="copyjars" description="Compiles Java source and copies other source files to the WAR.">
    <mkdir dir="${build.dir}" />
    <copy todir="${build.dir}">
      <fileset dir="src">
        <exclude name="**/*.java" />
      </fileset>
    </copy>
    <property name="myclasspath" refid="project.classpath"/>

    <echo message="Classpath ${myclasspath}" />
    <javac
        srcdir="src"
        destdir="${build.dir}"
        classpathref="project.classpath"
        debug="on" />
  </target>

  <target name="compiletest" depends="compile" description="compile the tests " >
    <mkdir dir="${test.build.dir}" />
    <javac
        srcdir="test"
        destdir="${test.build.dir}"
        classpathref="project.test.classpath"
        debug="on" />
  </target>

  <target name="dist" depends="compile">
    <mkdir dir="war/WEB-INF/classes" />
    <copy todir="war/WEB-INF/classes">
      <fileset dir="src">
        <exclude name="**/*.java" />
      </fileset>
    </copy>
    <property name="myclasspath" refid="project.classpath"/>
  </target>

  <target name="test" depends="compiletest" description="run the tests " >
    <junit printsummary="yes" fork="yes" haltonfailure="yes">
      <formatter type="plain"/>
      <batchtest fork="true">
        <fileset dir="${test.dir}">
          <include name="**/*Test*.java"/>
        </fileset>
      </batchtest>
      <classpath refid="project.test.classpath" />
    </junit>
  </target>

  <target name="test-single" depends="compiletest" description="Run a single test">
    <fail unless="browser.context" message="Property not set: 'test-class'"/>
    <echo message="                             -- Running single test --"/>
    <echo message="                                 -- ${browser.context} --" />
    <junit printsummary="yes" fork="yes" showoutput="yes" haltonfailure="yes">
      <formatter type="plain"/>
      <batchtest fork="true">
        <fileset dir="test">
          <include name="${javac.includes}"/>
        </fileset>
      </batchtest>
      <classpath refid="project.classpath" />
    </junit>
</target>
</project>

【问题讨论】:

  • stackoverflow.com/questions/672466/… 的可能重复项。基本上尝试将“可运行”测试类限制为“**/*Test.java”
  • 不是重复的,这个问题是因为一个不可测试的类正试图被测试。这个问题是一个正确注释的类被测试的地方,带有相同的信息。
  • 实际上,您似乎包含的不仅仅是${javac.includes} 的文件,因此我的建议
  • 您的测试类路径上似乎没有您的测试类,因此尽管 Ant 知道它应该寻找哪些测试名称来运行,但 JUnit 找不到要运行的关联类。当您看到失败时,您还可以告诉我们您正在运行哪个目标,这可能会有所帮助。
  • 我尝试运行 test 和 test-single。现在测试通过包含 project.classpath 和 project.test.classpath 来“工作”,我看到了输出但是测试没有显示在 netbeans 测试窗口中

标签: java ant junit


【解决方案1】:

您的测试类路径上似乎没有您的测试类,因此尽管 Ant 知道它应该寻找哪些测试名称来运行,但 JUnit 找不到要运行的关联类。

根据您的后续评论 - 当 Netbeans 直接执行测试时,它会调用它自己的 JUnit 运行器以及关联的侦听器 - 或结果解析器 - 挂钩到 JUnit 生命周期,因此知道测试在做什么。当您告诉 Netbeans 运行 Ant 任务时,它不知道 Ant 正在做什么(它看不到 Ant 中的任务),因此不知道您是否正在尝试运行 JUnit 任务,或 Ant 中的许多其他任务之一,除了在控制台中之外,尝试和报告是没有意义的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多