Create JUnit tests for a card validating method

62 views Asked by At

I created a Java method that takes a card number and an expiry date and validates the card. I used Luhn's algorithm for validating the card number.

public static boolean isValidCreditCard(String cardNumber, String expiryDate) {
    if (cardNumber.length() != 16) {
        return false;
    }

    int[] digits = new int[16];
    for (int i = 0; i < 16; i++) {
        char digitChar = cardNumber.charAt(i);
        if (!Character.isDigit(digitChar)) {
            return false;
        }
        digits[i] = Character.getNumericValue(digitChar);
    }

    int sum = 0;
    for (int i = 15; i >= 0; i--) {
        int digit = digits[i];

        if (i % 2 == 0) {
            digit *= 2;

            if (digit > 9) {
                digit -= 9;
            }
        }

        sum += digit;
    }

    if (sum % 10 != 0) {
        return false;
    }

    try {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/yy");
        dateFormat.setLenient(false);
        Date expiry = dateFormat.parse(expiryDate);
        Date currentDate = new Date();
        return expiry.after(currentDate);
    } catch (ParseException e) {
        return false;
    }
}

Now I want to create tests for this method and implement them in JUnit. I need to create them using equivalence partitioning, boundary value analysis and cause-effect graphing. So I should have 3 sets of tests, one for each testing technique, but I have a hard time figuring out how to divide the tests between the different sets.

I tried making the equivalence classes and it resulted in the tests below. I'm curious about what sets of tests should I choose for boundary value analysis and cause-effect graphing.

These are my tests so far:

@Test
void testValidCard() {
    String cardNumber = "4140496470751796";
    String expiryDate = "02/25";
    assertTrue(CardValidator.isValidCreditCard(cardNumber, expiryDate));
}

@Test
void testCardNumberTooShort() {
    String cardNumber = "4615241";
    String expiryDate = "08/24";
    assertFalse(CardValidator.isValidCreditCard(cardNumber, expiryDate));
}

@Test
void testCardNumberTooLong() {
    String cardNumber = "42518293883012154802";
    String expiryDate = "05/24";
    assertFalse(CardValidator.isValidCreditCard(cardNumber, expiryDate));
}

@Test
void testCardNumberInvalidDigits() {
    String cardNumber = "4140ab4398603587";
    String expiryDate = "12/24";
    assertFalse(CardValidator.isValidCreditCard(cardNumber, expiryDate));
}

@Test
void testCardNumberInvalid() {
    String cardNumber = "4064249483030546";
    String expiryDate = "05/24";
    assertFalse(CardValidator.isValidCreditCard(cardNumber, expiryDate));
}

@Test
void testCardExpiryDateBadFormat() {
    String cardNumber = "5416151244634950";
    String expiryDate = "11-2025";
    assertFalse(CardValidator.isValidCreditCard(cardNumber, expiryDate));
}

@Test
void testCardExpired() {
    String cardNumber = "5443965499742482";
    String expiryDate = "01/23";
    assertFalse(CardValidator.isValidCreditCard(cardNumber, expiryDate));
}
0

There are 0 answers