Creating the Student Class


Compile the test. You should expect to see problems,[6] since you have only built the test class StudentTest. StudentTest refers to a class named Student, which is nowhere to be foundyou haven't built it yet!

[6] You may see different errors, depending on your Java compiler and/or IDE.

 StudentTest.java:3: cannot find symbol symbol  : class Student location: class StudentTest       new Student("Jane Doe");           ^ 1 error 

Note the position of the caret (^) beneath the statement in error. It indicates that the Java compiler doesn't know what the source text Student represents.

The compile error is expected. Compilation errors are a good thingthey provide you with feedback throughout the development process. One way you can look at a compilation error is as your very first feedback after writing a test. The feedback answers the question: Have you constructed your code using proper Java syntax so that the test can execute?

Simplifying Compilation and Execution

In order to alleviate the tedium of re-executing each of these statements, you may want to build a batch file or script. An example batch file for Windows:

[View full width]

@echo off javac -cp c:\junit3.8.1\junit.jar *.java if not errorlevel 1 java -cp .;c:\junit3.8.1\junit.jar junit .awtui.TestRunner StudentTest

The batch file does not execute the JUnit test if the compiler returns any compilation errors. A similar script for use under Unix:

[View full width]

#!/bin/sh javac -classpath "/junit3.8.1/junit.jar" *.java if [ $? -eq 0 ]; then java -cp ".:/junit3.8.1/junit.jar" junit.awtui.TestRunner StudentTest fi

Another option under Unix is to use make, a classic build tool available on most systems. However, an even better solution is a tool called Ant. In Lesson 3, you will learn to use Ant as a cross-platform solution for building and running your tests.


To eliminate the current error, create a new class named Student.java. Edit it to contain the following code:

 class Student { } 

Run javac again, this time using a wildcard to specify that all source files should be compiled:

 javac -cp c:\junit3.8.1\junit.jar *.java 

You will receive a new but similar error. Again the compiler cannot find a symbol, but this time it points to the word new in the source text. Also, the compiler indicates that the symbol it's looking for is a constructor that takes a String argument. The compiler found the class Student, but now it needs to know what to do with respect to the "Jane Doe" String.

 StudentTest.java:3: cannot find symbol symbol  : constructor Student(java.lang.String) location: class Student       new Student("Jane Doe");       ^ 1 error 



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net