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 特性

看自己总结的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

 import java.util.*;

 /*
* Emails class -- unit testing example.
* Encapsulates some text with email address in it.
* getUsers() returns a list of the usernames from the text.
*/
public class Emails { // Sets up a new Emails obj with the given text public Emails(String text) {
this.text = text;
} // Returns a list of the usernames found in the text.
// We'll say that a username is one or more letters, digits,
// or dots to the left of a @. public List<String> getUsers() {
int pos = 0;
List<String> users = new ArrayList<String>(); while (true) {
int at = text.indexOf('@', pos);
if (at == -1) break; // Look backwards from at
int back = at - 1;
while (back >=0 &&
(Character.isLetterOrDigit(text.charAt(back)) || text.charAt(back) == '.')) {
back--;
}
// Now back is before start of username
String user = text.substring(back+1, at); if (user.length() > 0) users.add(user); // Advance pos for next time
pos = at + 1; }
return users;
} /* private values */
private String text; }

CLASS

2. TEST CLASS

 import static org.junit.Assert.assertEquals;

 import java.util.Arrays;
import java.util.Collections; import org.junit.Test; /**
* EmailsTest -- unit tests for the Emails class.
* @author Ronnie
*
*/
public class EmailsTest { // Basic use
@Test
public void testUsersBasic() {
Emails emails = new Emails("foo bart@cs.edu xyz marge@ms.com baz");
assertEquals(Arrays.asList("bart", "marge"), emails.getUsers());
// Note: Array.asList(...) is a handy way to make list literal.
// Also note that .equals() works for collections, so the above works.
} // Weird chars -- push on what chars are allowed
@Test
public void testUsersChars() {
Emails emails = new Emails("fo f.ast@cs.edu bar&a.2.c@ms.com");
assertEquals(Arrays.asList("f.ast", "a.2.c"), emails.getUsers());
} // Hard cases -- push on unusual, edge cases
@Test
public void testUsersHard() {
Emails emails = new Emails("x y@cs 3@ @z@");
assertEquals(Arrays.asList("y", "3", "z"), emails.getUsers()); // No emails
emails = new Emails("no emails here!");
assertEquals(Collections.emptyList(), emails.getUsers()); // All @, all the time!
emails = new Emails("@@@");
assertEquals(Collections.emptyList(), emails.getUsers()); // Empty string
emails = new Emails("");
assertEquals(Collections.emptyList(), emails.getUsers()); } }

TEST CLASS

Example 2

 public class Myunit {
public String concatenate(String one, String two) {
return one + two;
}
} //======================================== import static org.junit.Assert.*; import org.junit.Test; public class MyunitTest { @Test
public void testConcatenate() {
Myunit myUnit = new Myunit(); String result = myUnit.concatenate("one", "two");
assertEquals("onetwo", result);
} }

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标志, 那么这个测试方法将无法运行.

cs108 java 02的更多相关文章

  1. Effective Java 02 Consider a builder when faced with many constructor parameters

    Advantage It simulates named optional parameters which is easily used to client API. Detect the inva ...

  2. 从零开始学JAVA(02)-用Eclipse写hello World

    在安装好JAVA开发环境的前提下开始以下工作,以下文章参考http://blog.csdn.net/ojtojt/article/details/3476157文章,进行测试编写日记,内容版权归原作者 ...

  3. OOP设计模式[JAVA]——02观察者模式

    观察者模式 观察者模式的设计原则 为交互对象之间的松耦合设计而努力,使对象之间的相互依赖降到最低. 观察者模式也是对象行为型模式,其意图为:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时 ...

  4. 原来你是这样的JAVA[02]-包、传参、构造器

    一.包(package) 在java程序中,一个java源文件称为编译单元,以.java后缀命名.编译单元内可以有一个public类,类名必须与文件名相同.注意:每个编译单元只能有一个public类. ...

  5. .Net转Java.02.数据类型

    .NET中常见的数据类型分类分别是值类型和引用类型 值类型包括(基元类型.struct.枚举) 引用类型包括(类.类.数组.接口.指针) Java分为,基本类型和类   C#   Java   值类型 ...

  6. [Java] 02 String的常用方法

    public class TestString{ public static void main(String[] args){ String str1 = "123"; Stri ...

  7. java 02 内部类

  8. Effective Java Index

    Hi guys, I am happy to tell you that I am moving to the open source world. And Java is the 1st langu ...

  9. .Net转Java.01.从Main(main)函数说起

    在C#中,main函数的签名可以有四种 static void Main(string[] args)static void Main()static int Main(string[] args)s ...

随机推荐

  1. [工作积累] Software keyboard not shown on Activity.onCreate

    protected void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setCo ...

  2. NDK: unable to watch local variables after using GCC4.8

    the problem definitly apears after changing toolchain from gcc 4.6 to gcc 4.8. here's a solution wit ...

  3. 将apk安装包安装在Android真机或者模拟器

    例子如下: 一.准备 打开MAC PC上的Android模拟器方法:打开eclipse-—>window->Android Virtual Device Manager 如果是安装在真机上 ...

  4. JAVA算法系列 冒泡排序

    java算法系列之排序 手写冒泡 冒泡算是最基础的一个排序算法,简单的可以理解为,每一趟都拿i与i+1进行比较,两个for循环,时间复杂度为 O(n^2),同时本例与选择排序进行了比较,选择排序又叫直 ...

  5. SSRF攻击实例解析

    ssrf攻击概述 很多web应用都提供了从其他的服务器上获取数据的功能.使用用户指定的URL,web应用可以获取图片,下载文件,读取文件内容等.这个功能如果被恶意使用,可以利用存在缺陷的web应用作为 ...

  6. YTKNetwork

    YTKNetwork 是猿题库 iOS 研发团队基于 AFNetworking 封装的 iOS 网络库,其实现了一套 High Level 的 API,提供了更高层次的网络访问抽象. YTKNetwo ...

  7. WSDL相关文档

    http://msdn.microsoft.com/en-us/library/ms996486.aspx http://msdn.microsoft.com/en-us/library/aa4685 ...

  8. org.apache.kafka.common.network.Selector

    org.apache.kafka.common.client.Selector实现了Selectable接口,用于提供符合Kafka网络通讯特点的异步的.非阻塞的.面向多个连接的网络I/O. 这些网络 ...

  9. Extjs整体加载树节点

    Ext.onReady(function () {             Ext.define('company', {                 extend: 'Ext.data.Mode ...

  10. linux shell的输出效果修改方法(界面颜色)

    文本终端的颜色可以使用“ANSI非常规字符序列”来生成.举例: echo -e "\033[44;37;5m ME \033[0m COOL" 以上命令设置背景成为蓝色,前景白色, ...