简体   繁体   English

将值添加到Java ENUM以进行测试

[英]Add values to a Java ENUM for testing purposes

I'm trying to make some multi-threading test, using JMockit in a code similar to this one: 我正在尝试使用类似于以下代码的JMockit进行一些多线程测试:

class scratch_1 {
    public static void main(String[] args) {

        for (final Car ex: Car.values()) {
            System.out.println(ex.getValue());
        }
    }
}

enum Car {
    A(1);

    public int getValue() {
        return value;
    }

    private final int value;

    Car(final int value){
        this.value = value;
    }
}

The problem is that to test this, my for cycle should handle more than one Car (the multi-threading logic happens inside). 问题是要对此进行测试,我的for周期应处理多个Car(多线程逻辑发生在内部)。 However, I can't change the enum, because at this point in time we only have 1 car, but will have more in the following springs. 但是,我无法更改枚举,因为在这一点上,我们只有1辆车,但在接下来的春天将有更多的车。

How can I add another Car in runtime, only for testing? 如何在运行时添加仅用于测试的其他Car?

EDIT: 编辑:

This is what I have tried that didn't work: 这是我尝试过的无效的方法:

new Car(2); 新车(2); -> no new instances of enumarators ->没有新的列举者

Create a second class called SpecialCar with 2 SpecialCars, and replace them during tests. 用2个SpecialCars创建一个名为SpecialCar的第二类,并在测试期间替换它们。

Class SpecialCar extends -> enums can't be extended SpecialCar类扩展->无法扩展枚举

mock the values() method from Car. 模拟Car中的values()方法。 so 所以

new Expectations() {
            {
                 car.values();
                 result = {car.A... }

Problem : no more cars to add to array. 问题:没有更多的汽车要添加到数组中。

There is Car.values() . Car.values() So either wait writing the unit test, or: 因此,要么等待编写单元测试,要么:

Add a second Car value, write a the unit tests based on values() , agnostic towards the specific constant. 添加第二个Car值,根据values()编写单元测试,对特定常量不可知。 Remove the second Car value, and check all in into the version control system. 删除第二个Car值,然后全部输入到版本控制系统中。

Some test may be disarmed because of the being just one value, and maybe even needs a check on if (Car.values().length != 0) . 某些测试可能只是一个值而无法使用,甚至可能需要检查if (Car.values().length != 0)

You can have your enum implement an interface, and have a test enum that also implements that interface, and then pass the class of the appropriate enum into the test. 您可以让您的枚举实现一个接口,并让一个测试枚举也实现该接口,然后将适当的枚举的类传递给测试。

public interface Vehicle {
    public int getValue();
}

public enum Car implements Vehicle {
    A(1);

    public int getValue() {
        return value;
    }

    private final int value;

    Car(final int value){
        this.value = value;
    }
}

public enum TestCar implements Vehicle {
    A(1), B(2);

    public int getValue() {
        return value;
    }

    private final int value;

    Car(final int value){
        this.value = value;
    }
}

public void test(Class<? extends Vehicle> clazz) {
    for (final Vehicle vehicle : clazz.getEnumConstants()) {
        System.out.println(vehicle.getValue());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM