Java 为什么这个jUnit测试总是失败?

Java 为什么这个jUnit测试总是失败?,java,junit,Java,Junit,当我运行jUnit测试时,它总是说数字格式异常字符串没有正确的卡号长度,你知道代码有什么问题吗?我怎么可能解决这个问题?我已经在下面发布了测试、代码和跟踪: 测试: 跟踪: java.lang.NumberFormatException: The string "377659710738349" does not have the proper length of a card number at prog2.as1.CardNumber.<init>(CardNumber.java

当我运行jUnit测试时,它总是说数字格式异常字符串没有正确的卡号长度,你知道代码有什么问题吗?我怎么可能解决这个问题?我已经在下面发布了测试、代码和跟踪:

测试:

跟踪:

java.lang.NumberFormatException: The string "377659710738349" does not have the proper length of a card number
at prog2.as1.CardNumber.<init>(CardNumber.java:19)
at prog2.as1.BankCard.getNextCardNumber(BankCard.java:68)
at prog2.as1.BankCard.<init>(BankCard.java:54)
at prog2.as1.CurrentAccount.<init>(CurrentAccount.java:95)
at prog2.as1.test.BankCommon.setUp(BankCommon.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
银行卡:

public class BankCard {

private static long lastNumber;

static {
    Random r = new Random();
    do {
        lastNumber = r.nextLong() % 10000000000000000l;
    } while (lastNumber < 0l);
}

/**
 * An enumeration of the types of bank cards.
 *
 * @author Paul de Vrieze
 */
public static enum CardType {
    /** A cash card. */
    CashCard,
    /** A debit card. */
    DebitCard,
    /** A credit card. */
    CreditCard;
}

private CardNumber cardNumber;

private Calendar expiry;

private CardType kind;

/**
 * Create a new bank card of the specified type.
 *
 * @param type The type of card.
 */
public BankCard(final CardType type) {
    kind = type;
    cardNumber = getNextCardNumber();
    Calendar now = Calendar.getInstance();
    expiry = new GregorianCalendar(now.get(Calendar.YEAR) + 3, now.get(Calendar.MONTH), 1);
}

/**
 * Get a new 16 digit card number, randomly increased from the last one.
 *
 * @return A random card number.
 */
private static CardNumber getNextCardNumber() {
    Random rand = new Random();
    lastNumber = (lastNumber + 1 + rand.nextInt(100000)) % 1000000000000000l;
    Formatter f = new Formatter();
    CardNumber result = new CardNumber(f.format("%015d", Long.valueOf(lastNumber)).toString());
    f.close();
    return result;
}

/**
 * Get the expiry date of the card.
 *
 * @return A calendar with the expiry date.
 */
public Calendar getExpiry() {
    return expiry;
}


/**
 * Set the expiry date of the card.
 *
 * @param expiry The new expiry date.
 */
public void setExpiry(final Calendar expiry) {
    this.expiry = expiry;
}


/**
 * Return the type of the card.
 *
 * @return The type.
 */
public CardType getKind() {
    return kind;
}


/**
 * Set the type of the card.
 *
 * @param kind The new type of the card.
 */
public void setKind(final CardType kind) {
    this.kind = kind;
}

/**
 * Get the number of the card.
 *
 * @return The card number.
 */
public CardNumber getCardNumber() {
    return cardNumber;
}

/**
 * Set the number of the card.
 *
 * @param cardNumber The new card number.
 */
public void setCardNumber(final CardNumber cardNumber) {
    this.cardNumber = cardNumber;
}

/**
 * {@inheritDoc}
 */
@Override
public String toString() {
    return cardNumber + (new Formatter()).format(" exp: %1$tm/%1$ty", expiry).toString();
}

    }
银行普通股:

package prog2.as1.test;

 import java.math.BigDecimal;
 import java.util.GregorianCalendar;

 import org.junit.Before;

 import prog2.as1.AccountNumber;
 import prog2.as1.Address;
 import prog2.as1.Bank;
 import prog2.as1.Branch;
 import prog2.as1.CurrentAccount;
 import prog2.as1.Customer;
 import prog2.as1.EmploymentStatus;
 import prog2.as1.HighInterestSavingsAccount;
 import prog2.as1.ISASavingsAccount;
 import prog2.as1.PhoneNumber;
 import prog2.as1.StandardSavingsAccount;


 @SuppressWarnings("javadoc")
 public class BankCommon {

protected Bank bank;

protected Branch branch1;

protected Branch branch2;

protected Branch branch3;

protected Customer customer1;

protected Customer customer2;

protected Customer manager3;

protected Customer manager2;

protected Customer manager1;

public BankCommon() {
    super();
}

@Before
public void setUp() throws Exception {
    manager1 = new Customer("Gordon", "Gecko", new GregorianCalendar(1951, 5, 23), EmploymentStatus.Permanent);
    branch1 = new Branch("Winton", new Address("6", "Bournemouth Road", "BH4 1AA", "Bournemouth", "Dorset", Address.UK), new PhoneNumber("01202 123456"), manager1);
    manager2 = new Customer("Sarah", "Jane", new GregorianCalendar(1952, 2, 29), EmploymentStatus.SelfEmployed);
    branch2 = new Branch("Poole", new Address("12", "Swanage Road", "BH12 5XY", "Poole", "Dorset", Address.UK), new PhoneNumber("01202 123457"), manager2);
    manager3 = new Customer("John", "Person", new GregorianCalendar(1961, 11, 5), EmploymentStatus.Temporary);
    branch3 = new Branch("Bournemouth", new Address("6", "Winton Road", "BH1 1AA", "Bournemouth", "Dorset", Address.UK), new PhoneNumber("01202 123458"), manager3);
    bank = new Bank("Northern Rock", branch1, branch2, branch3);


    customer1 = new Customer("John", "Doe", new GregorianCalendar(1970, 1, 1), EmploymentStatus.Unemployed);
    customer1.addAccount(new CurrentAccount(customer1, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(100), BigDecimal.valueOf(500)));
    customer1.addAccount(new ISASavingsAccount(customer1, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(1000)));
    branch1.getCustomers().add(customer1);

    manager3.addAccount(new CurrentAccount(manager3, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(10000)));
    manager3.addAccount(new HighInterestSavingsAccount(manager3, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(20000)));
    branch3.getCustomers().add(manager3);

    customer2 = new Customer("Jane", "Doe", new GregorianCalendar(1971, 1, 1), EmploymentStatus.Temporary);
    customer2.addAccount(new CurrentAccount(customer2, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(100), BigDecimal.valueOf(600)));
    customer2.addAccount(new HighInterestSavingsAccount(customer2, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(1000)));
    branch1.getCustomers().add(customer2);

    manager2.addAccount(new CurrentAccount(manager2, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(200)));
    manager2.addAccount(new ISASavingsAccount(manager2, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(6000)));
    branch2.getCustomers().add(manager2);

    manager1.addAccount(new CurrentAccount(manager1, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(500)));
    manager1.addAccount(new StandardSavingsAccount(manager1, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(5000)));
    branch1.getCustomers().add(manager1);

}

}

显然,这是因为生成的CardNumber字符串长于或短于16个字符:

if (toString().length() != 16) {
    throw new NumberFormatException("The string \"" + origString + "\" does not have the proper length of a card number");
}

testcase一直失败,因为测试中的代码正在引发异常。它与测试中的字符串比较无关。测试中的代码永远不会正常完成。请使用调试器找出发生了什么?您已经问过一次这个问题。您必须调试代码。无论调用卡构造函数的是什么,都会向其传递一个15个字符的字符串,这会使您的异常跳闸。您已经编写了一个bug,需要检查代码以修复它。这不是一个你应该期待别人为你解决的问题。这就是编程。@user2971783您阅读stacktrace,并试图理解它所说的内容。在本例中,它将其拼写为:“字符串“377659710738349”没有正确的卡号长度”。它说这个异常是在CardNumber.java第19行抛出的。走到那一行,你会意识到代码要求信用卡号是16位数字。您正在生成15位数的数字。
public class CurrentAccount extends Account {

private BankCard debitCard;

/**
 * Get the debit card for this account.
 * 
 * @return The debit card.
 */
public BankCard getDebitCard() {
    return debitCard;
}

/**
 * Set the debit card for this account.
 * 
 * @param debitCard The debit card. The card type must be
 *            {@link CardType#DebitCard}.
 */
public void setDebitCard(final BankCard debitCard) {
    if (debitCard.getKind() != BankCard.CardType.DebitCard) {
        throw new IllegalArgumentException("The given card is not a debit card");
    }
    this.debitCard = debitCard;
}

/**
 * Get the credit card for this account.
 * 
 * @return The credit card.
 */
public BankCard getCreditCard() {
    return creditCard;
}


/**
 * Set the credit card for this account.
 * 
 * @param creditCard The credit card. The card type must be
 *            {@link CardType#CreditCard}.
 */
public void setCreditCard(final BankCard creditCard) {
    if (creditCard.getKind() != BankCard.CardType.CreditCard) {
        throw new IllegalArgumentException("The given card is not a credit card");
    }
    this.creditCard = creditCard;
}

private BankCard creditCard;

private BigDecimal overdraftLimit;

/**
 * Create a new account with initial zero balance.
 * 
 * @param customer The customer owning the account. This account will be
 *            added to the customer automatically.
 * @param accountNumber The account number.
 * @param overdraftLimit The overdraft limit on the account.
 */
public CurrentAccount(final Customer customer, final AccountNumber accountNumber, final BigDecimal overdraftLimit) {
    super(customer, accountNumber);
    this.overdraftLimit = overdraftLimit;
    setDebitCard(new BankCard(BankCard.CardType.DebitCard));
    setCreditCard(new BankCard(BankCard.CardType.CreditCard));
}

/**
 * Create a new account with initialized with a given balance.
 * 
 * @param customer The customer owning the account. This account will be
 *            added to the customer automatically.
 * @param accountNumber The account number.
 * @param balance The initial balance of the account.
 * @param overdraftLimit The overdraft limit on the account.
 */
public CurrentAccount(final Customer customer, final AccountNumber accountNumber, final BigDecimal balance, final BigDecimal overdraftLimit) {
    super(customer, accountNumber, balance);
    this.overdraftLimit = overdraftLimit;
    setDebitCard(new BankCard(BankCard.CardType.DebitCard));
    setCreditCard(new BankCard(BankCard.CardType.CreditCard));
}

/**
 * Get the overdraft limit for the account.
 * 
 * @see prog2.as1.Account#getOverdraftLimit()
 */
@Override
public BigDecimal getOverdraftLimit() {
    return overdraftLimit;
}

/**
 * Set the overdraft limit of the account.
 * 
 * @param overdraftLimit The new limit.
 */
public void setOverdraftLimit(final BigDecimal overdraftLimit) {
    this.overdraftLimit = overdraftLimit;
}

/**
 * {@inheritDoc}
 */
@Override
public String toString() {
    String s = super.toString();
    StringBuilder result = new StringBuilder(s.length() + 50);
    result.append(s.substring(0, s.length() - 1)).append(", debitcard: [").append(debitCard).append(']');
    result.append(", creditcard: [").append(creditCard).append("])");
    return result.toString();
}

/**
 * {@inheritDoc}
 */
@Override
public String getAccountName() {
    return "Current Account";
}


    }
package prog2.as1.test;

 import java.math.BigDecimal;
 import java.util.GregorianCalendar;

 import org.junit.Before;

 import prog2.as1.AccountNumber;
 import prog2.as1.Address;
 import prog2.as1.Bank;
 import prog2.as1.Branch;
 import prog2.as1.CurrentAccount;
 import prog2.as1.Customer;
 import prog2.as1.EmploymentStatus;
 import prog2.as1.HighInterestSavingsAccount;
 import prog2.as1.ISASavingsAccount;
 import prog2.as1.PhoneNumber;
 import prog2.as1.StandardSavingsAccount;


 @SuppressWarnings("javadoc")
 public class BankCommon {

protected Bank bank;

protected Branch branch1;

protected Branch branch2;

protected Branch branch3;

protected Customer customer1;

protected Customer customer2;

protected Customer manager3;

protected Customer manager2;

protected Customer manager1;

public BankCommon() {
    super();
}

@Before
public void setUp() throws Exception {
    manager1 = new Customer("Gordon", "Gecko", new GregorianCalendar(1951, 5, 23), EmploymentStatus.Permanent);
    branch1 = new Branch("Winton", new Address("6", "Bournemouth Road", "BH4 1AA", "Bournemouth", "Dorset", Address.UK), new PhoneNumber("01202 123456"), manager1);
    manager2 = new Customer("Sarah", "Jane", new GregorianCalendar(1952, 2, 29), EmploymentStatus.SelfEmployed);
    branch2 = new Branch("Poole", new Address("12", "Swanage Road", "BH12 5XY", "Poole", "Dorset", Address.UK), new PhoneNumber("01202 123457"), manager2);
    manager3 = new Customer("John", "Person", new GregorianCalendar(1961, 11, 5), EmploymentStatus.Temporary);
    branch3 = new Branch("Bournemouth", new Address("6", "Winton Road", "BH1 1AA", "Bournemouth", "Dorset", Address.UK), new PhoneNumber("01202 123458"), manager3);
    bank = new Bank("Northern Rock", branch1, branch2, branch3);


    customer1 = new Customer("John", "Doe", new GregorianCalendar(1970, 1, 1), EmploymentStatus.Unemployed);
    customer1.addAccount(new CurrentAccount(customer1, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(100), BigDecimal.valueOf(500)));
    customer1.addAccount(new ISASavingsAccount(customer1, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(1000)));
    branch1.getCustomers().add(customer1);

    manager3.addAccount(new CurrentAccount(manager3, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(10000)));
    manager3.addAccount(new HighInterestSavingsAccount(manager3, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(20000)));
    branch3.getCustomers().add(manager3);

    customer2 = new Customer("Jane", "Doe", new GregorianCalendar(1971, 1, 1), EmploymentStatus.Temporary);
    customer2.addAccount(new CurrentAccount(customer2, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(100), BigDecimal.valueOf(600)));
    customer2.addAccount(new HighInterestSavingsAccount(customer2, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(1000)));
    branch1.getCustomers().add(customer2);

    manager2.addAccount(new CurrentAccount(manager2, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(200)));
    manager2.addAccount(new ISASavingsAccount(manager2, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(6000)));
    branch2.getCustomers().add(manager2);

    manager1.addAccount(new CurrentAccount(manager1, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(500)));
    manager1.addAccount(new StandardSavingsAccount(manager1, AccountNumber.getNextAccountNumber(), BigDecimal.valueOf(5000)));
    branch1.getCustomers().add(manager1);

}

}
if (toString().length() != 16) {
    throw new NumberFormatException("The string \"" + origString + "\" does not have the proper length of a card number");
}