【笔记】Eclipse and Java for Total Beginners—004

Lesson 04 – JUnit Testing in Eclipse, Part 1

  • Create test source folder
  • Create Person Test class
  • Run first JUnit test

1. Scrapbook的优/缺点

  • Great for exploring new Java classes and trying commmands.
  • Does not let you automate testing

2. Unit Testing的特点

  • Automated testing of all methods
  • Very valuable when making changes to code
  • Key element of agile software development
  • Unit testing support built-in to Eclipse

3. 把test classes和application code放在不同的目录,好处易见。首先,为test classes创建一个文件夹

    Select Project / File / New / Source folder / test (Folder name) / Finish.

4. 接下来,想把test classes放在和被测试的classes同一个package里,所以在test目录下创建一个同名的package。

    Select test / File / New / Package / org.totalbeginner.tutorial (name) / Finish .

5. 接着,创建第1个JUnit test。

    选择刚创建的package / RC org.totalbeginner.tutorial / New / JUnit test cae / PersonTest (name) / Person (Class under test )/ Next / 勾选 Person, setName, setMaximumBooks.

这里需要指定libray的路径。

6. 指定library的路径

    方法1:Quick Fix

              右击或Ctrl+1.

    方法2:RC TotalBeginner / Build Path / Configure Build path.. / Libraries / Add Library.

7. 运行一下test。

    Run / Run As / JUnit Test.

  
    
1 package org.totalbeginner.tutorial;
2
3   import junit.framework.TestCase;
4
5   public class PersonTest extends TestCase {
6
7 public void testPerson() {
8 fail( " Not yet implemented " );
9 }
10
11 public void testSetName() {
12 fail( " Not yet implemented " );
13 }
14
15 public void testSetMaximumBooks() {
16 fail( " Not yet implemented " );
17 }
18
19 }

你可能感兴趣的:(eclipse)