Junit进行单元测试
Junit提供 单元测试,多组参数的单元测试,打包单元测试。
比如你写了一个Calculator类:
package test_junit;
public class Calculator {
private static int result; // 静态变量,用于存储运行结果
public void add(int n) {
result = result + n;
}
public void substract(int n) {
result = result - 1; // Bug: 正确的应该是 result =result-n
}
public void multiply(int n) {
} // 此方法尚未写好
public void divide(int n) {
result = result / n;
}
public void square(int n) {
result = n * n;
}
public void squareRoot(int n) {
for (;;)
; // Bug : 死循环
}
public void clear() { // 将结果清零
result = 0;
}
public int getResult() {
return result;
}
}
写完了想测试下:
选择想测试的类右键new,junit,选择想测试的方法
生成一个test类,把这个测试类补充完整:
package test_junit; import static org.junit.Assert.*; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; public class CalculatorTest { private static Calculator calculator = new Calculator(); @AfterClass
public static void tearDownAfterClass() throws Exception {
} @Before
public void setUp() throws Exception {
} @After
public void tearDown() throws Exception {
} @Test
public void testAdd() {
calculator.add(2);
calculator.add(3);
assertEquals(5, calculator.getResult());
// fail("Not yet implemented");
} @Ignore
public void testSubstract() {
calculator.clear();
calculator.add(10);
calculator.substract(2);
assertEquals(8, calculator.getResult());
} @Test
public void testMultiply() {
// fail("Not yet implemented");
} @Test
public void testDivide() {
calculator.clear();
calculator.add(8);
calculator.divide(2);
assertEquals(4, calculator.getResult());
} @Test(timeout = 12)
public void testSquareRoot() { calculator.squareRoot(8); // assertEquals(4,calculator.getResult());
} //@Test(expected = ArithmeticException.class)
@Test
public void divideByZero() {
calculator.divide(0); }
}
运行下这个类,就可以看到可视化对错的效果。
如果这个类的某个方法有多组测试数据,那么可以这么写。
package test_junit; import static org.junit.Assert.*; import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import java.util.Arrays; import java.util.Collection; @RunWith(Parameterized.class)
public class CalculatorTest2 { private static Calculator calculator = new Calculator(); private int param; private int result; @Parameters
public static Collection data() { return Arrays.asList(new Object[][] { { 2, 4 }, { 0, 0 }, { -3, 9 }, }); } // 构造函数,对变量进行初始化 public CalculatorTest2(int param, int result) { this.param = param; this.result = result; } @Test
public void square() { calculator.square(param); assertEquals(result, calculator.getResult()); } }
这时,你可能已经写了十来个测试类了,但是一个一个是比较麻烦的,这时多重选择想要的那几个测试类,右键new ->junit -> Junit Test Suits就可以打包了。
package test_junit; import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class)
@SuiteClasses({ CalculatorTest.class, CalculatorTest2.class })
public class AllTests { }
直接运行这个类就可以一起测试了。
参考:http://blog.csdn.net/andycpp/article/details/1329218
Junit进行单元测试的更多相关文章
- java如何使用JUnit进行单元测试
注:所有内容都是在eclipse上实现,关于eclipse的安装和jdk的安装配置,请看:http://www.cnblogs.com/fench/p/5914827.html 单元测试是什么? 百度 ...
- JUnit 4 单元测试
Individual Project ——JUnit 4 单元测试 学习到JUnit单元测试,我拿来测试之前写过的一个计算器(两个依存类:Calc.java CalcFunction.java).代码 ...
- Android之如何使用JUnit进行单元测试
转的:http://www.blogjava.net/qileilove/archive/2014/05/19/413824.html Android中如何使用JUnit进行单元测试 在我们日常开发a ...
- 使用Spring配合Junit进行单元测试的总结
最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法: 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注 ...
- Spring(3)—— Junit框架单元测试
Junit主要用于单元测试,即白盒测试.它是一个开源的由JAVA开发的一个用于测试的框架. Junit的几个基本概念:TestCase,TestSuite,TestFixtrue TestCase:代 ...
- JUnit + Mockito 单元测试(二)
摘自: http://blog.csdn.net/zhangxin09/article/details/42422643 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 入门 ...
- spring junit 做单元测试,报 Failed to load ApplicationContext 错误
spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locatio ...
- 使用 JUnit 进行单元测试 - 教程
tanyuanji@126.com 版本历史 JUnit 该教程主要讲解 JUnit 4.x 版本的使用,以及如何在Eclipse IDE 中如何使用JUnit 目录 tanyuanji@126. ...
- JUnit + Mockito 单元测试(二)(good)
import org.junit.Test; import org.mockito.Matchers; import org.mockito.Mockito; import java.util.Lis ...
- 使用Junit进行单元测试
使用Junit进行单元测试 一.目的和要求 JUnit是一款由Erich Gamma(<设计模式>的作者)和Kent Beck(极限编程的提出者)编写的开源的回归测试框架,供Java编码人 ...
随机推荐
- G1收集器
转载:https://blog.csdn.net/zhou2s_101216/article/details/79202893 http://blog.jobbole.com/109170/ http ...
- 自定义实现spark的分区函数
有时自己的业务需要自己实现spark的分区函数 以下代码是实现一个自定义spark分区的demo 实现的功能是根据key值的最后一位数字,写到不同的文件 例如: 10写入到part-00000 11写 ...
- spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid
SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...
- linux shard virtual memory
- Using XmlHttpRequest 写JSON网页
代码如下-----xmlhttprequest.responseJSON: <!DOCTYPE html> <html> <head> <meta chars ...
- [转载]彻底卸载oracleXE数据库服务器
URL:http://www.2cto.com/database/201306/216182.html
- selenium(二)查找定位目标 ID XPATH CSS 操作目标
简介: 我们只打开一个页面是没有什么意义的. 尽管你可以get很多次. selenium可以做的更多,比如找到百度的搜索框,输入我们要搜索的内容,再用鼠标点击搜索按钮,再把搜索结果提取出来…… 这篇文 ...
- html网页设计
对于html文档可以直接通过浏览器打开并解释执行,不需要使用服务器.一个html文档的架构,一般由3对标签构成:<html></html>,<head></h ...
- Free 4 months Serial License Key Of Outpost Security Suite Pro 8.1
VISIT HEREto gey a 1 year key.... for the latest version Promo code ? well use the same here too : C ...
- cocos2d-x 2.0.2升级后某些函数变化(转)
最近看cocos2d-x 2.0.2发布后升级了一下,升级后发现又出现了很多错误,原来有一些地方的代码用法改变了.在修改代码的过程中,简单做了一些记录,当做是一个备忘录. 1.CCScene和CCLa ...