junit(六)
2016-01-24 23:10
447 查看
package com.ygl;
public class Calculator {
public int add(int a,int b){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a+b;
}
public int divide(int a,int b) throws Exception{
try{
return a/b;
}catch(Exception e){
throw new Exception("除数不能为零");
}
}
}
//***********************************************
package com.ygl;
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 CalculatorTest {
private Calculator cal;
@BeforeClass
public static void globalInit(){
System.out.println("globalInit inoked");
}
@AfterClass
public static void globalDestory(){
System.out.println("globalDestory inoked");
}
@Before
public void init(){
cal=new Calculator();
System.out.println("before");
}
@After
public void destroy(){
System.out.println("destroy");
}
@Test(timeout=600)
public void test() {
Calculator calculator=new Calculator();
int result=calculator.add(3, 5);
assertEquals(8, result);
}
@Test(expected=Exception.class)
public void testDivide() throws Exception{
cal.divide(1, 0);
}
}
//**************************************************
package com.ygl;
public class Calculator {
public int add(int a,int b){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return a+b;
}
public int divide(int a,int b) throws Exception{
try{
return a/b;
}catch(Exception e){
throw new Exception("除数不能为零");
}
}
}
//***********************************************
package com.ygl;
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 CalculatorTest {
private Calculator cal;
@BeforeClass
public static void globalInit(){
System.out.println("globalInit inoked");
}
@AfterClass
public static void globalDestory(){
System.out.println("globalDestory inoked");
}
@Before
public void init(){
cal=new Calculator();
System.out.println("before");
}
@After
public void destroy(){
System.out.println("destroy");
}
@Test(timeout=600)
public void test() {
Calculator calculator=new Calculator();
int result=calculator.add(3, 5);
assertEquals(8, result);
}
@Test(expected=Exception.class)
public void testDivide() throws Exception{
cal.divide(1, 0);
}
}
//**************************************************
相关文章推荐
- HDU 1050 Moving Tables
- 第二天
- select多路复用
- 我要成为优秀的前端一员!
- Java 并发基础
- Java 并发基础
- Java 并发基础
- Android 加载图片优化(一)
- OC基础之一-______HelloWorld
- [转]Unity中使用ulua的个人经验总结
- Codeforces Round #340 (Div. 2) A. Elephant 水题
- 重温C语言小感
- CSS块级元素、内联元素概念
- 在django1.7.5中访问静态文件(js css)
- Python 各种测试框架简介(二):unittest
- 模板的实例化和具体化
- xilinx FPGA 配置原理
- StrictMode 严苛模式 .
- 一个自用的xeLatex中英文模板
- 1.2.3 Name That Number