摘要:本篇教程探讨了测试工具之Eclipse中Junit的使用,希望阅读本篇文章以后大家有所收获,帮助大家对相关内容的理解更加深入。
本篇教程探讨了测试工具之Eclipse中Junit的使用,希望阅读本篇文章以后大家有所收获,帮助大家对相关内容的理解更加深入。
<
以下以银行余额、存款、取款为例
一、Junit配置
Junit同Eclipse一同提供,无需下载。要使用Junit必须先将Junit jar 保存在项目的build路径上,并创建一个测试类,步骤如下:
1)法一:
法二:点击项目test001,右键-propreties
2)选择Libraries,点击Add variable按钮,输入变量名JUNIT_LIB,路径:E:\测试\测试工具\Juint\eclipse\eclipse-jee-kepler-SR2-win32\eclipse\plugins\org.junit_4.11.0.v201303080030,结果如下图:
二、编写测试类和代码、执行测试用例
1)新建测试类:New-Junit Test Case
2)编写测试代码:
1 package testsample;
2
3 import org.junit.After;
4 import org.junit.Before;
5 import junit.framework.TestCase;
6 public class Tc_Account extends TestCase {
7
8 public Tc_Account(String arg0)
9 {
10 super(arg0);
11 }
12 @Before
13 public void setUp() throws Exception {
14 super.setUp() ;
15 }
16 public void testDeposit(){
17 Account account=new Account();
18 assertEquals("Account should start with no funds.",1,account.balance());
19
20 account.deposit(5);
21 assertEquals("Account should reflect deposit.", 7, account.balance());
22 }
23
24 public void testwithdraw(){
25 Account account=new Account();
26 account.deposit(5);
27 account.withdraw(3);
28 assertEquals("Account should reflect withdarw.", 3, account.balance());
29 }
30
31 @After
32 public void tearDown() throws Exception {
33 super.tearDown();
34 }
35 }
3)新建Acoount类,实现银行的余额、存款、取款:
1 package testsample;
2
3 public class Account {
4 protected int balance;
5 public int balance(){
6 return balance;
7 }
8 public void deposit(int amount){
9 balance+=amount;
10 }
11 public void withdraw(int amount){
12 balance-=amount;
13 }
14 }
4)执行测试用例:右键项目testsample-Run as-Junit Test Case,通过failure trace可以查看错误信息
5)调整测试用例中的预期值:
1 package testsample;
2
3 import org.junit.After;
4 import org.junit.Before;
5 import junit.framework.TestCase;
6 public class Tc_Account extends TestCase {
7
8 public Tc_Account(String arg0)
9 {
10 super(arg0);
11 }
12 @Before
13 public void setUp() throws Exception {
14 super.setUp() ;
15 }
16 public void testDeposit(){
17 Account account=new Account();
18 assertEquals("Account should start with no funds.",0,account.balance());
19
20 account.deposit(5);
21 assertEquals("Account should reflect deposit.", 5, account.balance());
22 }
23
24 public void testwithdraw(){
25 Account account=new Account();
26 account.deposit(5);
27 account.withdraw(3);
28 assertEquals("Account should reflect withdarw.", 2, account.balance());
29 }
30
31 @After
32 public void tearDown() throws Exception {
33 super.tearDown();
34 }
35 }
6)执行测试用例,结果如下:所有测试通过
本文由职坐标整理发布,学习更多的相关知识,请关注职坐标IT知识库!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号