Skip to content

Commit 0241694

Browse files
committed
delete test and add my blog
1 parent 3a3e4e7 commit 0241694

File tree

2 files changed

+143
-32
lines changed

2 files changed

+143
-32
lines changed

_posts/2017-3-13-how-to-write-a-count-down.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

_posts/2017-3-14-JUnit4.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
layout : post
3+
title : JUnit4的使用 01
4+
date : 2017-3-14
5+
categoties : java
6+
tags : java JUnit4
7+
---
8+
9+
* content
10+
{:toc}
11+
12+
[学习来源:慕课网 JUnit—Java单元测试必备工具](http://www.imooc.com/learn/356)
13+
14+
15+
16+
17+
18+
## 简介
19+
20+
## JUnit4的使用
21+
22+
### 1. 准备
23+
---
24+
我使用的软件是Eclipse
25+
新建一个java项目**junit4Demo**,在src目录下新建两个**Source Folder**——main(用于存储自己编写的类方法)和test(用于存储测试用例),这样做的好处是可以在发布项目是直接删除测试目录test。
26+
27+
![](~/14-16-50.jpg)
28+
29+
在main目录下新建包**pre.huangjs.junit4**新建类**Calculate**,编写如下的代码
30+
```java
31+
package pre.huangjs.junit4;
32+
33+
public class Calculate{
34+
35+
public int add(int a, int b){
36+
return a + b;
37+
}
38+
39+
public int subtraction(int a, int b){
40+
return a - b;
41+
}
42+
43+
public int multiply(int a, int b){
44+
return a * b;
45+
}
46+
47+
public int divide(int a, int b){
48+
return a / b;
49+
}
50+
}
51+
```
52+
此时我的目录结构:
53+
54+
![](~/14-38-56.jpg)
55+
56+
### 2. 导入
57+
---
58+
59+
![](~/14-47-02.jpg)
60+
61+
![](~/14-49-55.jpg)
62+
63+
![](~/14-51-24.jpg)
64+
65+
![](~/14-51-53.jpg)
66+
67+
![](~/14-52-05.jpg)
68+
69+
### 3.建立
70+
---
71+
72+
![](~/14-59-11.jpg)
73+
74+
![](~/14-59-19.jpg)
75+
76+
![](~/14-59-29.jpg)
77+
78+
![](~/14-59-34.jpg)
79+
80+
![](~/14-59-39.jpg)
81+
82+
### 4. 使用
83+
`assertEquals(expected, actual)`
84+
85+
修改
86+
87+
---
88+
89+
```java:n
90+
package pre.huangjs.junit4;
91+
92+
import static org.junit.Assert.*;
93+
94+
import org.junit.Test;
95+
96+
public class CalculateTest {
97+
98+
@Test
99+
public void testAdd() {
100+
assertEquals(6, new Calculate().add(3, 3));
101+
}
102+
103+
@Test
104+
public void testSubtraction() {
105+
assertEquals(2, new Calculate().subtraction(5, 2));
106+
}
107+
108+
@Test
109+
public void testMultiply() {
110+
assertEquals(2, new Calculate().multiply(1, 2));
111+
}
112+
113+
@Test
114+
public void testDivide() {
115+
assertEquals(0, new Calculate().divide(3, 0));
116+
}
117+
118+
}
119+
```
120+
121+
### 5. 结果说明
122+
123+
![](~/15-14-28.jpg)
124+
125+
![](~/15-15-58.jpg)
126+
127+
![](~/15-13-57.jpg)
128+
129+
* 第一部分:
130+
- run指的是运行的测试方法数目
131+
- Error:
132+
- Failure
133+
JUnit 将测试失败的情况分为两种:failure 和 error。Failure 一般由单元测试使用的断言方法判断失败引起,它表示在测试点发现了问题;而 error 则是由代码异常引起,这是测试目的之外的发现,它可能产生于测试代码本身的错误(测试代码也是代码,同样无法保证完全没有缺陷),也可能是被测试代码中的一个隐藏的 bug。
134+
135+
水水
136+
![](~/15-16-46.jpg)
137+
138+
![](~/15-16-50.jpg)
139+
140+
141+
142+
> ##
143+
> ##

0 commit comments

Comments
 (0)