cs108 java 02

Eclipise

1. import, 所有的homework 是以 eclipse project directories 的形式.

   所以要选择 “File –> Import “, Existing project “, 选择 “copy to workspace” option will copy the project directory into Eclipse’s workspace directory.

2. debugging

   double click in the editor at the left of a line to set a breakpoint on that line.

   Use “Debug” instead of “Run” to run in the debugger.

3. Eclipse 特性

   image

看自己总结的eclipse快捷键更好一点.

Unit test( 单元测试 )

Unit test 的目的, for each piece of "work" code --a class or a method, pair the work code with some "unit test" code ( 每完成一个类或方法就测试一下 )

这种测试不用是 "exhaustive", 非常完整的测试, 功能实现没问题, 基本数据测试就可以了.

Unit tests are a standard, maintained way to keep tests in parallel with the work code

只要测试一些明显的, 基本的数据就可以了.

Right-click on the class to be tested.

Junit 很流行, new-> JUnit Test case, 比如类名为 Binky, 那么测试的类名为 BinkyTest. (自动生成的)

这个生成是在本 project 内完成的.

Junit 依赖一些类, file: junit.jar 添加的办法: -自动添加-, 当你选择new->Junit Test case时, 如果第一次, eclipse会提示你让你安装 Junit.jar, -手动安装-, properities, –> Java Build Path –> Libraries –> Add Jar button .

测试方法的话, 比如方法名叫 Foo, 那么测试的方法名为 testFoo(), 像下边的

@Test

public void testFoo() {}

Example 1

1. CLASS

 1 import java.util.*;
 2 
 3 /*
 4  * Emails class -- unit testing example.
 5  * Encapsulates some text with email address in it.
 6  * getUsers() returns a list of the usernames from the text. 
 7  */
 8 public class Emails {
 9     
10     // Sets up a new Emails obj with the given text
11 
12     public Emails(String text) {
13         this.text = text;
14     }
15     
16     // Returns a list of the usernames found in the text.
17     // We'll say that a username is one or more letters, digits,
18     // or dots to the left of a @.
19     
20     public List<String> getUsers() {
21         int pos = 0;
22         List<String> users = new ArrayList<String>();
23         
24         while (true) {
25             int at = text.indexOf('@', pos);
26             if (at == -1) break;
27             
28             // Look backwards from at
29             int back = at - 1;
30             while (back >=0 &&
31                     (Character.isLetterOrDigit(text.charAt(back)) || text.charAt(back) == '.')) {
32                 back--;
33             } 
34             // Now back is before start of username
35             String user = text.substring(back+1, at);
36             
37             if (user.length() > 0) users.add(user);
38             
39             // Advance pos for next time
40             pos = at + 1;
41             
42         }
43         return users;
44     }
45 
46     /* private values */    
47     private String text;
48     
49 }
CLASS


2. TEST CLASS

 1 import static org.junit.Assert.assertEquals;
 2 
 3 import java.util.Arrays;
 4 import java.util.Collections;
 5 
 6 import org.junit.Test;
 7 
 8 /**
 9  * EmailsTest -- unit tests for the Emails class.
10  * @author Ronnie
11  *
12  */
13 public class EmailsTest {
14 
15     // Basic use
16     @Test
17     public void testUsersBasic() {
18         Emails emails = new Emails("foo bart@cs.edu xyz marge@ms.com baz");
19         assertEquals(Arrays.asList("bart", "marge"), emails.getUsers());
20         // Note: Array.asList(...) is a handy way to make list literal.
21         // Also note that .equals() works for collections, so the above works.
22     }
23     
24     // Weird chars -- push on what chars are allowed
25     @Test
26     public void testUsersChars() {
27         Emails emails = new Emails("fo f.ast@cs.edu bar&a.2.c@ms.com");
28         assertEquals(Arrays.asList("f.ast", "a.2.c"), emails.getUsers());
29     }
30     
31     // Hard cases -- push on unusual, edge cases
32     @Test
33     public void testUsersHard() {
34         Emails emails = new Emails("x y@cs 3@ @z@");
35         assertEquals(Arrays.asList("y", "3", "z"), emails.getUsers());
36         
37         // No emails
38         emails = new Emails("no emails here!");
39         assertEquals(Collections.emptyList(), emails.getUsers());
40         
41         // All @, all the time!
42         emails = new Emails("@@@");
43         assertEquals(Collections.emptyList(), emails.getUsers());
44         
45         // Empty string
46         emails = new Emails("");
47         assertEquals(Collections.emptyList(), emails.getUsers());
48         
49     }
50     
51     
52 }
TEST CLASS


Example 2

 1 public class Myunit {
 2     public String concatenate(String one, String two) {
 3         return one + two;
 4     }
 5 }
 6 
 7 
 8 //========================================
 9 
10 import static org.junit.Assert.*;
11 
12 import org.junit.Test;
13 
14 
15 public class MyunitTest {
16 
17     @Test
18     public void testConcatenate() {
19         Myunit myUnit = new Myunit();
20         
21         String result = myUnit.concatenate("one", "two");
22         assertEquals("onetwo", result);
23     }
24 
25 }
ALL

以上代码中, Unit Test 目的是测试所有的 public 方法, 在上例中, 只有1个public 方法就是 concatenate, 一般来讲, 每个测试方法对应一个方法, 命名方式为 test该方法名, 例如 testConcatenate, 但是也有几个测试方法对应一个方法的情况, 比如要测试的方法很大. assertEquals 很明显是用来比较的, 比较你要的结果是否和程序得出的结果一直. The asserEquals() method is a statically imported method, which normally resides in the org.junit.Assert class.

用来测试的方法上边有个 @Test 用来注释,表示这个方法是一个 unit test.  注意这样有一个好处就是, 这个方法在eclipse中可以运行(没有 main 方法 ), 如果没有这个@Test标志, 那么这个测试方法将无法运行.

原文地址:https://www.cnblogs.com/moveofgod/p/3437444.html