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进行单元测试的更多相关文章

  1. java如何使用JUnit进行单元测试

    注:所有内容都是在eclipse上实现,关于eclipse的安装和jdk的安装配置,请看:http://www.cnblogs.com/fench/p/5914827.html 单元测试是什么? 百度 ...

  2. JUnit 4 单元测试

    Individual Project ——JUnit 4 单元测试 学习到JUnit单元测试,我拿来测试之前写过的一个计算器(两个依存类:Calc.java CalcFunction.java).代码 ...

  3. Android之如何使用JUnit进行单元测试

    转的:http://www.blogjava.net/qileilove/archive/2014/05/19/413824.html Android中如何使用JUnit进行单元测试 在我们日常开发a ...

  4. 使用Spring配合Junit进行单元测试的总结

    最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法: 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注 ...

  5. Spring(3)—— Junit框架单元测试

    Junit主要用于单元测试,即白盒测试.它是一个开源的由JAVA开发的一个用于测试的框架. Junit的几个基本概念:TestCase,TestSuite,TestFixtrue TestCase:代 ...

  6. JUnit + Mockito 单元测试(二)

    摘自: http://blog.csdn.net/zhangxin09/article/details/42422643 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 入门 ...

  7. spring junit 做单元测试,报 Failed to load ApplicationContext 错误

    spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locatio ...

  8. 使用 JUnit 进行单元测试 - 教程

    tanyuanji@126.com 版本历史 JUnit 该教程主要讲解 JUnit 4.x 版本的使用,以及如何在Eclipse IDE 中如何使用JUnit   目录 tanyuanji@126. ...

  9. JUnit + Mockito 单元测试(二)(good)

    import org.junit.Test; import org.mockito.Matchers; import org.mockito.Mockito; import java.util.Lis ...

  10. 使用Junit进行单元测试

    使用Junit进行单元测试 一.目的和要求 JUnit是一款由Erich Gamma(<设计模式>的作者)和Kent Beck(极限编程的提出者)编写的开源的回归测试框架,供Java编码人 ...

随机推荐

  1. python里实现DSL

    以后用到的话可以参考如下链接: http://safehammad.com/downloads/domain-specific-languages-and-python-2011-04-21.pdf ...

  2. 修改cmd窗口mysql的提示符:

    mysql提示符: \D  完整的日期 \d   当前数据库 \h    服务器地址 \u    当前用户 登录数据库的时候设置提示符:  mysql  -uroot  -proot  --promp ...

  3. SGU 110. Dungeon 计算几何 难度:3

    110. Dungeon time limit per test: 0.25 sec. memory limit per test: 4096 KB The mission of space expl ...

  4. MS14-021: Internet Explorer 安全更新: 2014 年 5 月 1 日

    微软推送全平台IE零日漏洞补丁 2014.05.2 3 Comments 207 views今天,微软正式发布IE安全补丁,修复4月底曝光的零日漏洞.用户可通过Windows Update自动更新服务 ...

  5. 免费申请一年版Eset NOD32 Antivirus激活码(无限制)- 已失效

    You will receive a 1-year license of the antivirus software Eset NOD32 (version 6), including all up ...

  6. 高强度减脂Tabata练习

    每个动作20秒 动作间休息10秒 8个动作为一组 每次做四组 让你大汗淋漓全身酸爽 波比跳 ▼ 跳起箭步蹲 ▼ 登山者 ▼ 俯卧撑 ▼ 卷腹 ▼ 开合跳 ▼ 高抬腿 ▼ 俄罗斯转体 ▼ Bingo ▼ ...

  7. Vue.js更改调试地址端口号

    Vue项目一般使用自带的脚手架工具vue-cli和webpack打包方式进行项目构建运行.开发中,我们在控制台输入命令行npm run dev部署项目后,默认开启的页面调试地址是8080端口.但是有时 ...

  8. linux下忘记mysql密码的几种找回方法

    今天我们主要是讲一下关于linux忘记mysql密码处理方法,下面提供了5种linux忘记mysql密码找回方法哦.方法一(先进入root权限):# /etc/init.d/mysql stop# m ...

  9. C# 跨线程更新UI界面的适当的处理方式,友好的退出界面的机制探索

    本文主要讲讲C#窗体的程序中一个经常遇到的情况,就是在退出窗体的时候的,发生了退出的异常. 工业软件技术交流群:群1:592132877(满)  群2:948305931     欢迎技术探讨 我们先 ...

  10. HOG+SVM+INRIAPerson数据集代码

    #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgu ...