Junit的简单使用

时间:2022-02-10 02:22:01

1 实验

刚学了下利用进行Junit的单元测试,现在来记录下。

JUnit是一个开源的Java编程语言的单元测试框架,应用Junit可以快速高效的对代码进行测试。

现在主要利用JUnit测试如下代码,代码比较简单就不注释了,可以看出代码主要分为两个类BankAccount 和BankAccountGold

 1 public class BankAccount
2 {
3 String AccountNumber;
4 String AccountName;
5 String ID;
6 String Password;
7 double TotalMoney;
8
9 public BankAccount(String AccountNumber, String AccountName, String ID, String Password)
10 {
11 this.AccountNumber = AccountNumber;
12 this.AccountName = AccountName;
13 this.ID = ID;
14 this.Password = Password;
15 }
16
17 public void Balance(String AccountNumber)
18 {
19 System.out.println("PLEASE FIND YOUR BALANCE INFORMATION");
20 System.out.println("Account name:" + AccountName);
21 System.out.println("Account number:" + AccountNumber);
22 System.out.println("Your balance:" + TotalMoney);
23 }
24
25 public void Deposit(double AddMoney)
26 {
27 try
28 {
29 if(AddMoney < 0) throw new RuntimeException();
30 this.TotalMoney += AddMoney;
31 }
32 catch(RuntimeException except)
33 {
34 System.out.println("Failed to deposite, money should be larger than 0");
35 }
36 System.out.println("Deposited money succeeded, the total money is:" + TotalMoney);
37 }
38
39 public void Withdraw(double GetMoney)
40 {
41 try
42 {
43 if(GetMoney > this.TotalMoney ) throw new RuntimeException();
44 this.TotalMoney -= GetMoney;
45 System.out.println("You have withdrawed money:" + GetMoney);
46 }
47 catch (RuntimeException except)
48 {
49 System.out.println("Error! you don't have enough balance");
50 }
51 }
52 }
53
54 class BankAccountGold extends BankAccount
55 {
56 public BankAccountGold(String AccountNumber, String AccountName, String ID, String Password)
57 {
58 super(AccountNumber,AccountName,ID,Password);
59 }
60 public void Withdraw(double GetMoney, double overdraft)
61 {
62 overdraft = this.TotalMoney - GetMoney;
63 if(overdraft > 0)
64 {
65 this.TotalMoney = overdraft;
66 System.out.println("Succeeded to withdraw. your balance is" + this.TotalMoney);
67 }
68 else if((overdraft < 0)&&(overdraft > -1000))
69 {
70 this.TotalMoney = overdraft + overdraft * 0.05; // The interest is 5% of the overdraft
71 System.out.println("Succeeded to withdraw. your balance is" + this.TotalMoney);
72 }
73 else
74 {
75 System.out.println("Failed to withdraw, you can not overdraft more than 1000!");
76 }
77 }
78 }

利用Junit写出的测试代码如下

  1 import static org.junit.Assert.*;
2
3 import org.junit.AfterClass;
4 import org.junit.Test;
5
6 public class BankAccountTest {
7
8 @AfterClass
9 public static void tearDownAfterClass() throws Exception {
10 }
11
12 @Test
13 public void BankAccount()
14 {
15 String AccountNumberTest = new String("1");
16 String AccountNameTest = new String("Q");
17 String IDTest = new String("QID");
18 String PasswordTest = new String("123");
19 BankAccount s = new BankAccount(AccountNumberTest, AccountNameTest, IDTest, PasswordTest);
20 assertEquals(AccountNumberTest, s.AccountNumber);
21 assertEquals(AccountNameTest, s.AccountName);
22 assertEquals(IDTest, s.ID);
23 assertEquals(PasswordTest, s.Password);
24 }
25
26 @Test
27 public void Balance()
28 {
29 BankAccount s = new BankAccount("1", "Q", "QID", "123");
30
31 s.Balance(s.AccountNumber);
32 }
33
34 @Test
35 public void ExceptionDeposit()
36 {
37 BankAccount s = new BankAccount("1", "Q", "QID", "123");
38 s.Deposit(-10);
39 }
40
41 @Test
42 public void Deposit()
43 {
44 BankAccount s = new BankAccount("1", "Q", "QID", "123");
45 s.Deposit(10);
46 }
47
48 @Test
49 public void ExceptionWithDraw()
50 {
51 BankAccount s = new BankAccount("1", "Q", "QID", "123");
52 s.TotalMoney = 10;
53 s.Withdraw(100);
54 }
55
56 @Test
57 public void WithDraw()
58 {
59 BankAccount s = new BankAccount("1", "Q", "QID", "123");
60 s.TotalMoney = 100;
61 s.Withdraw(10);
62 }
63 }
64
65 import static org.junit.Assert.*;
66
67 import org.junit.AfterClass;
68 import org.junit.Test;
69
70 public class BankAccountGoldTest {
71
72 @AfterClass
73 public static void tearDownAfterClass() throws Exception {
74 }
75
76 @Test
77 public void BankAccountGold()
78 {
79 String AccountNumberTest = new String("1");
80 String AccountNameTest = new String("Q");
81 String IDTest = new String("QID");
82 String PasswordTest = new String("123");
83
84 BankAccountGold s= new BankAccountGold(AccountNumberTest, AccountNameTest, IDTest, PasswordTest);
85 assertEquals(AccountNumberTest, s.AccountNumber);
86 assertEquals(AccountNameTest, s.AccountName);
87 assertEquals(IDTest, s.ID);
88 assertEquals(PasswordTest, s.Password);
89 }
90
91 @Test
92 public void WithDraw()
93 {
94 BankAccountGold s = new BankAccountGold("1", "Q", "QID", "123");
95 s.TotalMoney = 100;
96 s.Withdraw(10.0);
97 s.TotalMoney = 100;
98 s.Withdraw(150, 0);
99 s.TotalMoney = 100;
100 s.Withdraw(2000, 0);
101 }
102 }

测试结果如下:

Junit的简单使用Junit的简单使用

2 总结&问题

  这是我第一次接触自动的单元测试的框架,感觉还是很新奇。Junit在我看来本质就是利用一些assert等的辅助语句实现对软件自动化测试的工具。刚开始还以为测试用例都可以自己生成,接触后才发现还是无法达到这么智能,需要自己进行书写测试用例,只不过把一些之前可能要利用print才能看出结果的语句,利用自动化的图表统计出来。这个实验由于java和Junit都不熟,有个问题在这里记录下,希望之后可以解决。

  在待测试代码中存在try catch语句时,如何测试代码进入到catch中。这里并不能直接利用Junit4中的expected来检测待测试进入异常。上述实验中是通过观看输出来判定是否到达这个分支,并不能实现自动测试。