如何使用 jUnit 测试使用单例的类?

发布于 2024-09-07 17:19:38 字数 1690 浏览 3 评论 0原文

我正在为机器开发模拟器。它读取一个文件,将其以字符串(作为二进制数)存储到一个模拟计算机内存的数组中。然后它被传递到一个解释器,该解释器获取数组的第一个元素,并根据前 4 个字符将其发送到另一个类。

这些类之一的示例如下:

public class Add {
private String cell;

Add() {
    cell = null;
}

/**
 * Method that performs the operation add.
 */
public void addition() {
    // TODO Figure out 2's complement.
    Loader instance = Loader.Instance();
    cell = instance.getCell();

    Integer DR1num = Integer.parseInt(cell.substring(4, 6), 2);
    Integer SR1num = Integer.parseInt(cell.substring(7, 9), 2);

    if (cell.charAt(10) == '0') {
        Integer SR2num = Integer.parseInt(cell.substring(13, 15), 2);
        Integer addedval = instance.getRegister(SR1num)
                + instance.getRegister(SR2num);
        instance.setCCR(addedval);
        instance.setRegister(DR1num, addedval);
    } else if (cell.charAt(10) == '1') {
        Integer immediate = Integer.parseInt(cell.substring(11, 15), 2);
        Integer addedval = instance.getRegister(SR1num) + immediate;
        instance.setCCR(addedval);
        instance.setRegister(DR1num, addedval);
    } else {
        System.err.println("Invalid Char at Position 10. Expected 0 or 1");
    }

    instance.incrementPC();

}

public String getcell() {
    return cell;
}

}

变量描述: 实例是单例 cell 是存储在内存数组中的二进制字符串 DR1num 是目标寄存器。 (又名 R0、R1、...R7) SR1num 是源寄存器

此操作的作用是获取两个源寄存器并将它们相加(然后设置条件代码寄存器(如果它是正数、负数或零)),或者获取一个源寄存器和一个立即值并将这两个值相加。结果存储在目标寄存器中。

我选择使用单例类,因为我需要内存、通用寄存器、条件代码寄存器和程序计数器在整个程序中保持同步。这带来的问题是我不知道如何测试我的程序(最好使用 jUnit)。

我能想到的测试程序的唯一方法是编写一堆单独的文件供程序读入。但这意味着它不是只测试程序的一部分,而是至少测试三个类。在程序中读取并包含单例的所有方法的类,确定调用哪个类的类,最后实际执行该部分的类(例如 Add)。

有人对更好的测试方法有什么建议吗?

I am developing a simulator for a machine. It reads a file in, stores it in a string (as a binary number) to an array that is supposed to simulate the memory of a computer. Then it gets passed to an interpreter that takes gets the first element of the array, and based on the first 4 characters it gets sent to another class.

An example of one of those classes is below:

public class Add {
private String cell;

Add() {
    cell = null;
}

/**
 * Method that performs the operation add.
 */
public void addition() {
    // TODO Figure out 2's complement.
    Loader instance = Loader.Instance();
    cell = instance.getCell();

    Integer DR1num = Integer.parseInt(cell.substring(4, 6), 2);
    Integer SR1num = Integer.parseInt(cell.substring(7, 9), 2);

    if (cell.charAt(10) == '0') {
        Integer SR2num = Integer.parseInt(cell.substring(13, 15), 2);
        Integer addedval = instance.getRegister(SR1num)
                + instance.getRegister(SR2num);
        instance.setCCR(addedval);
        instance.setRegister(DR1num, addedval);
    } else if (cell.charAt(10) == '1') {
        Integer immediate = Integer.parseInt(cell.substring(11, 15), 2);
        Integer addedval = instance.getRegister(SR1num) + immediate;
        instance.setCCR(addedval);
        instance.setRegister(DR1num, addedval);
    } else {
        System.err.println("Invalid Char at Position 10. Expected 0 or 1");
    }

    instance.incrementPC();

}

public String getcell() {
    return cell;
}

}

Variable Descriptions:
instance is the singleton
cell is the binary string that is stored in the memory array
DR1num is Destination Register. (aka R0, R1, ... R7)
SR1num is Source Register

What this operation does is takes either two Source Registers and adds them together (then sets the Condition Code Register if it is positive, negative, or zero), or takes a Source Register and an immediate value and adds those two together. The result is stored in the Destination Register.

I chose to use a singleton class because I need the Memory, General Purpose Registers, Condition Code Register, and Program Counter to be in sync throughout the entire program. The problem that this presents is that I have no idea how to test my program (preferably with jUnit).

The only way I can think of testing the program is by writing a bunch of individual files for the program to read in. Except this means that instead of testing only one part of my program, at a minimum it tests three classes. The class that reads in the program and contains all the methods for the singleton, the class that determines which class to call, and finally the class that actually executes that portion (such as Add).

Does anyone have any suggestions for a better way to test?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个少女永远怀春 2024-09-14 17:19:54

我认为如果加载器是一个接口,您可以测试它,然后通过使用 EasyMock 之类的模拟框架,您可以插入模拟的类实例而不是实际的实例。

如果加载器是一个接口,这将起作用

I think you can test it if the loader is an interface and then by using mocking frameowork like EasyMock you can insert the mocked class instance instead of the actual instance.

This will work if the Loader is an interface

淡看悲欢离合 2024-09-14 17:19:50

传统上避免这种情况的方法是使用控制反转 (IoC) 并将单例类的实例注入到客户端类中。您的客户端类提供了其运行所需的所有依赖类,并且不关心依赖类的生命周期。

Spring 是用于执行此操作的常见 Java 框架,但您可以通过使用引用构造类来简单地注入到单例(就这么简单)。

如果单例类实现了一个接口,那么测试就会变得更容易,因为您可以注入模拟变体而不是真正的实现,并构建您的模拟来为您的测试提供合适的测试数据。

The way to avoid this is traditionally to use inversion of control (IoC) and inject an instance of your singleton class into the client class. Your client class is provided with all the dependent classes it requires to function, and not concerned with the lifecycle of the dependent classes.

Spring is the common Java framework used to do this, but you can inject simply by constructing your class with a reference to the singleton (it's that simple).

If the singleton class implements an interface then testing is made easier since you can inject mock variants rather than the real implementation, and build your mock to provide suitable test data for your tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文