-1

jUnit テストを実行すると、testSetName でエラーが発生しました。なぜですか? テストの原因は、名前がferndownであり、それが設定されているものであるかどうかを確認することですが、なぜエラーが発生するのですか? jUnit がブランチ名が正確で、正確に見えるかどうかをテストしていると思ったのに、なぜテストが失敗したと表示されるのですか?

    @Test
public void testSetName() {
    branch2.setName("Ferndown");
    assertEquals("Ferndown", branch2.getName());
}

支店コード:

package prog2.as1;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;



public class Branch implements Comparable<Branch> {


public static enum SortOrder implements Comparator<Customer> {
    /**
     * Sort ordered by the name of the customer
     */
    SORTNAME {

        @Override
        public int compare(final Customer o1, final Customer o2) {
            return o1.compareTo(o2);
        }
    },
    /**
     * Sort ordered by the first found current account number of the
     * customer.
     */
    SORTCURRENT {

        @Override
        public int compare(final Customer o1, final Customer o2) {
            Account a1 = null;
            for (Account a : o1.getAccounts()) {
                if (a instanceof CurrentAccount) {
                    a1 = a;
                    break;
                }
            }
            Account a2 = null;
            for (Account a : o2.getAccounts()) {
                if (a instanceof CurrentAccount) {
                    a2 = a;
                    break;
                }
            }

            if (a1 == null) {
                if (a2 == null) {
                    return 0;
                }
                return -1;
            }
            if (a2 == null) {
                return 1;
            }
            return a2.getAccountNumber().compareTo(a1.getAccountNumber());
        }

    };
}

private String name;

private Address address;

private PhoneNumber phoneNumber;

private Person bankManager;

private final Set<Customer> customers;

/**
 * Create a new branch object.
 *
 * @param name The name of the branch.
 * @param address The address of the branch.
 * @param phoneNumber The phone number of the branch.
 * @param bankManager The manager of the branch. This can be any person,
 *            including a {@link Customer} instance.
 */
public Branch(final String name, final Address address, final PhoneNumber phoneNumber, final Person bankManager) {
    this.name = name;
    this.address = address;
    this.phoneNumber = phoneNumber;
    this.bankManager = bankManager;
    this.customers = new HashSet<Customer>();
}

/**
 * Get the name of the branch.
 *
 * @return The name.
 */
public String getName() {
    return name;
}

/**
 * Set the name of the branch.
 *
 * @param name The new name of the branch.
 */
public void setName(final String name) {
    this.name = name;
}

/**
 * Get the address of the branch.
 *
 * @return The address.
 */
public Address getAddress() {
    return address;
}

/**
 * Set the address of the branch.
 *
 * @param address The new address.
 */
public void setAddress(final Address address) {
    this.address = address;
}

/**
 * Get the phone number of the branch.
 *
 * @return The phone number.
 */
public PhoneNumber getPhoneNumber() {
    return phoneNumber;
}

/**
 * Set the phone number of the branch.
 *
 * @param phoneNumber The new phone number.
 */
public void setPhoneNumber(final PhoneNumber phoneNumber) {
    this.phoneNumber = phoneNumber;
}

/**
 * Get the manager of the branch.
 *
 * @return The manager.
 */
public Person getBankManager() {
    return bankManager;
}

/**
 * Set the manager of the branch.
 *
 * @param bankManager The manager.
 */
public void setBankManager(final Person bankManager) {
    this.bankManager = bankManager;
}

/**
 * Get a set with all customers of the branch. This set is editable to allow
 * updating of the customer list.
 *
 * @return A {@link Set} with the customers.
 */
public Set<Customer> getCustomers() {
    return customers;
}


/**
 * {@inheritDoc}
 */
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((address == null) ? 0 : address.hashCode());
    result = prime * result + ((bankManager == null) ? 0 : bankManager.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode());
    return result;
}

/**
 * Compare this branch to another branch. First sorted by name
 * (lexicographically), then by phone number, and finally by manager.
 */
@Override
public int compareTo(final Branch o) {
    int result = name.compareTo(o.name);
    if (result == 0) {
        result = phoneNumber.compareTo(o.phoneNumber);
        if (result == 0) {
            result = bankManager.compareTo(o.bankManager);
        }
    }
    return result;
}

/**
 * {@inheritDoc}
 */
@Override
public String toString() {
    StringBuilder builder = new StringBuilder();
    builder.append("Branch [name=");
    builder.append(name);
    builder.append(", address=");
    builder.append(address.getHouseNumber()).append(' ').append(address.getStreetname());
    builder.append(", phoneNumber=");
    builder.append(phoneNumber);
    builder.append(", bankManager=");
    builder.append(bankManager.getName());
    builder.append("]");
    return builder.toString();
}

/**
 * Get a sorted list of customers. This list is constructed on demand, and
 * list modification does not result into modification of actual recorded
 * set of customers of the branch.
 *
 * @param sortorder The sorting order for the results. Expects
 *            {@link SortOrder}, but other comparators work.
 * @return A newly allocated list initialised with the customers of the
 *         branch, then sorted in the requested order.
 */
public List<Customer> getSortedCustomers(final Comparator<Customer> sortorder) {
    List<Customer> result = new ArrayList<Customer>(customers);
    Collections.sort(result, sortorder);
    return result;
}

}

スタックトレース:

java.lang.NumberFormatException: The string "128852690292106" 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)
4

1 に答える 1

3

でテストを作成するために使用される番号が長すぎるか短すぎるため、単体テストsetUpはテスト クラスのメソッドで失敗します。スタックトレースからわかる限り、メソッドとは何の関係もありません。BankCommonBankCardCurrentAccounttestSetName

于 2013-11-07T22:17:15.527 に答える