我怎样才能修复这段代码中的这个错误[关闭]

2024-05-06

当我运行 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)

您的单元测试失败于setUp测试类的方法BankCommon因为用于创建测试的编号BankCard in a CurrentAccount太长或太短。和方法没有关系testSetName据我从堆栈跟踪中可以看出。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我怎样才能修复这段代码中的这个错误[关闭] 的相关文章

随机推荐

  • 使用 django-filter 检查并清除过滤器

    我在用Django 过滤器 http django filter readthedocs io过滤一个ListView如果应用了任何过滤器 希望显示 清除所有过滤器 链接 由于过滤系统的通用性 我还没有找到实现此目的的直接方法 到目前为止我
  • 模拟用户输入以使用不同参数多次调用脚本

    我必须使用提供的脚本 该脚本在脚本运行时接受用户输入而不是参数 我无法解决这个问题 脚本的一个例子是 bin bash echo param one read one doSomething echo param two read two
  • 如何使用 unboundid-ldap-sdp 导入 ldif 文件?

    我从 LDAP 服务器导出了以下 ldif 文件 现在尝试导入它 以便可以复制从中导出它的目录 dn cn MYCOMPANY Users dc mycompany dc com changetype add objectClass pos
  • 如何在 Emacs Lisp 中“URL 解码”字符串?

    我有一个像 foo 20bar 这样的字符串 我想要其中的 foo bar 我知道 Emacs Lisp 中必须有一个内置函数来解码 URL 编码的字符串 查询字符串 但我今天找不到它 无论是在我的文档中还是在我的文档中都找不到它 lisp
  • 如何解决 MongoWaitQueueFullException?

    我运行一个java程序 它是一个线程执行程序 它将数千个文档插入到mongodb中的表中 我收到以下错误 Exception in thread pool 1 thread 301 com mongodb MongoWaitQueueFul
  • 如何在文件中存储 Apache Ant 属性值

    我需要修改 Apache Ant 中的 xml 文件 loadfile 任务允许将文件的内容加载到属性中 但是 在 属性 修改后如何将属性的值存储回文件中 当然 我可以编写自定义任务来执行此操作 但我想知道是否有一些现有的实现 您可以使用e
  • 如何在 UNIX / Linux 操作系统下使用 sed 替换带有换行符 (\n) 的模式?

    我有一个 txt 文件 其中包含 Some random text here This file has multiple lines Should be one line I use sed q N s n sl g t q file1
  • Android:随着设备移动在mapv2上绘制路径

    我正在制作一个应用程序 我必须在其中当我的设备移动时在地图上绘制路径 我搜索了我的教程但都显示在两点之间绘制路径但是我要当我的设备移动时绘制路径 任何有关此的帮助将不胜感激 当你的设备移动时 你的意思是什么 无论哪种方式 您都必须使用位置侦
  • 为什么在 Javascript 中添加两位小数会产生错误的结果? [复制]

    这个问题在这里已经有答案了 可能的重复 JavaScript 的数学有问题吗 https stackoverflow com questions 588004 is javascripts math broken 为什么 JS 搞砸了这个简
  • PHP 中的 ebXML\ebMS 实现

    中是否有任何示例 测试用例或定义PHP实施 ebXML 它被推广为 B2B 数据传输方式 但信息 讨论和工具却无处可寻 我是否找错了地方或者使用 PHP 来解决这个错误 None
  • 我的所有 UIAlertController 消息都变成了单行

    我不确定这是否是罪魁祸首 但我对这个项目所做的最大改变是几天前升级到 Swift 4 我知道我的 UIAlertController 消息在需要时显示多行 但今天我偶然意识到它们全部变成了单行并且末尾有省略号 由于我从 API 显示这些消息
  • QGroupBox边框

    经过一段时间的搜索后 我发现在组框上设置可见边框的方法是使用 StyleSheet 属性 我补充道 border 2px solid gray 但有几个问题 1 组框内的所有内容也继承此设置 2 边框在标题附近有一个小洞 碎片缺失 Here
  • Python 有哪些 SOAP 客户端库,它们的文档在哪里? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 在 matplotlib 中查看然后自动关闭图形?

    我必须检查我的参数设置是否正确 因此我需要绘制许多图 为了绘制这些图 我选择使用 matplotlib 每次检查后 我需要单击左上角的关闭按钮 这很微不足道 那么有没有什么方法可以让剧情在3 5秒左右显示并且无需点击就自动关闭呢 我知道关于
  • 错误 LNK2019:无法解析的外部符号

    好的 所以我在尝试找出代码中的问题时遇到问题 我有很多代码 所以我只会发布编译时出现混乱的相关部分 我在类中有以下函数 它将编译并且一切都会正常运行 直到我调用函数 CalculateProbabilityResults 并运行其中的第 7
  • 不需要 VPC 运行的 AWS 服务列表

    谷歌再次让我失败 或者可能是我的问题不太清楚 有没有一种简单的方法 或者更确切地说 我们如何确定哪些服务是 VPC 绑定的 哪些服务是非 VPC 的 例如 EC2 RDS 需要 VPC 设置 Lambda S3 是公开可用的服务 不需要 V
  • okhhtp3 模拟服务器 java.lang.NoClassDefFoundError: okhttp3/internal/concurrent/TaskRunner$RealBackend

    Maven 依赖
  • 使用 Android 浏览器的 HTML5 离线存储

    我正在研究如何使用 Android 设备来创建需要离线使用的网站 我还没有找到很多关于Android浏览器如何处理HTML5的localStorage和WebSQL的信息 有人知道这些的尺寸限制吗 它们是否可以更改 我知道移动 Safari
  • 旋转后获取线的新位置

    我需要在一条线上使用 RotateTransform 方法找到旋转后线的新坐标 例如 在这一行之后 line RenderTransform new RotateTransform 25 0 0 line X1其他三个属性不会改变 我找到了
  • 我怎样才能修复这段代码中的这个错误[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 当我运行 jUnit 测试时 我在 testSetName 上遇到错误 这是为什么 因为测试是为了查看名称是否为 ferndown 以及它