Tutorial 1
This tutorial introduces the basic annotation supports that implemented in Junit 4.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import
org.junit.*
;
import
static
org.junit
.Assert
.*;
import
java.util.*
;
/**
* @author mkyong
*
*/
public
class
JunitTest1 {
private
Collection
collection;
@BeforeClass
public
static
void
oneTimeSetUp(
)
{
// one-time initialization code
System
.out
.println
(
"@BeforeClass - oneTimeSetUp"
)
;
}
@AfterClass
public
static
void
oneTimeTearDown(
)
{
// one-time cleanup code
System
.out
.println
(
"@AfterClass - oneTimeTearDown"
)
;
}
@Before
public
void
setUp(
)
{
collection =
new
ArrayList
(
)
;
System
.out
.println
(
"@Before - setUp"
)
;
}
@After
public
void
tearDown(
)
{
collection.clear
(
)
;
System
.out
.println
(
"@After - tearDown"
)
;
}
@Test
public
void
testEmptyCollection(
)
{
assertTrue(
collection.isEmpty
(
)
)
;
System
.out
.println
(
"@Test - testEmptyCollection"
)
;
}
@Test
public
void
testOneItemCollection(
)
{
collection.add
(
"itemA"
)
;
assertEquals(
1
, collection.size
(
)
)
;
System
.out
.println
(
"@Test - testOneItemCollection"
)
;
}
}
|
Result
@BeforeClass – oneTimeSetUp
@Before – setUp
@Test – testEmptyCollection
@After – tearDown
@Before – setUp
@Test – testOneItemCollection
@After – tearDown
@AfterClass – oneTimeTearDown
In JUnit 4, we have to declare “@BeforeClass” and “@AfterClass” method as static method.
Tutorial 2
The “exception testing” means what exception throw from the unit test.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import
org.junit.*
;
/**
* JUnit Expected Exception Test
* @author mkyong
*
*/
public
class
JunitTest2 {
@Test(
expected =
ArithmeticException
.class
)
public
void
divisionWithException(
)
{
int
i =
1
/
0
;
}
}
|
Result
Unit test marked success
Tutorial 3
The “Ignored” means whether it should ignore the unit test.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import
org.junit.*
;
/**
* JUnit Ignore Test
* @author mkyong
*
*/
public
class
JunitTest3 {
@Ignore(
"Not Ready to Run"
)
@Test
public
void
divisionWithException(
)
{
System
.out
.println
(
"Method is not ready yet"
)
;
}
}
|
Result
unit test ignored
Tutorial 4
The “Time Test” means if an unit test takes longer than the
specified number of milliseconds to run, the test will terminated and
mark as fails.
import org.junit.*;
/**
* JUnit TimeOut Test
* @author mkyong
*
*/
public class JunitTest4 {
@Test(timeout = 1000)
public void infinity() {
while (true);
}
}
Result
java.lang.Exception: test timed out after 1000 milliseconds
Tutorial 5
The “Suite Test” means bundle a few unit test and run it together.
The “@RunWith” and “@Suite” are use to run the suite test. The below
class means both unit test “JunitTest1” and “JunitTest2” run together
after JunitTest5 executed. All the declaration is define inside the
class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import
org.junit.runner.RunWith
;
import
org.junit.runners.Suite
;
/**
* JUnit Suite Test
* @author mkyong
*
*/
@RunWith(
Suite.class
)
@Suite.SuiteClasses
(
{
JunitTest1.class
,
JunitTest2.class
}
)
public
class
JunitTest5 {
}
|
Result
@BeforeClass – oneTimeSetUp
@Before – setUp
@Test – testEmptyCollection
@After – tearDown
@Before – setUp
@Test – testOneItemCollection
@After – tearDown
@AfterClass – oneTimeTearDown
P.S Result is from JunitTest1 and JunitTest2 unit test
Tutorial6
The “Parameterized Test” means vary parameter value for unit test.
The “@RunWith” and “@Parameter” is use to provide parameter value for
unit test, @Parameters have to return List[], and the parameter will
pass into class constructor as argument.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import
java.util.Arrays
;
import
java.util.Collection
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runners.Parameterized
;
import
org.junit.runners.Parameterized.Parameters
;
/**
* JUnit Parameterized Test
* @author mkyong
*
*/
@RunWith(
value =
Parameterized.class
)
public
class
JunitTest6 {
private
int
number;
public
JunitTest6(
int
number)
{
this
.number
=
number;
}
@Parameters
public
static
Collection<
Object[
]
>
data(
)
{
Object
[
]
[
]
data =
new
Object
[
]
[
]
{
{
1
}
, {
2
}
, {
3
}
, {
4
}
}
;
return
Arrays
.asList
(
data)
;
}
@Test
public
void
pushTest(
)
{
System
.out
.println
(
"Parameterized Number is : "
+
number)
;
}
}
|
Result
Parameterized Number is : 1
Parameterized Number is : 2
Parameterized Number is : 3
Parameterized Number is : 4
It has many limitations here; we have to follow the “JUnit” way to
declare the parameter, and the parameter has to pass into constructor
in order to initialize the class member as parameter value for testing.
The return type of parameter class is “List []”, data has been limited
to String or a primitive value for testing.
分享到:
相关推荐
本案例程序功能是:输入一个有效的日期,输出下一天的日期。比如,输入2013.12.31,输出为2014.1.1。
JUNIT 实例教程 是帮助你怎样使用JUNIT的文档,对于初学者来说是个不错的助手
Junit实例图解,描述Junit安装环境,实用例子
maven+spring+mybatis+junit实例
junit是java进行单元测试最成熟的一个框架,这个junittest工程里以实例的方式,介绍了怎样编写测试方法。包含三个测试方法,Math函数的测试,堆栈Stack的测试,用户User类的测试。导入后如果没有引入junit库,记得在...
junit实例demo
JUnit4的实例代码 JUnit4是新的test模式,欢迎大家一起来学习
eclipse下的一个小实例,可运行,简单明了,不需配置其他包,学习使用junit进行单元测试的快速上手实例
初次写testsuite会遇到“NO runnable methods”,添加@Test也没有解决问题,看了多个文档实例,终于解决了这个问题,愿意跟大家一起分享,添加了资源分2分,迫于我去下载资源跟我要分啊。java源代码。
主要介绍了SpringBoot整合Junit实例过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
使用junit中例子, junit demo, junit 教程
JAVA文档\java的反射技术JAVA文档\java的反射技术功能十分强大.doc功能十分强大.docJAVA文档\java的反射技术功能十分强大.docJAVA文档\java的反射技术功能十分强大.doc
ant+junit程序小实例(绝对可以运行)
Android JUnit单元测试基本实例
单元测试利器 JUnit 4 完整实例图解 做测试的必看的文档 希望对您有帮助
eclipse中junit测试实例,有文档和源码
上下文时间软件测试中Junit单元测试实例在一种传统的结构化编程语言中,比如C,要进行测试的单元一般是函数或子过程。在象C++这样的面向对象的语言中,要进行测试的基本单元是类。对Ada语言来说,开发人员可以选择是...
JUnit eclipse工程实例,使用junit测试你的程序!
JUnit教程工程代码实例+PDF下载