【问题标题】:JUnit Testing in EclipseEclipse 中的 JUnit 测试
【发布时间】:2017-10-31 03:47:11
【问题描述】:

这是我还需要在 JUnit 测试用例中测试的 BankAccount 类的一部分。

以下内容来自我的 BankAccount 类,涉及 JUnit 测试用例:

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-Unit 测试用例。我已经对此做了很多修改,到目前为止,我能让测试用例成功的唯一方法是,如果我将预期结果(对于帐户类型 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.type() 的输出,您会发现问题。运行此测试时,您认为您的帐户属于哪种类型?
  • test.getInterestRate(1.0); return 0 所以test() 的结果是正确的。
  • 为什么一个名为get的方法会带参数并改变状态?此外,您在没有明确原因的情况下比较整数和双精度数。

标签: java eclipse unit-testing junit


【解决方案1】:

您没有在测试中设置帐户类型。根据您在问题和测试代码中的陈述,我认为您希望您的测试看起来像这样:

@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() 方法中使用它们一样。例如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;
}

【讨论】:

  • 我添加了 test.set(BankAccount(1)); 行,现在它说:方法 BankAccount(int) 未定义类型 test_getInterestRate。 我想我只需要一个适当的 get() ?
  • 抱歉打错了。我的意思是说 test.setAccountType(1)。带有此编辑的上述代码可以正常工作。
【解决方案2】:

我明白了。主要问题在于我的代码 BankAccount 类的第一行,我的意思是我没有将int accountType; 设置为 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);
}
}

【讨论】:

  • 最好将 accountType 设为私有并使用 setter。最好将它们分成 4 个单独的测试。
猜你喜欢
  • 2011-08-08
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多