`
neu_gefei
  • 浏览: 6160 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

TDD 第一章 使用入门(四)

阅读更多
A Simple Test
[size=large]
    为了实现保存学生信息的最初需求,我们先建立一个表现测试用例的类。首先,建立一个目录或文件夹来放这个类。接下来新建一个文件StudentTest.java在这个目录中。在接下来的时间里你会在这个目录下进行保存,编译,执行等一步步操作。在你的编辑器中输入如下的代码:
  
 public class StudentTest extends junit.framework.TestCase {
}

    以StudentTest.java为名保存这个文件。
    上面的两行代码定义了一个名为StudentTest的类。在两个大括号之间的部分是对这个类的定义。
    你必须标明这个类是public的,这样做可以使Junit测试框架认出这个类。我将在今后的课程中深入讲解public,现在你只需要弄懂这个public关键字允许别的代码(包括Junit框架中的)与这段代码一起工作。
    代码段extends junit.framework.TestCase 声明StudentTest类是另一个类junit.framework.TestCase的子类,就是说这个类将拥有,或者说继承junit.framework.TestCase的所有特性和数据,作为子类的StudentTest类还可以添加自己特有的行为和属性。extends语句同时可以告诉Junit测试框架StudentTest类是一个包含了测试方法的测试类。
    1.2节中的图展示了这两个类的继承关系,在UML图中箭头的方向指明了继承关系的父子两方。
    Figure 1.2. StudentTest Inherits from junit.framework.TestCase
    你下一步需要做的是编译这个StudentTest类。在编译时你需要告诉编译器StudentTest类以及它的所有引用的位置。当然,目前只引用了junit.framework.TestCase,这个类在一个Jar包 (Java ARchive)里,这个包还包含了构成Junit测试框架的哪些类。
    我将在附加课程中深入讲述ClassPath的含义,现在你只需要知道如何告诉Java那个Jar包在哪里就可以了。
    在命令行中,你可以在编译的同时告诉Java那个Jar包的位置。
   
javac -classpath c:\junit3.8.1\junit.jar StudentTest.java

    你可以以相对目录和绝对目录两种方式告诉Java位置的信息,在上例中我用了绝对路径的方式。同时你还得确保那个StudentTest.java就在当前路径下。
    如果你省略了classpath就会报错,具体错误如下:
 
  StudentTest.java:1: package junit.framework does not exist
public class StudentTest extends junit.framework.TestCase {
                            ^
1 error

    一些IDE工具可以帮助你在项目属性的地方配置classpath的信息,你将可以更简便的做这些事情。
   
引用
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:

[1] The instructions in this lesson are geared toward command-line use of Java. If you are using an IDE, you will create a class named StudentTest in something called the "default package." If you are prompted for any package name, do not enter anything.
    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.
    Your 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.

[2] An absolute location represents the explicit, complete path to a file, starting from the drive letter or root of your file system. A relative location contains a path to a file that is relative to your current location. Example: If StudentTest is in /usr/src/student and JUnit.jar is in /usr/src/JUnit3.8.1, you can specify the relative location as ../JUnit/3.8.1/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.




[/size]
  • 大小: 11.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics