在eclipse之中使用Junit
使用Junit单位测试,能够一定程度上减小项目bug的产生。很多时候,我们都是在main函数里面单独去测试一个方法,这样的测试非常不方便。使用Junit可以是测试单位和项目代码分离,一次性测试多个方法,并对结果进行评估。
在eclipse中进行单位测试,过程如下 右键项目-》build path》add Library 》添加Junit类库。然后右键项目,添加一个source folder,命名为test。
下面的代码来至于http://www.imooc.com/learn/356。
首先新建一个类。
package com.dong.util;
public class Calculate {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int mutiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
return a / b;
}
}
然后,我们可以右键该类生成一个Junit test case。这样便会生成一个测试类。
package com.dong.util;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculateTest {
// @Test
// public void test() {
// fail("Not yet implemented");
// }
@Test
public void testAdd() {
assertEquals(6, new Calculate().add(3, 3));
}
@Test
public void testSubtract() {
assertEquals(0, new Calculate().subtract(3, 3));
}
@Test
public void testMutiply() {
assertEquals(9, new Calculate().mutiply(3, 3));
}
@Test
public void testDivide() {
assertEquals(1, new Calculate().divide(3, 3));
}
}
测试类被和被测试类其实处于同一个包名下面,只是处于不同的source folder下。上述测试样例是最简单的例子,只是调用了assertEqual方法,更多的测试方法可以见文档。
下面是测试类的一些要求。
- 1测试方法必须使用@Test进行修饰
- 2测试方法必须使用public void方法,不能带参数
- 3新建一个源代码目录来存放测试代码
- 4测试类的包应该和被测试类一致
- 5测试单位的每个方法必须可以独立测试,测试方法不能有依赖
- 6测试类使用Test作为类名后最(不是必须)
- 7测试方法使用test作为前缀(不是必须)
下面这个类演示了junit测试时候调用每个方法的次序。
package com.dong.util; import static org.junit.Assert.assertEquals; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; public class JunitFlowTest { /**
* 所有方法被调用前执行。由于是静态的,适合只加载一次的内容。 例如配置文件
*
* @throws Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception { System.out.println("@BeforeClass");
} /**
* 资源清理
*
* @throws Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("@AfterClass");
} /**
* 方法执行前
*
* @throws Exception
*/
@Before
public void setUp() throws Exception {
System.out.println("@@Before"); } /**
* 方法执行后
*
* @throws Exception
*/
@After
public void tearDown() throws Exception {
System.out.println("@@After");
} @Test
public void testAdd() {
System.out.println("testAdd");
assertEquals(5, new Calculate().add(3, 3));
} @Test
public void testMutiply() {
assertEquals(9, new Calculate().mutiply(3, 3));
}
}
@BeforeClass表示的是测试类测试所用方法前进行的操作。
@AfterClass表示的是测试类测试所有方法后执行的操作。
@Before表示的是执行每个测试方法前进行的操作。
@After表示的是执行每个测试方法后所执行的操作。
下面这个类演示了一些常用的测试注解所使用的方式。
package com.dong.util; import static org.junit.Assert.assertEquals; import org.junit.Ignore;
import org.junit.Test; /**
* junit注解
*
* @author 95
*
*/
public class AnotationTest { /**
* @Test把一个普通方法设置为测试方法
* @Test(expected = ArithmeticException.class) 表示预期会抛出一个异常
* @Test(timeout=毫秒) 测试死循环和性能测试
* @BeforeClass 所有方法执行前执行
* @AfterClass 所有方法执行后执行
* @Before 每个方法执行前执行
* @After 每个方法执行后执行
* @Ignore 忽略
* @RunWith可以更改测试运行器 org.junit.runner.Runner
*/ /**
* @Test(expected = ArithmeticException.class) 表示预期会抛出一个异常
*/
@Test
public void testDivide() {
assertEquals(1, new Calculate().divide(3, 0));
} /**
* @Test(timeout=毫秒) 测试死循环和性能测试
*/
@Ignore
@Test(timeout = 1000)
public void testWhile() {
while (true) {
assertEquals(1, new Calculate().divide(3, 3));
}
} @Test(timeout = 2000)
public void testReadFile() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
在一些情况下,可能需要同时测试多个测试用例,如果每次都写assert则显得很麻烦。比如我们想测试加法时候,有这样2个样例 1+2?=3 ,2+2?=4。可以使用如下代码进行实现。
package com.dong.util; import static org.junit.Assert.assertEquals; 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; @RunWith(Parameterized.class)
public class ParameterTest {
/**
* 1更改默认的测试运行期为@RunWith(Parameterized.class) 2声明变量来存放预期值和结果值
* 3声明一个返回值为Collection的公共静态方法,并使用@Parameters修饰
* 4为测试类声明一个带有参数的公共构造函数,并在其中为之声明变量赋值
*/
int expected = 0;
int input1 = 0;
int input2 = 0; @Parameters
public static Collection<Object[]> t() {
return Arrays.asList(new Object[][] { { 3, 1, 2 }, { 4, 2, 2 } }); } public ParameterTest(int expected, int input1, int input2) {
super();
this.expected = expected;
this.input1 = input1;
this.input2 = input2;
} @Test
public void testAdd() {
assertEquals(expected, new Calculate().add(input1, input2)); } }
当我们一次想运行制定的几个测试类的时候,我们可以使用suite。使用方法如下。
/**
* 测试套件
*
* @author 95
*
*/
@RunWith(Suite.class) // 测试套件入口类,一次运行多个类
@Suite.SuiteClasses({ TastTest.class, TastTest1.class, TastTest4.class })
public class SuiteTest {
/**
* 1、 测试套件就是组织测试类一起运行的
*
* 写一个作为测试套件的入口类,这个类不包含其他的方法 更改测试类运行期Suite.class
* 将要测试的类作为数组传入到Suite.SuiteClass({})
*/ }
在eclipse之中使用Junit的更多相关文章
- Eclipse中使用Junit编写测试用例
Eclipse自带Junit插件,不用安装就能在项目中编写测试用例,非常方便. 在项目中添加Junit库 在编写测试用例之前,需要先引入Junit.对项目根目录右键,选择Properties,Java ...
- 在Eclipse之中调试FastDFS-storage
FDFS版本为5.03 1.首先在eclipse之中创建一个C/C++工程,取名为FastDFS_v5.03 2.将FastDFS源码解压后拷贝到新创建的工程目录下,然后在ecipse之中刷新下工程就 ...
- Android studio及eclipse中的junit单元測试
转载请标明出处:http://blog.csdn.net/nmyangmo/article/details/51179106 前一段时间有人问我单元測试的相关内容,我稍作总结做日志例如以下: 由于我接 ...
- 【Java】eclipse中的JUnit单元测试
eclipse中的JUnit单元测试 步骤: 选中当前工程 - 右键选择:build path - add libraries - JUnit 4 - 下一步 创建Java类,进行单元测试. 此时的J ...
- 在Eclipse中使用Junit进行单元测试
单元测试与Junit4 单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证.单元测试是在软件开发过程中要进行的最低级别的测试活动,软件的独立单元将在与程序的其他部分相隔离 ...
- Eclipse中配置Junit
在要使用Junit的project名上,点击properties-->Java Build Path-->Libraries,点击Add Library 选择Junit 选择Junit 4 ...
- 在Eclipse中使用Junit进行单元测试练习 实现最大子数组和算法
1.如何在MAC OS X下安装配置java开发工具 http://www.cnblogs.com/coderL/p/5939541.html 2.最大子数组和算法 附上程序运行及测试截图,源码见后 ...
- 在Eclipse中生成接口的JUnit测试类
在Spring相关应用中,我们经常使用“接口” + “实现类” 的形式,为了方便,使用Eclipse自动生成Junit测试类. 1. 类名-new-Other-java-Junit-Junit Tes ...
- windows下eclipse跑junit报错:CreateProcess error=206
from:http://isuifengfei.iteye.com/blog/1684262 windows下,eclipse中运行junit出现错误提示: Exception occurred ex ...
随机推荐
- java中的抽象类和接口
抽象类和接口本身让面向对象真正实现,一个好的系统可以让抽象类或者接口实现多次复用,如果出现了集成具体类那么肯定是有问题的. 抽象类和接口很相似,很多时候好像功能可以混用,java设计者赋予了很多不一样 ...
- dashboard
http://www.htmleaf.com/pins/chart-doc/index.html http://www.flotcharts.org/flot/examples/ https://gi ...
- 国外程序员整理的 C++ 资源大全(转)
原文:http://www.csdn.net/article/2014-10-24/2822269-c 关于 C++ 框架.库和资源的一些汇总列表,由 fffaraz发起和维护. 内容包括:标准库.W ...
- Extjs各版本的下载链接
Extjs的版本繁多,本文收集了Extjs各个版本的下载链接,包括官网和非官网的,以及各种汉化版api,欢迎大家下载分享. Extjs最新版下载链接:http://www.sencha.com/pro ...
- Js操作DOM小练习_01
1.页面引入jQuery文件和bootstrap文件: 2.贴上代码 <!DOCTYPE html> <html lang="en"> <head&g ...
- 分布式任务分发框架Gearman教程和PHP实现实例
1.Gearman介绍和使用场景 Gearman是一个分发任务的程序框架,可以用在各种场合,与Hadoop相 比,Gearman更偏向于任务分发功能.它的任务分布非常简单,简单得可以只需要用脚本即可完 ...
- Linux命令(21)查看文件的行数
在 linux 系统中没有在 windows 系统中那么方便的点点鼠标就可以操作文件了,对文件的各种操作都必须使用各种命令来完成.比如有时候我们需要在不查看文件内容的情况下需要知道该文件有多少行.这个 ...
- [家里蹲大学数学杂志]第013期2010年西安偏微分方程暑期班试题---NSE,非线性椭圆,平均曲率流,非线性守恒律,拟微分算子
Navier-Stokes equations 1 Let $\omega$ be a domain in $\bbR^3$, complement of a compact set $\mathca ...
- NGUI之渲染DrawCall的合并
在Unity中,每次引擎准备数据并通知GPU的过程称为一次Draw Call.Draw Call值越低,会得到更好的渲染性能. (NGUI 查看DrawCall工具(NGUI-OPEN-Draw Ca ...
- (DP)3.Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...