Java Eclipse中的JUnit测试
这是BankAccount类的一部分,我还需要在JUnit测试用例中进行测试 以下内容来自我的BankAccount类,它涉及JUnit测试用例:Java Eclipse中的JUnit测试,java,eclipse,unit-testing,junit,Java,Eclipse,Unit Testing,Junit,这是BankAccount类的一部分,我还需要在JUnit测试用例中进行测试 以下内容来自我的BankAccount类,它涉及JUnit测试用例: int accountType; public int getAccountType() { return accountType; } public void setAccountType(int accountType) { this.accountType = accountType; } //getting varia
int accountType;
public int getAccountType() {
return accountType;
}
public void setAccountType(int accountType) {
this.accountType = accountType;
}
//getting variable to determine interest rate according to account type
public double getInterestRate(double r) {
double a;
if(getAccountType()==1.0) {
a = 0.5;
}
else if(getAccountType()==2.0) {
a = 4.5;
}
else if(getAccountType()==3.0) {
a = 1.0;
}
else if(getAccountType()==4.0) {
a = 15;
}
else {
a = 0;
}
return a;
}
String type() {
if(getAccountType()==1) {
return "Savings";
}
else if(getAccountType()==2) {
return "Award Savers";
}
else if(getAccountType()==3) {
return "Checking";
}
else if(getAccountType()==4) {
return "Credit Card";
}
else {
return "None";
}
}
//getting the account type from the user
System.out.println("Enter a number from 1 to 4 according to your account type: ");
System.out.println("1 --> Savings");
System.out.println("2 --> Award Savers");
System.out.println("3 --> Checking");
System.out.println("4 --> Credit Card");
bank.setAccountType(scan.nextInt());
//printing the account type back to the user
System.out.println("Account Type: " + bank.type());
这是我实际的J单元测试用例。我已经对此进行了相当多的修改,到目前为止,使测试用例成功的唯一方法是将预期结果(对于帐户类型1)设置为0,而实际结果应为0.5
package test;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import core.BankAccount;
class test_getInterestRate {
@Test
void test() {
BankAccount test = new BankAccount();
double rate = test.getInterestRate(1.0); // this SHOULD make rate = 0.5
assertEquals(0.5, rate); //this test fails because rate = 0, not 0.5
}
}
有什么建议吗?这是我第一次使用JUnit框架。我阅读/观看的所有教程都使用了非常简单的方法,因此我被卡住了。您没有在测试中设置帐户类型。根据您在问题中的陈述和测试代码,我认为您希望您的测试如下所示:
@Test
void test() {
BankAccount test = new BankAccount();
test.setAccountType(1); // this is what should make the rate = 0.5
double rate = test.getInterestRate(1.0);
assertEquals(0.5, rate); //this test fails because rate = 0, not 0.5
}
你得到的双r利率(现在)没有任何作用。使用的if语句检查从getAccountType()返回的双值。它们应该是int值,就像在type()方法中使用它们一样。e、 g.1、2、3或4
if(getAccountType()==1) {
a = 0.5;
}
else if(getAccountType()==2) {
a = 4.5;
}
else if(getAccountType()==3) {
a = 1.0;
}
else if(getAccountType()==4) {
a = 15;
}
else {
a = 0;
}
我明白了。主要问题是在我的代码BankAccount类的第一行,我的意思是我没有
intAccountType代码>设置为public,这样我就可以在我的J单元测试用例中使用它了。有一次,我很容易就把它拼凑起来了。感谢所有提供见解的人,我发现所有这些都很有帮助
class test_getInterestRate {
@Test
void test() {
BankAccount test = new BankAccount();
//testing for account type #1: "Savings"
test.accountType = 1;
double rate1 = test.getInterestRate(1);
assertEquals(0.5, rate1);
//testing for account type #2: "Award Savers"
test.accountType = 2;
double rate2 = test.getInterestRate(2);
assertEquals(4.5, rate2);
//testing for account type #3: "Checking"
test.accountType = 3;
double rate3 = test.getInterestRate(3);
assertEquals(1.0, rate3);
//testing for account type #4: "Credit Card"
test.accountType = 4;
double rate4 = test.getInterestRate(4);
assertEquals(15, rate4);
}
}
我认为如果您检查test.type()的输出,就会发现问题所在。运行此测试时,您认为您的帐户具有什么类型?test.getInterestRate(1.0)
返回0,因此test()
的结果是正确的。为什么名为get
的方法会获取参数并更改状态?另外,您比较整数和双精度是没有明确原因的代码>行,现在它说:BankAccount(int)方法对于类型测试是未定义的。我想我只需要一个合适的get()?对不起,那是个打字错误。我的意思是test.setAccountType(1)。使用此编辑的上述代码工作正常。将accountType设置为私有并使用setter是一种很好的做法。另外,最好在4个单独的测试中分开这些测试。