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. ios 缓存策略

    NSURLRequestCachePolicy 缓存策略   1> NSURLRequestUseProtocolCachePolicy = 0, 默认的缓存策略, 如果缓存不存在,直接从服务端 ...

  2. nmap所有参数详解

    QQ:1258496116 端口:80http 443https 53dns 25smtp 22ssh 23telnet20.21ftp 110pop3 119nntp 143imap 179bgp ...

  3. IE如何兼容placeholder属性

    在前端开发中,经常需要为input设置placeholder属性,但是placeholder是HTML5新属性,在IE10以下不兼容,那么如何完美兼容呢? 网上搜索了一下,其实也挺简单的,可以采用以下 ...

  4. 纯CSS3实现3D动画导航,html5 webRTC技术实现免费网页电话拨打

    花了一周的时间完成了 说吧 (免费网页电话) 的前端开发工作,先将技术点总结如下: 免费电话采用最新的html5 webRTC 技术 实现互联网和电信MIS网互通实现网页电话,目前只有 google ...

  5. HDOJ 3183 A Magic Lamp

    A Magic Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. 微信电脑版也能用公众号自定义菜单 微信1.2 for Windows发布

    昨日,微信电脑版发布更新,版本为微信1.2 for Windows,最大的特色就是加入了保存聊天记录功能,可以使用公账号菜单,手机上收藏的表情也能在电脑版上发送,可以接收转账消息. 本次微信pc版更新 ...

  7. MYSQL注入天书之服务器(两层)架构

    Background-6 服务器(两层)架构 首先介绍一下29,30,31这三关的基本情况: 服务器端有两个部分:第一部分为tomcat为引擎的jsp型服务器,第二部分为apache为引擎的php服务 ...

  8. .NET Framework 框架简述01

    NET技术可以以规范和实现两部分来划分.   规范:   公共语言架构(Common Language Infrastructure, CLI),主要包括 1.通用类型系统(Common Type S ...

  9. sizeof学习理解

    以下内容转自: http://www.cnblogs.com/ComputerG/archive/2012/02/02/2335611.html 博问 闪存 首页 新随笔 联系 管理 随笔- 72  ...

  10. MVC中前台如何向后台传递数据------$.get(),$post(),$ajax(),$.getJSON()总结

    一.引言 MVC中view向controller传递数据的时候真心是一个挺让人头疼的一件事情.因为原理不是很懂只看一写Dome,按葫芦画瓢只能理解三分吧. 二.解读Jquery个Ajax函数 $.ge ...