【原创】Junit4详解一:Junit总体介绍
Junit是一个可编写重复测试的简单框架,是基于Xunit架构的单元测试框架的实例。Junit4最大的改进是大量使用注解(元数据),很多实际执行过程都在Junit的后台做完了,而且写test case 的类不需要继承TestCase,只需要在所要做test case的方法前加@Test 注解即可。
如:
import static org.junit.Assert.*;
2 public class TestCaculatorClass {
3 @Test
4 public void test() throws IOException, RuntimeException{
5 CaculatorClassForTest cal = new CaculatorClassForTest();
6 assertEquals(30, cal.sum(10, 20));
7 }
8 }
View @Test Code
直接点击右键,run as... Junit Test即可运行此test case。
Assert类里面有很多assert方法,包括:assertEquals(), assertNotNull(),assertTtrue(),assertFalse(),assertThat()等,其中assertThat用的是match的形式。
因此,Junit提供很多中Match,其中CoreMatchers是其中一个比较完善的实现类。具体有上面方法可以查阅CoreMatchers类。
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.both;
import static org.junit.matchers.JUnitMatchers.containsString;
import static org.junit.matchers.JUnitMatchers.everyItem;
import static org.junit.matchers.JUnitMatchers.hasItems; import java.util.Arrays; import org.hamcrest.core.CombinableMatcher;
import org.junit.Test; public class AssertTests {
@Test
public void testAssertArrayEquals() {
byte[] expected = "trial".getBytes();
byte[] actual = "trial".getBytes();
org.junit.Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);
} @Test
public void testAssertEquals() {
org.junit.Assert.assertEquals("failure - strings not same", 5l, 5l);
} @Test
public void testAssertFalse() {
org.junit.Assert.assertFalse("failure - should be false", false);
} @Test
public void testAssertNotNull() {
org.junit.Assert.assertNotNull("should not be null", new Object());
} @Test
public void testAssertNotSame() {
org.junit.Assert.assertNotSame("should not be same Object", new Object(), new Object());
} @Test
public void testAssertNull() {
org.junit.Assert.assertNull("should be null", null);
} @Test
public void testAssertSame() {
Integer aNumber = Integer.valueOf(768);
org.junit.Assert.assertSame("should be same", aNumber, aNumber);
} // JUnit Matchers assertThat
@Test
public void testAssertThatBothContainsString() {
org.junit.Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));
} @Test
public void testAssertThathasItemsContainsString() {
org.junit.Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));
} @Test
public void testAssertThatEveryItemContainsString() {
org.junit.Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));
} // Core Hamcrest Matchers with assertThat
@Test
public void testAssertThatHamcrestCoreMatchers() {
assertThat("good", allOf(equalTo("good"), startsWith("good")));
assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));
assertThat("good", anyOf(equalTo("bad"), equalTo("good")));
assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));
assertThat(new Object(), not(sameInstance(new Object())));
} @Test
public void testAssertTrue() {
org.junit.Assert.assertTrue("failure - should be true", true);
}
}
View Assert Code
问题一,我可不可以把多个测试类放在一起执行?
回答: 可以。org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);利用这样,把test case 的类放进去,然后放在main()方法里执行。
问题二,@RunWith这个注解有什么作用?
回答:Junit4的默认runner为BlockJunit4ClassRunner,但是Junit4包括第三方软件还提供很多其他的runner,这样如果,我们想让我们的测试类用专门的runner来运行,这时候就可以用@RunWith(Suit.class
)标注测试类。其他特殊的runner有:
1. Suite: 字面理解是一个套装,通俗地讲,就是你可以把很多测试类放在一起,然后建一个类,标注为Suite.class,那么如果执行这个类,就会把所有的测试类一起执行。
import org.junit.runner.RunWith;
import org.junit.runners.Suite; @RunWith(Suite.class)
@Suite.SuiteClasses({
TestFeatureLogin.class,
TestFeatureLogout.class,
TestFeatureNavigate.class,
TestFeatureUpdate.class
}) public class FeatureTestSuite {
// the class remains empty,
// used only as a holder for the above annotations
}
View Suite Code
2. Parameterized:根据所设计的参数来执行测试。假设我们要测试某一个方法,它有两个参数,每个参数需要设计不同值,那么我们最开始就是需要为每个参数设计一个测试方法,这样就很麻烦,10种case就得10个方法,但是有了Parameterized runner,我们可以设计一个方法,多种参数来执行test case。
package com.citi.risk.core.test.impl; public class CaculatorClassForTest { private int o1;
private int o2;
public int getO1() {
return this.o1;
}
public void setO1(int value) {
this.o1 = value;
}
public int getO2() {
return this.o2;
}
public void setO2(int value) {
this.o2 = value;
} public CaculatorClassForTest() {}
public CaculatorClassForTest(int o1, int o2) {
this.o1 = o1;
this.o2 = o2;
} public int sum(int o1, int o2){
if(o1 > 200) {
throw new RuntimeException("o1 is too big");
}
if(o2 > 200) {
throw new RuntimeException("o2 is too big");
}
int sum;
sum = o1 + o2;
return sum;
}
}
View CaculatorClassForTest Code
package com.citi.risk.core.test.impl; import static org.junit.Assert.*; import java.io.IOException;
import java.util.List; import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters; import com.google.common.collect.Lists; @RunWith(Parameterized.class)
public class TestCaculatorClass {
@Rule
public ExpectedException thrown = ExpectedException.none(); @Parameters
public static List<Object[]> data() {
return Lists.asList(new Object[]{-1, 1, 0}, new Object[][]{{20, 20, 40},{30, 30, 60},{-5, -5, -10}});
}
@Parameter(value = 0)
public int o1;
@Parameter(value = 1)
public int o2;
@Parameter(value = 2)
public int expector; @Test
public void test() throws IOException, RuntimeException{
CaculatorClassForTest cal = new CaculatorClassForTest();
assertEquals(expector, cal.sum(o1, o2));
} @Test
public void testO1Exception(){
CaculatorClassForTest cal = new CaculatorClassForTest();
thrown.expect(RuntimeException.class);
thrown.expectMessage("o1 is too big");
cal.sum(300, 100);
}
@Test
public void testO2Exception(){
CaculatorClassForTest cal = new CaculatorClassForTest();
thrown.expect(RuntimeException.class);
thrown.expectMessage("o2 is too big");
cal.sum(100, 300);
} }
View TestCaculatorClass Code
以上两个类就是测试了Parameterized runner, 参数会自动匹配。它其实就是,看我们传入几种case, 也就是List.size(),然后,把类里面的方法,循环重复执行size()数目。
3. Categories:容易理解就是分类执行。假设我们有一种case: 我们写好了两个测试类,类A,类B,A有两个方法a(), b(),这时候我们有一个类来执行这两个类的test case,但是我们在类A里只想执行A.b(),但却不执行A.a(),这个时候我们可以用Categories runner。
public interface FastTests { /* category marker */ }
public interface SlowTests { /* category marker */ } public class A {
@Test
public void a() {
fail();
} @Category(SlowTests.class)
@Test
public void b() {
}
} @Category({SlowTests.class, FastTests.class})
public class B {
@Test
public void c() { }
} @RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b and B.c, but not A.a
} @RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@ExcludeCategory(FastTests.class)
@SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite
public class SlowTestSuite {
// Will run A.b, but not A.a or B.c
}
View Categories Code
4. Enclosed:如果我们把tests放在了内部类,这时候执行外部类是无法执行里面的test cases,这种情况下,就应该在outer class 用Enclosed runner。
要测试的类 Address:
package abstractions.domain; import java.io.Serializable; import com.google.common.collect.ComparisonChain; public class Address implements Serializable, Comparable<Address> { private static final long serialVersionUID = 1L;
private final String address1;
private final String city;
private final String state;
private final String zip; private Address(Builder builder) {
this.address1 = builder.address1;
this.city = builder.city;
this.state = builder.state;
this.zip = builder.zip;
} public String getAddress1() {
return address1;
} public String getCity() {
return city;
} public String getState() {
return state;
} public String getZip() {
return zip;
} @Override
public int compareTo(Address that) {
return ComparisonChain.start().compare(this.zip, that.zip).compare(this.state, that.state)
.compare(this.city, that.city).compare(this.address1, that.address1).result();
} @Override
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (getClass() != obj.getClass()) { return false; }
final Address that = (Address) obj; return com.google.common.base.Objects.equal(this.address1, that.address1)
&& com.google.common.base.Objects.equal(this.city, that.city)
&& com.google.common.base.Objects.equal(this.state, that.state)
&& com.google.common.base.Objects.equal(this.zip, that.zip);
} @Override
public int hashCode() {
return com.google.common.base.Objects.hashCode(getAddress1(), getCity(), getCity(), getState(), getZip());
} @Override
public String toString() {
return com.google.common.base.Objects.toStringHelper(this).addValue(getAddress1()).addValue(getCity()).addValue(getState()).addValue(getZip()).toString();
} public static class Builder { private String address1;
private String city;
private String state;
private String zip; public Builder address1(String address1) {
this.address1 = address1;
return this;
} public Address build() {
return new Address(this);
} public Builder city(String city) {
this.city = city;
return this;
} public Builder state(String state) {
this.state = state;
return this;
} public Builder zip(String zip) {
this.zip = zip;
return this;
}
}
}
View Address Code
test case:
package abstractions.domain; import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat; import java.io.Serializable; import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith; import testhelpers.ComparabilityTestCase;
import testhelpers.EqualsHashCodeTestCase;
import testhelpers.SerializabilityTestCase; /**
* The Class AddressTest.
*/
@RunWith(Enclosed.class)
public class AddressTest { /**
* The Class AddressComparabilityTest.
*/
public static class AddressComparabilityTest extends ComparabilityTestCase<Address> { @Override
protected Address createEqualInstance() throws Exception {
return new Address.Builder().address1("2802 South Havana Street").city("Aurora").state("CO").zip("80014").build();
} @Override
protected Address createGreaterInstance() throws Exception {
return new Address.Builder().address1("9839 Carlisle Boulevard NE").city("Albuquerque").state("NM").zip("87110").build();
} @Override
protected Address createLessInstance() throws Exception {
return new Address.Builder().address1("14 Broad St").city("Nashua").state("NH").zip("03064").build();
}
} /**
* The Class AddressEqualsHashCodeTest.
*/
public static class AddressEqualsHashCodeTest extends EqualsHashCodeTestCase { @Override
protected Address createInstance() throws Exception {
return new Address.Builder().address1("2802 South Havana Street").city("Aurora").state("CO").zip("80014").build();
} @Override
protected Address createNotEqualInstance() throws Exception {
return new Address.Builder().address1("9839 Carlisle Boulevard NE").city("Albuquerque").state("NM").zip("87110").build();
}
} /**
* The Class AddressSerializabilityTest.
*/
public static class AddressSerializabilityTest extends SerializabilityTestCase { @Override
protected Serializable createInstance() throws Exception {
return new Address.Builder().address1("9839 Carlisle Boulevard NE").city("Albuquerque").state("NM").zip("87110").build();
}
} public static class AddressMiscTest { private Address address; /**
* Setup.
*
* @throws Exception the exception
*/
@Before
public void setUp() throws Exception {
address = new Address.Builder().address1("9839 Carlisle Boulevard NE").city("Albuquerque").state("NM").zip("87110").build();
} /**
* Test builder.
*/
@Test
public void testBuilder() {
assertThat(address.getAddress1(), is("9839 Carlisle Boulevard NE"));
assertThat(address.getCity(), is("Albuquerque"));
assertThat(address.getState(), is("NM"));
assertThat(address.getZip(), is("87110"));
} @Test
public void testToString() {
assertThat(address.toString(), is("Address{9839 Carlisle Boulevard NE, Albuquerque, NM, 87110}"));
}
}
}
View AddressTest Code
问题三:不想执行某个类的test case 有什么方法?
回答: 用@Ignore, 如果要让某个类都不执行,@Ignore放在类里,如果不想执行某一个方法,只需要放在方法上。
@Ignore
public class TestClass{ @Ignore("Test is ignored as a demonstration")
@Test
public void testSane() {
assertThat(1, is(1));
}
}
问题四:某个test case执行时间太长,有什么办法终止?
回答: Junit4提供了timeout属性。
@Test(timeout=1000)
public void testWithTimeout() {
...
}
Junit4还有更重要的@Rule 和 执行顺序。且听下回分解。
草原战狼淘宝小店:http://xarxf.taobao.com/ 淘宝搜小矮人鞋坊,主营精致美丽时尚女鞋,为您的白雪公主挑一双哦。谢谢各位博友的支持。
===============================================================================
========================== 以上分析仅代表个人观点,欢迎指正与交流 ==========================
========================== 草原战狼博客,转载请注明出处,万分感谢 ==========================
===============================================================================
【原创】Junit4详解一:Junit总体介绍的更多相关文章
- 【原创】Junit4详解二:Junit4 Runner以及test case执行顺序和源代码理解
概要: 前一篇文章我们总体介绍了Junit4的用法以及一些简单的测试.之前我有个疑惑,Junit4怎么把一个test case跑起来的,在test case之前和之后我们能做些什么? Junit4执行 ...
- Java基础学习总结(24)——Java单元测试之JUnit4详解
Java单元测试之JUnit4详解 与JUnit3不同,JUnit4通过注解的方式来识别测试方法.目前支持的主要注解有: @BeforeClass 全局只会执行一次,而且是第一个运行 @Before ...
- C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)
上一讲<C++11 并发指南四(<future> 详解一 std::promise 介绍)>主要介绍了 <future> 头文件中的 std::promise 类, ...
- Solr系列三:solr索引详解(Schema介绍、字段定义详解、Schema API 介绍)
一.Schema介绍 1. Schema 是什么? Schema:模式,是集合/内核中字段的定义,让solr知道集合/内核包含哪些字段.字段的数据类型.字段该索引存储. 2. Schema 的定义方式 ...
- [原创]mybatis详解说明
mybatis详解 2017-01-05MyBatis之代理开发模式1 mybatis-Dao的代理开发模式 Dao:数据访问对象 原来:定义dao接口,在定义dao的实现类 dao的代理开发模式 只 ...
- C++11 并发指南四(<future> 详解一 std::promise 介绍)
前面两讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread 和 std::m ...
- C++11 并发指南四(<future> 详解一 std::promise 介绍)(转)
前面两讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread 和 std::m ...
- 2020你还不会Java8新特性?方法引用详解及Stream 流介绍和操作方式详解(三)
方法引用详解 方法引用: method reference 方法引用实际上是Lambda表达式的一种语法糖 我们可以将方法引用看作是一个「函数指针」,function pointer 方法引用共分为4 ...
- junit4 详解
转:http://www.cnblogs.com/eggbucket/archive/2012/02/02/2335697.html JUnit4概述 JUnit4是JUnit框架有史以来的最大改进, ...
随机推荐
- ListView嵌套出现的问题
项目中一个列表子项中也需要用到列表,这就不由得使我想到ListView的嵌套,其实这个东西想想也只是复杂了一点,并没有什么难的地方,可是却依然在这里狠狠滴栽个跟头.问题出在子列表动态展开的操作上.可能 ...
- Cocos2d-x 基础元素
看过本章,然后实践之后,应该会掌握以下的认识: 1.Cocos2d-x引擎的基本运转过程 2.Cocos2d-x引擎的一些初始设置 3.对导演及图层及现实对象的认识 4.如何定义自己的显示对象 *:f ...
- java获取时间戳的方法
JAVA 获取当前月的初始时间的时间戳 public static long getMonthFirstDay() { Calendar calendar = Calendar.getInstance ...
- 读书笔记——Windows核心编程(2)比较字符串
1. CompareString 以符合用户语言习惯的方式,EX版本使用UNICODE int CompareString( __in LCID Locale, __in DWORD dwCmpFla ...
- Android开发之 adb 启动问题或是部署应用不成功,出现“The connection to adb is down, and a severe error has occured.”错误
首先是今天想测试下应用,没有问题的话就进行下一步的操作来着,结果遇到这个问题, The connection to adb is down, and a severe error has occure ...
- LVS四种实现模式详解
一.集群cluster 当后端服务器承受不住访问的压力,提高服务器性能的解决方案会极大增加成本时,人们提出了横向扩展的解决方案.增加一台或几台服务器,提供相同的服务,通过前段分发器将访问量均匀的分配到 ...
- Python Memcached Script
介绍 利用 python 书写了 memcached 的启动等一类操作 尽量的实现脚本的复用性,以及脚本的可扩展性,已达到一劳永逸的效果, 并且添加了 memcached 监控搭建 memcached ...
- 数据结构--树状数组(黑龙江省第八届大学生程序设计竞赛--post office)
例题来源: 题目: 1468: Post office 题目描述 There are N(N<=1000) villages along a straight road, numbered fr ...
- python 小程序练习
还有一些小bug 基本有 输入用户名密码 认证成功后显示欢迎信息 输出三次后锁定 # -*- coding:utf-8 -*- account_file=('C:\Users\guigu\Deskto ...
- Mac OS X 中vim开启配色和git补全
问题起源:mac 下自带的git 无法补全命令,配色方案不完善 Mac OS Yosemite 10.10.3 git version 2.3.2 (Apple Git-55) 处理结果:git命令可 ...