您的位置:首页 > 其它

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);

}

}

//**************************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: