运行测试时不断出错

时间:2013-11-10 20:49:21

标签: java junit

所以我正在运行jUnit测试并且我继续在分支2上收到错误,说该名称不正确,我该如何解决?我真的很困惑,你有没有想法需要改变什么因为我找不到如何解决这个问题?

测试代码:

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

整个测试代码:

package prog2.as1.test;

private static final String NEWLINE=System.getProperty("line.separator");

@Test
public void testGetName() {
    assertEquals("Bournemouth", branch3.getName());
}

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

@Test
public void testGetAddress() {
    assertEquals("Bournemouth Road", branch1.getAddress().getStreetname());
}

@Test
public void testSetAddress() {
    Address address2 = branch1.getAddress().clone();
    address2.setHouseNumber("9");
    branch1.setAddress(address2);
    assertEquals(address2, branch1.getAddress());
    assertEquals("9", branch1.getAddress().getHouseNumber());
}

@Test
public void testAddressToString() {
    assertEquals("6 Bournemouth Road"+NEWLINE+"BH4 1AA Bournemouth"+NEWLINE+"Dorset"+NEWLINE+"UK", branch1.getAddress().toString());
}

@Test
public void testAddressToString2() {
    branch1.getAddress().setHouseNumber(null);
    assertEquals("Bournemouth Road"+NEWLINE+"BH4 1AA Bournemouth"+NEWLINE+"Dorset"+NEWLINE+"UK", branch1.getAddress().toString());
}

@Test
public void testGetPhoneNumber() {
    assertEquals(new PhoneNumber("01202-123458"), branch3.getPhoneNumber());
}

@Test
public void testSetPhoneNumber() {
    branch2.setPhoneNumber(new PhoneNumber("-0 1 2 -0-2  654 321"));
    assertEquals(new PhoneNumber("01202-654321"), branch2.getPhoneNumber());
}

@Test
public void testGetBankManager() {
    assertEquals("Sarah", branch2.getBankManager().getGivenName());
    assertEquals("Jane", branch2.getBankManager().getSurName());
}

@Test
public void testSetBankManager() {
    final Person bankManager = new Person("Joe", "Blogs", new GregorianCalendar(1979, 8, 11));
    branch3.setBankManager(bankManager);
    assertEquals(bankManager, branch3.getBankManager());
}

@Test
public void testGetCustomers() {
    final TreeSet<Customer> s = new TreeSet<Customer>();
    s.add(customer1);
    s.add(customer2);
    s.add(manager1);
    assertEquals(s, branch1.getCustomers());
}

@Test
public void testEqualsObject() {
    assertEquals(branch1, branch1);
    assertFalse(branch1.equals(branch2));
    assertFalse(branch2.equals(branch1));

    assertEquals(branch3, branch3);
    assertFalse(branch1.equals(branch3));
    assertFalse(branch3.equals(branch1));

    assertEquals(branch2, branch2);
    assertFalse(branch2.equals(branch3));
    assertFalse(branch3.equals(branch2));
}

@Test
public void testCompareTo() {
    assertTrue(branch1.compareTo(branch2) > 0);
    assertTrue(branch2.compareTo(branch1) < 0);
    assertTrue(branch1.compareTo(branch1) == 0);

    assertTrue(branch1.compareTo(branch3) > 0);
    assertTrue(branch3.compareTo(branch1) < 0);
    assertTrue(branch3.compareTo(branch3) == 0);

    assertTrue(branch3.compareTo(branch2) < 0);
    assertTrue(branch2.compareTo(branch3) > 0);
    assertTrue(branch2.compareTo(branch2) == 0);
}

/**
 * Check that the account adding and transfer etc logic works correctly.
 */
@Test
public void testAccountTransfer() {
    Account account1 = customer1.getAccounts().iterator().next();
    assertTrue(customer1.getAccounts().contains(account1));
    assertFalse(customer2.getAccounts().contains(account1));
    assertFalse(manager1.getAccounts().contains(account1));
    assertEquals(customer1, account1.getCustomer());
    account1.setCustomer(customer2);
    assertFalse(customer1.getAccounts().contains(account1));
    assertTrue(customer2.getAccounts().contains(account1));
    assertFalse(manager1.getAccounts().contains(account1));
    assertEquals(customer2, account1.getCustomer());
    manager1.addAccount(account1);
    assertFalse(customer2.getAccounts().contains(account1));
    assertFalse(customer1.getAccounts().contains(account1));
    assertTrue(manager1.getAccounts().contains(account1));
    assertEquals(manager1, account1.getCustomer());
}

@Test
public void testToString() {
    assertEquals("Branch [name=Poole, address=12 Swanage Road, phoneNumber=01202123457, bankManager=Sarah Jane]", branch2.toString());
}

@Test
public void testSortedSurname() {
    assertEquals(Arrays.asList(customer2, customer1, manager1), branch1.getSortedCustomers(Branch.SortOrder.SORTNAME));
}

@Test
public void testSortedCurrentAccount() {
    assertEquals(Arrays.asList(customer1, customer2, manager1), branch1.getSortedCustomers(Branch.SortOrder.SORTCURRENT));
}

   }

银行普通:

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

}

 }

分行代码:

public class Branch实现了Comparable {

/**
 * Enum representing the various ways in which the customers in a branch can
 * be sorted.
 *
 * @author Paul de Vrieze
 */
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;
}

  }

0 个答案:

没有答案