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. UIResponder

    原网址:http://www.cnblogs.com/kuku/archive/2011/11/12/2246389.html 在 iOS 中,一个 UIResponder 对象表示一个可以接收触摸屏 ...

  2. eclipse中本地项目怎么和svn中的项目关联?

    网速不好,通过别的方式把项目下载到本地硬盘,然后导入eclipse,代码修改后怎么提交到svn呢? 这个纠结了好久的问题... 首先要确定eclipse中安装了svn插件. 然后在“svn资源库研究” ...

  3. 对话机器学习大神Yoshua Bengio(上)

    Yoshua Bengio教授(个人主页)是机器学习大神之一,尤其是在深度学习这个领域.他连同Geoff Hinton老先生以及 Yann LeCun(燕乐存)教授,缔造了2006年开始的深度学习复兴 ...

  4. CSL概述(翻译总结自TI官方文档)

    一.简单介绍 CSL是函数,宏和符号常数的集合,用来控制和配置片上外设.(Chip Support Library) 每一个外设都有自己对应的CSL模块.每个模块有自己的支持符,来表示对于给定的设备, ...

  5. mysql存储过程和函数使用实例

    1.需求:根据输入的年份,月份,和当前系统的年份比较,不满1年按1年计算,多出1年11个月也按1年计算. 2.计算得出来的使用年份,计算车辆残值. 3.存储过程 DELIMITER $$ USE `d ...

  6. Sqli-labs less 53

    Less-53 和less51是一样的,只是这里的mysql错误不会在前台显示,但是对于stacked injection是一样的利用方式 http://127.0.0.1/sqli-labs/Les ...

  7. HDU 4576 Robot(概率dp)

    题目 /*********************复制来的大致题意********************** 有N个数字,M个操作, 区间L, R. 然后问经过M个操作后落在[L, R]的概率. * ...

  8. iOS多线程的初步研究(三)-- NSRunLoop

    弄清楚NSRunLoop确实需要花时间,这个类的概念和模式似乎是Apple的平台独有(iOS+MacOSX),很难彻底搞懂(iOS没开源,呜呜). 官网的解释是说run loop可以用于处理异步事件, ...

  9. (转)Android: NDK编程入门笔记

    转自: http://www.cnblogs.com/hibraincol/archive/2011/05/30/2063847.html 为何要用到NDK? 概括来说主要分为以下几种情况: 1. 代 ...

  10. js小技巧(二)

    //移动的图层,拖动 1.<span style='position:absolute;width:200;height:200;background:red' onmousedown=Mous ...