Junit4入门

1、 配置环境
一般的eclipse都是自带Junit的jar文件的, 选择java build path—>选择

Libraries—>添加相应版本的junit即可。

2、 Junit使用Demo

NameFormat类
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NameFormat {


/**
* 名称格式化(全部使用小写字母,单词区分使用分隔符"-")
* @param str
* @return
* Example:
* Input:HumanInformation * Output:Human-information
*/
public String nameFormateOne(String str){
Pattern pattern = Pattern.compile("[A-Z]");
Matcher matcher = pattern.matcher(str);
StringBuffer sb = new StringBuffer();
if(matcher.find()){
matcher.appendReplacement(sb, "-"+matcher.group());
}
return matcher.appendTail(sb).toString().toLowerCase();
}

public static void main(String[] args) {
String result = new NameFormat().nameFormateOne("Vieri");
System.out.println(result);

}
}

对应的测试类TestNameFormat

import static org.junit.Assert.assertEquals;//note 1

import org.junit.BeforeClass;
import org.junit.Test;

import com.apusic.junit.NameFormat;

public class TestNameFormat {

private static NameFormat nf = null;
//测试前初始化工作
@BeforeClass// note 2
public static void initEnvironment(){//note 3
nf = new NameFormat();
System.out.println("annonatation: Before Class is excutor

once");
}

//input: null
@Test //note 4
public void nameFormateOneNull(){
String target = null;
String result = nf.nameFormateOne(target);
assertEquals(null,result);//note 5
System.out.println("Input null is tested");
}

//input:empty
@Test
public void nameFormateOneEmpty(){
String target = "";
String result = nf.nameFormateOne(target);
assertEquals("", result);
System.out.println("Input empty is tested");
}

@Test
public void nameFormateOneLowcase(){
String target = "humanInfo";
String result = nf.nameFormateOne(target);
assertEquals("human-info", result);
System.out.println("lowcase is tested");
}

@Test
public void nameFormateOneMajuscule(){
String target = "HumanInfo";
String result = nf.nameFormateOne(target);
assertEquals("human-info", result);
System.out.println("Input emajuscule is tested");
}
}

3、 测试用例解读
note1 由于没有像Junit3一样继承TestCase,所以必须导入所有静态断言
note2 被annotation”@BeforeClass”修饰的方法,在测试的最开始被执行一次,一般用来初始化整个测试需要的公共资源(比如数据库连接等),与之对应的annotation有“@AfterClass”。
note3 annotation”@BeforeClass”只能修饰public static void方法
note4 被annotation”@Test”修饰的方法,JUnit4测试框架会自动运行
note5 assertEquals()方法为junit的断言,该方法判断2个参数是否相等,如果不相等,则出发断言。

4、 运行测试用例
run as junit test 就可以了。 分析测试运行结果

5、 junit4扩展
A、@Test(timeout,expected)
被该annotation修饰的方法,在JUnit中将会自动被执行。该annotation只能修饰public void方法。参数timeout:规定该方法执行的时间,如果设置该参数,则被修饰方法必须在该时间内执行结束,否则抛出异常。
参数expected:规定该方法抛出异常,如果设置该参数,则被修饰方法在抛出该参数规定的异常的情况下才可能通过测试,否则测试失败。
B、 Before/After/BeforeClass/AfterClass/

6、 常用断言介绍
A、assertEquals([String message],Object target,Object result)
target与result不相等,中断测试方法,输出message
B、assertTrue/False([String message],Boolean result)
Result为 false/true,中断测试方法,输出message
C、assertNotNull/Null([String message],Obejct result
Retult= = null/result!=null,中断测试方法,输出message
D、assertSame/NotSame(Object target,Object result)
Traget与result 不指向/指向 同一内存地址(实例),中断测试方法,输出message
F、fail([String message])
中断测试方法,输出message

注释:JUNIT4详细的API参见http://junit.sourceforge.net/javadoc_40/index.html

——————————————关于Matcher类的group方法————————————
看API觉得着实费解,因此特意动手做了一下验证试验, 结果如下:
// create a Pattern
Pattern p = Pattern.compile("Bond");

// create a Matcher and use the Matcher.group() method
String candidateString = "My name is Bond. James Bond.";
Matcher matcher = p.matcher(candidateString);
// extract the group
matcher.find();
System.out.println(matcher.group());
打印输出: Bond

例2:
group(0)就是用整个正则表达式进行匹配


import java.util.regex.*;
class test{
public static void main(String[] args){
Pattern p = Pattern.compile("([0-9])([AD])([A-Z]*)");
Matcher m = p.matcher("0AALSDKFJ");
m.matches();
System.out.println(m.group(0));//注意,group(0)总是返回整个匹配字串
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
}
}


0AALSDKFJ
0
A
ALSDKFJ
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值