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.