Junit笔记
1、 单元测试
package edu.hpu.junit;
public class T {
public int plus(int x , int y) {
return x + y;
}
}
//测试用
package edu.hpu.junit.test;
//静态引入,引入相应的方法,而不用再写类名
import static org.junit.Assert.*;
import org.junit.Test;
import edu.hpu.junit.T;
public class TTest {
@Test
public void testPlus() {
int z = new T().plus(3, 5);
assertEquals(8, z);
}
}
2、 Error与Failure
Error是程序出错,Failure是测试失败
3、 测试方法是否抛出相应的异常
@Test(expected=java.lang.ArithmeticException.class)
public void testDivide() {
int z = new T().divide(8, 0);
}
4、 测试程序的执行速度
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
int z = new T().divide(8, 0);
}
5、 @before与@after
package edu.hpu.junit.test;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import edu.hpu.junit.T;
public class TTest {
@Before
public void before() {
System.out.println("before");
}
@Test
public void testPlus() {
int z = new T().plus(3, 5);
assertEquals(8, z);
}
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
int z = new T().divide(8, 0);
}
@After
public void after() {
System.out.println("after");
}
}
运行结果:
before
after
before
after
表明每当测试一个方法前运行一下用@before标识的方法,结束时运行一下用@after标识的方法
6、 @BeforClass和@AfterClass
package edu.hpu.junit.test;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import edu.hpu.junit.T;
public class TTest {
@BeforeClass
public static void beforeClass() {
System.out.println("beforeClass");
}
@AfterClass
public static void afterClass() {
System.out.println("afterClass");
}
@Before
public void before() {
System.out.println("before");
}
@Test(expected=java.lang.ArithmeticException.class, timeout=100)
public void testDivide() {
int z = new T().divide(8, 0);
}
@After
public void after() {
System.out.println("after");
}
}
运行结果:
beforeClass
before
after
afterClass
表明当Class未初始化前执行BeforeClass标识的方法,程序运行完以后执行AfterClass方法
分享到:
相关推荐
Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验
很简单的说明,这是好的一个开始,希望大家能够看到,学习测试是很重要的。
Junit入门练习代码。
我以前写的一个junit入门的培训ppt,传了大家参考一下
JUnit入门,Junit入门学习,Junit初学者!
junit 入门 指南 eclipse 测试 工具 test testcase
Junit入门培训资料(断言相关),自己总结
Junit入门及应用,入门级别的学习资料。
JUnit入门简介,初学JUnit适用,深入浅出,实例为证,
JUnit入门指导,包括软件架构,内部机制,讲课文档等
JUnit入门[收集].pdf
学习时候从网络上整理的juint资料,非常基础.
junit入门资料,例子,ppt等
NULL 博文链接:https://rwl6813021.iteye.com/blog/511660
前言11第 1 章第 1 章33第 2 章第 2 章66第 3 章第 3 章11第 4 章第 4 章15第 5 章第 5 章19第 6 章第 6 章28第 7