Ryan Horner
Ryan Horner

Reputation: 123

JUnit Testing in Eclipse

This is part of a BankAccount class that I need to also test within a JUnit test case.

the following is from my BankAccount class that concerns the JUnit test case:

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());

Now this is my actual J-Unit test case. I've tinkered with this quite a bit and so far the only way I can get the test case to be successful is if I set the expected result (for account type 1) to be 0, when it should be 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
}

}

Any tips? This is my first time working with JUnit frames. All the tutorials I read/watched were using very simple methods so I'm stuck.

Upvotes: 0

Views: 669

Answers (2)

tune5ths
tune5ths

Reputation: 723

You aren't setting the account type in your test. Based on what you've stated in your question and your test code I think you want your test to look like this:

@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
}

The double r that you take in get interest rate doesn't do anything (yet). The if statements that you use check for double values being returned from getAccountType(). They should be int values the way you use them in your type() method. e.g. 1, 2, 3, or 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;
}

Upvotes: 1

Ryan Horner
Ryan Horner

Reputation: 123

I got it. The main issue was in the very first line of my code BankAccount class, by which I mean I didn't have int accountType; set to public so I could use it in my J-unit test case. Once I did I pieced it together quite easily. Thank you to everyone who provided insight, I found all of it to be helpful.

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);
}
}

Upvotes: 0

Related Questions