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. fiddler 代理不成功调试

    按照 http://jingyan.baidu.com/article/03b2f78c7b6bb05ea237aed2.html 设置. 之前可以用, 现在不能用. 1. 关闭 系统防火墙 2. 换 ...

  2. ios中webservice报文的拼接

    1.报文头需要密码验证的 - (void)sendAsynWebServiceRequest:(NSString *)nameSpace method:(NSString *)method reque ...

  3. Codeforces Round #261 (Div. 2)

    第一场难得DIV2简单+AK人数多: E:给出一张图,求最多的边数,满足:在这个边的集合中后面的边的权值大于前面的边; 思路:我们将图按权值排列,以为只可能边权值小的跟新权值大的所以对于一条边我们只跟 ...

  4. mongodb 主从服务器

    @set mongod=..\bin\mongod.exe set keyFile=key.key if not exist %keyFile% ( echo 123456>%keyFile% ...

  5. 您可能不知道的ASP.Net小技巧

    <!-- 页码和简介 --> 1.  在提交页面之后,保持滚动条的位置 可以在page指令上加上MaintainScrollPositionOnPostback指令 <%@ Page ...

  6. Ruby Profiler 详解之 ruby-prof(I)

    项目地址: ruby-prof 在上一篇 Ruby 中的 Profiling 工具中,我们列举了几种最常用的 Profiler,不过只是简单介绍,这一次详细介绍一下 ruby-prof 的使用方法. ...

  7. UVA 557 Burger 排列组合递推

    When Mr. and Mrs. Clinton's twin sons Ben and Bill had their tenth birthday, the party was held at t ...

  8. copy

    拷贝文件,不覆盖重复文件 yes no|cp -i a b

  9. java中静态代理,动态代理知识的补充

    文章转载自:http://blog.csdn.net/jialinqiang/article/details/8950989 一.Java动态代理 相对于静态代理的代理类在编译时生成(.class文件 ...

  10. Ffmpeg解析media容器过程/ ffmpeg 源代码简单分析 : av_read_frame()

    ffmpeg 源代码简单分析 : av_read_frame() http://blog.csdn.net/leixiaohua1020/article/details/12678577 ffmpeg ...