实验一 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自动生成测试代码。实验能够顺利完成!