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

看自己总结的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的更多相关文章
- 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 ...
- 从零开始学JAVA(02)-用Eclipse写hello World
在安装好JAVA开发环境的前提下开始以下工作,以下文章参考http://blog.csdn.net/ojtojt/article/details/3476157文章,进行测试编写日记,内容版权归原作者 ...
- OOP设计模式[JAVA]——02观察者模式
观察者模式 观察者模式的设计原则 为交互对象之间的松耦合设计而努力,使对象之间的相互依赖降到最低. 观察者模式也是对象行为型模式,其意图为:定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时 ...
- 原来你是这样的JAVA[02]-包、传参、构造器
一.包(package) 在java程序中,一个java源文件称为编译单元,以.java后缀命名.编译单元内可以有一个public类,类名必须与文件名相同.注意:每个编译单元只能有一个public类. ...
- .Net转Java.02.数据类型
.NET中常见的数据类型分类分别是值类型和引用类型 值类型包括(基元类型.struct.枚举) 引用类型包括(类.类.数组.接口.指针) Java分为,基本类型和类 C# Java 值类型 ...
- [Java] 02 String的常用方法
public class TestString{ public static void main(String[] args){ String str1 = "123"; Stri ...
- java 02 内部类
- 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 ...
- .Net转Java.01.从Main(main)函数说起
在C#中,main函数的签名可以有四种 static void Main(string[] args)static void Main()static int Main(string[] args)s ...
随机推荐
- 使用TCP协议的NAT穿透技术
一直以来,说起NAT穿透,很多人都会被告知使用UDP打孔这个技术,基本上没有人会告诉你如何使用TCP协议去穿透(甚至有的人会直接告诉你TCP协议是无法实现穿透的).但是,众所周知的是,UDP是一个无连 ...
- 2014年03月09日攻击百度贴吧的XSS蠕虫源码
var n=PageData.user.user_forum_list.info.length; var num=0; var config = { titles: ["\u4f60\u76 ...
- vuforia 结合 unity3d 开发 AR 的 androidAPP 总结
原地址:https://software.intel.com/zh-cn/blogs/2014/07/09/vuforia-unity3d-ar-androidapp/?utm_campaign=CS ...
- 如何在 Swift 语言下使用 iOS Charts API 制作漂亮图表?
[编者按]本文作者 Joyce Echessa 是渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.文中作者通过示例介绍用 ios-charts 库创建简易美观的 ...
- Codeforces 414C Mashmokh and Reverse Operation
题意:给你2^n个数,每次操作将其分成2^k份,对于每一份内部的数进行翻转,每次操作完后输出操作后的2^n个数的逆序数. 解法:2^n个数,可以联想到建立一棵二叉树的东西,比如 2,1,4,3就可以 ...
- POJ 1939
#include<iostream> #include<iomanip> #define MAXN 10000 using namespace std; ]; int main ...
- ***Xcode Interface Builder或Storyboard中可建立那两种连接?
在Xcode Interface Builder或Storyboard中,可建立到输出口(IBOutlet)和操作(方法,IBAction)的连接. IBOutlet are for output C ...
- log4net 一分钟上手
1. 首先从apache网站下载log4net, http://logging.apache.org/log4net/download_log4net.cgi .我下的是最新版本 log4net- ...
- http://blog.sina.com.cn/s/blog_5b9b4abe01017638.html
http://blog.sina.com.cn/s/blog_5b9b4abe01017638.html
- IOS中延时执行的几种方式的比较
本文列举了四种延时执行某函数的方法及其一些区别.假如延时1秒时间执行下面的方法. - (void)delayMethod { NSLog(@"execute"); } 1.perf ...