junit单元测试



需要引入库junit.jar
一待测试类

public class RunTime {
public boolean executeRunTime(int str) {
/*
* try { Process prop = Runtime.getRuntime().exec(str); // Process prop =
* Runtime.getRuntime().exec("cmd/E:ON/C start 1.txt"); } catch
* (IOException e) { e.printStackTrace(); }
*/
if (str == 1) {
return true;
} else {
return false;
}
}

public int reValue(int a, int b) {
return a + b;
}
}


二测试类


import actionFace.RunTime;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.Assert;
import junit.framework.TestSuite;

public class RunTimeTest extends TestCase {
public static RunTime op = null;

public RunTimeTest(String name) {
super(name);
}

protected void setUp() throws Exception {
super.setUp();
op = new RunTime();
}

protected void tearDown() throws Exception {
super.tearDown();
}

/*
* Test method for 'common.RunTimeTest.executeRunTime()'
*/
public void testExecuteRunTime() {
// junit.framework.TestResult r = new junit.framework.TestResult();
try {
Assert.assertEquals(false, op.executeRunTime(1));// 若失败则抛出AssertionFailedError异常
} catch (Exception e) {
System.out.println("sfsd");
Assert.fail("fsdf");
}
}

public void testreValue() {
Assert.assertEquals(" i am here!", 3, op.reValue(1, 2));
}

// 可在一个单独类中实现
public static Test suite() {
TestSuite suite = new TestSuite("ALL TEST");
/* 以下是测试某个具体方法 */
suite.addTest(new RunTimeTest("testreValue"));
suite.addTest(new RunTimeTest("testExecuteRunTime"));
/* 下句是执行指定类中的所有方法 */
// suite.addTestSuite(RunTimeTest.class);
return suite;
}

public static void main(String[] args) {
// 以下三种方式均可以,具体情况可运行以下,看一下结果
junit.textui.TestRunner.run(suite());
// junit.swingui.TestRunner.run(Test.class);
//junit.awtui.TestRunner.run(Test.class);
//junit.swingui.TestRunner.run(RunTimeTest.class);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值