软件测试技术实验一 JUnit 单元测试的环境搭建

实验一 JUnit 单元测试的环境搭建

一、实验目的

1、确安装并配置 IntelliJ IDEA开发环境

2、安装插件Junit4.x,进行测试环境搭建

3、根据给定的源代码,编写测试代码,并运行成功。

二、实验内容

1、调试指导书给定的目标源代码,除掉明显的语法错误,记录正确无误的被测程序代码。被测程序 :

(1) Account.Java

public class Account {
    protected int balance;
    public int balance(){
        return balance;
    }
    public void deposit(int amount){
        balance+=amount;
    }
    public void withdraw(int amount){
        balance-=amount;
    }
}

(2)Tc_Account.java

import org.junit.After;
import org.junit.Before;
import junit.framework.TestCase;
public class Tc_Account extends TestCase {
    public Tc_Account(String arg0)
    {
        super(arg0);
    }
    @Before
    public void setUp() throws Exception {
        super.setUp() ;
    }
    public void testDeposit(){
        Account account=new Account();
        assertEquals("Account should start with no funds.",0,account.balance());
        account.deposit(5);
        assertEquals("Account should reflect deposit.", 5, account.balance());
    }
    public void testwithdraw(){
        Account account=new Account();
        account.deposit(5);
        account.withdraw(3);
        assertEquals("Account should reflect withdarw.", 2, account.balance());
    }
    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }
}

(3)由插件Junit生成的测试代码

public class AccountTest {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void balance() {
    }

    @Test
    public void deposit() {
    }

    @Test
    public void withdraw() {
    }
}

2、程序运行结果截图

(1)测试通过截图

(2)测试失败截图

三、实验总结

通过本节实验,我了解到了测试的方法。掌握了用idea进行测试和插件Junit自动生成测试代码。实验能够顺利完成!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小孙同学1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值