0

I'm banging my head against a wall - I have a basically empty test file (its more to test my build file than anything else

A few months back my build file got corrupted - and for some reason starting a new project didnt fix it, so I had to make one from scratch, initially it just needed to support build, but now it requires test - and it is not working :(

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);
    }
  }
}

My build file:

<?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>
Zack Newsham
  • 2,810
  • 1
  • 23
  • 43
  • Possible duplicate of http://stackoverflow.com/questions/672466/junit-how-to-avoid-no-runnable-methods-in-test-utils-classes. Basically try restricting the "runnable" test classes to "**/*Test.java" – Morfic Apr 09 '14 at 06:37
  • Not a duplicate, that question is because an untestable class is trying to be tested. This question is where a correctly annotated class is being tested, with the same message. – Zack Newsham Apr 09 '14 at 16:02
  • Actually you seem to be including more than that file with `${javac.includes}` hence my suggestion – Morfic Apr 09 '14 at 16:05
  • 1
    Your test classpath doesn't seem to have your test classes on it, so although Ant knows which test names it should look for to run, JUnit can't find the associated class to run. I you could also tell us which target you're running when you're seeing the failure then that may help. – mc1arke Apr 09 '14 at 17:16
  • I tried running test and test-single. The tests now "work" by including both project.classpath and project.test.classpath, I see the output however the tests dont show in netbeans test window – Zack Newsham Apr 11 '14 at 05:31
  • Netbeans won't show test being executed by Ant in its test window - they only appear there when you execute tests using the Netbeans run options, not the Ant runners. – mc1arke Apr 11 '14 at 09:44

1 Answers1

0

Your test classpath doesn't seem to have your test classes on it, so although Ant knows which test names it should look for to run, JUnit can't find the associated class to run.

From your follow-up comment - when Netbeans executes tests directly, it invokes it's own JUnit runner with associated listeners - or results parsers - that hook into the JUnit lifecycle so know what tests are doing. When you tell Netbeans to run an Ant task, it doesn't know about what Ant is doing (it doesn't have sight of the tasks within Ant), so doesn't know whether you're trying to run the JUnit task, or one of the many other tasks within Ant that it wouldn't make sense to try and reports on, other than in the console.

mc1arke
  • 1,030
  • 10
  • 22