To represent the initial need to capture student information, start by creating a class that will act as your test case. First, create a new directory or folder on your machine.[1] Then create a file named StudentTest.java in this directory. For the time being, you will save, compile, and execute code out of this single directory. Type the following code into your editor:
public class StudentTest extends junit.framework.TestCase { } Save the file using the file name StudentTest.java. The two lines of code in StudentTest.java define a class named StudentTest. Everything that comes between the opening and closing braces ({and }) is part of the definition of StudentTest. You must designate the class as public in order for the testing framework JUnit to recognize it. I will cover public in more depth later. For now, understand that the public keyword allows other code, including the code in the JUnit framework, to work with the code you write. The code phrase extends junit.framework.TestCase declares StudentTest to be a subclass of another class named junit.framework.TestCase. This means that StudentTest will acquire, or inherit, all the capabilities (behavior) and data (attributes) from a class named junit.framework.TestCase. Student Test will also be able to add its own behavior and/or attributes. The extends clause also allows the JUnit user interface to recognize the StudentTest class as something that contains testing methods. The UML class diagram in Figure 1.2 shows the inheritance relationship between StudentTest and junit.framework.TestCase. StudentTest is now dependent upon both junit.framework.TestCase and Student. Remember that the arrowhead distinguishes between the type of dependencya closed arrowhead indicates an inheritance relationship. Figure 1.2. StudentTest Inherits from junit.framework.TestCaseYour next step is to compile the StudentTest class. In order to do so, you must tell Java where to find any other classes that StudentTest references. Currently this includes only the class junit.framework.TestCase. This class can be found in a packaged format known as a JAR (Java ARchive) file. The JAR file in which junit.framework.TestCase can be found also contains many other classes that comprise the JUnit framework. I discuss JAR files in more depth in Additional Lesson III. For now, you only need to understand how to tell Java where to find the JUnit JAR file. You do so using the classpath.
From the command line, you can specify the classpath at the same time that you compile the source file. javac -classpath c:\junit3.8.1\junit.jar StudentTest.java You must specify either the absolute or relative location of the file JUnit. jar;[2] you will find it in the directory where you installed JUnit. This example specifies the absolute location of JUnit.jar.
You should also ensure that you are in the same directory as StudentTest.java. If you omit the classpath, the Java compiler will respond with an error message: StudentTest.java:1: package junit.framework does not exist public class StudentTest extends junit.framework.TestCase { ^ 1 error Your IDE will allow you to specify the classpath somewhere in the current project's property settings. In Eclipse, for example, you specify the classpath in the Properties dialog for the project, under the section Java Build Path, and in the Libraries tab. |