0

So I am trying to build a project from this tutorial. I run ant, and get the error. The first thing I see is ...error: package junit.framework does not exist [javac] import junit.framework.TestCase; The tutorial was very unspecific about the possible error. It just says I might not have JUnit installed. I'm running the most recent version of Java, which I assume comes with JUnit. So the problem becomes finding it(or at least confirming it exists) and adding that to the build file. How do I do that?

Build.xml

<project name="cobertura.examples.basic" default="coverage" basedir=".">

    <description>
    Cobertura - http://cobertura.sourceforge.net/
    Copyright (C) 2003 jcoverage ltd.
    Copyright (C) 2005 Mark Doliner &lt;thekingant@users.sourceforge.net&gt;
    Copyright (C) 2006 Dan Godfrey
    Cobertura is licensed under the GNU General Public License
    Cobertura comes with ABSOLUTELY NO WARRANTY
    </description>

    <property file="build.properties" />

    <path id="cobertura.classpath">
        <fileset dir="${cobertura.dir}">
            <include name="cobertura.jar" />
            <include name="lib/**/*.jar" />
        </fileset>
    </path>
    <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>

    <target name="init">
        <mkdir dir="${classes.dir}" />
        <mkdir dir="${instrumented.dir}" />
        <mkdir dir="${reports.xml.dir}" />
        <mkdir dir="${reports.html.dir}" />
        <mkdir dir="${coverage.xml.dir}" />
        <mkdir dir="${coverage.summaryxml.dir}" />
        <mkdir dir="${coverage.html.dir}" />
    </target>

    <target name="compile" depends="init">
        <javac srcdir="${src.dir}" destdir="${classes.dir}" debug="yes">
            <classpath refid="cobertura.classpath" />
        </javac>
    </target>

    <target name="instrument" depends="init,compile">
        <!--
            Remove the coverage data file and any old instrumentation.
        -->
        <delete file="cobertura.ser"/>
        <delete dir="${instrumented.dir}" />

        <!--
            Instrument the application classes, writing the
            instrumented classes into ${build.instrumented.dir}.
        -->
        <cobertura-instrument todir="${instrumented.dir}">
            <!--
                The following line causes instrument to ignore any
                source line containing a reference to log4j, for the
                purposes of coverage reporting.
            -->
            <ignore regex="org.apache.log4j.*" />

            <fileset dir="${classes.dir}">
                <!--
                    Instrument all the application classes, but
                    don't instrument the test classes.
                -->
                <include name="**/*.class" />
                <exclude name="**/*Test.class" />
            </fileset>
        </cobertura-instrument>
    </target>

    <target name="test" depends="init,compile">
        <junit fork="yes" dir="${basedir}" failureProperty="test.failed">
            <!--
                Note the classpath order: instrumented classes are before the
                original (uninstrumented) classes.  This is important.
            -->
            <classpath location="${instrumented.dir}" />
            <classpath location="${classes.dir}" />

            <!--
                The instrumented classes reference classes used by the
                Cobertura runtime, so Cobertura and its dependencies
                must be on your classpath.
            -->
            <classpath refid="cobertura.classpath" />

            <formatter type="xml" />
            <test name="${testcase}" todir="${reports.xml.dir}" if="testcase" />
            <batchtest todir="${reports.xml.dir}" unless="testcase">
                <fileset dir="${src.dir}">
                    <include name="**/*Test.java" />
                </fileset>
            </batchtest>
        </junit>

        <junitreport todir="${reports.xml.dir}">
            <fileset dir="${reports.xml.dir}">
                <include name="TEST-*.xml" />
            </fileset>
            <report format="frames" todir="${reports.html.dir}" />
        </junitreport>
    </target>

    <target name="coverage-check">
        <cobertura-check branchrate="34" totallinerate="100" />
    </target>

    <target name="coverage-report">
        <!--
            Generate an XML file containing the coverage data using
            the "srcdir" attribute.
        -->
        <cobertura-report srcdir="${src.dir}" destdir="${coverage.xml.dir}" format="xml" />
    </target>

    <target name="summary-coverage-report">
        <!--
            Generate an summary XML file containing the coverage data using
            the "srcdir" attribute.
        -->
        <cobertura-report srcdir="${src.dir}" destdir="${coverage.summaryxml.dir}" format="summaryXml" />
    </target>

    <target name="alternate-coverage-report">
        <!--
            Generate a series of HTML files containing the coverage
            data in a user-readable form using nested source filesets.
        -->
        <cobertura-report destdir="${coverage.html.dir}">
            <fileset dir="${src.dir}">
                <include name="**/*.java"/>
            </fileset>
        </cobertura-report>
    </target>

    <target name="clean" description="Remove all files created by the build/test process.">
        <delete dir="${classes.dir}" />
        <delete dir="${instrumented.dir}" />
        <delete dir="${reports.dir}" />
        <delete file="cobertura.log" />
        <delete file="cobertura.ser" />
    </target>

    <target name="coverage" depends="compile,instrument,test,coverage-report,summary-coverage-report,alternate-coverage-report" description="Compile, instrument ourself, run the tests and generate JUnit and coverage reports."/>

</project>
EasilyBaffled
  • 3,822
  • 10
  • 50
  • 87

1 Answers1

2

It is true that you don't get JUnit when you grab the most recent JDK for the Mac.

While downloading and installing the latest junit.jar from http://junit.org, and configuring ant to find it will help you, you would probably be better off by learning junit from the JUnit page online rather from a very short cobertura tutorial online. These docs have a download and install guide, which is pretty good (assuming you know what a classpath is), as well as a super-small walkthrough to get you started.

You'll not that the class junit.framework.TestCase is part of JUnit version 3, which is very old by now. Unless you have legacy code to work with, you might want to start by learning JUnit 4, which has been around for many years now.

There are other ways to use JUnit, too. Your IDE might have a JUnit plugin already. An alternative is to use Maven. By specifying junit in your maven project file, maven will go fetch and install junit for you automatically. Maven is pretty complex, but once you get used to it, you'll probably use it for all of your Java applications.

ADDENDUM

Here is a complete example using Maven and JUnit that works on a Mac. All you need to install yourself is the JDK and Maven. Once that is done create a directory for a new project and create only three files:

  • PROJECT_ROOT/pom.xml
  • PROJECT_ROOT/src/main/java/com/example/Pair.java
  • PROJECT_ROOT/src/test/java/com/example/PairTest.java

Here is pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>pairs</artifactId>
  <version>1.0</version>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Here is Pair.java

package com.example;
class Pair {
    int x, y;
    public Pair(int x, int y) {this.x = x; this.y = y;}
    public int getX() {return x;}
    public int getY() {return y;}
    @Override public String toString() {return String.format("(%d,%d)", x, y);}
}

and here is PairTest.java

package com.example;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;

public class PairTest {

    @Test
    public void gettersWork() {
        Pair p = new Pair(4, 6);
        assertThat(p.getX(), is(4));
        assertThat(p.getY(), is(6));
        assertThat(p.toString(), is("(4,6)"));
    }
}

Now from your project root directory, simply type

mvn test

and everything should work. Note that the first time you run maven it might take about 4 minutes to download zillions of megabytes of stuff. Don't worry, that's normal. It will fetch junit for you.

The last few lines you should see are something like:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.example.PairTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.308 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

Hope that helps.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • I was more concerned with trying to learning how Cobertura, Jekins, and JUnit testing work together and less about JUnit itself. But your answer has given me much to think about so if you don't mind I have a few new quesitons for you. 1. If I get JUnit4 how do I configure ant to find it? 2. I don't really know wat classpath is, care to elaborate? 3. Is there a Maven tutorial similar to the one I'm using, where it lays out a project, junit tests and so on? – EasilyBaffled Jun 15 '13 at 22:53
  • 1
    No problem. I've added a complete maven example to my answer. I haven't used ant in years, having moved to maven in 2004. – Ray Toal Jun 16 '13 at 07:19
  • First thanks a ton for taking the time to write up the example. Second I'm running it, but at this point I'm getting "There are no tests to run." I'm not sure why I think something might be wrong with my JUnit jars and where they are. Third after running mcn, what is the target directory for? – EasilyBaffled Jun 16 '13 at 22:49
  • 1
    If you saw "no tests to run" make sure the files are in exactly the correct directories and the package names are exactly correct. The `@Test` annotation is necessary, too. Maven has you put all your source code in the `src` directory and it writes all of its output (e.g., `.class` files and jars and reports) into the `target` directory. The idea is that everything in `src` will go in to version control and everything in `target` is just output. [Here is some more info](http://cs.lmu.edu/~ray/notes/largejavaapps/) not targeted to beginners, though. – Ray Toal Jun 17 '13 at 05:14