为测试目的向Java枚举添加值

为测试目的向Java枚举添加值,java,unit-testing,testing,jmockit,Java,Unit Testing,Testing,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() {

我正在尝试进行一些多线程测试,使用与此类似的代码:

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;
    }
}
问题是为了测试这一点,我的for循环应该处理多辆车(多线程逻辑发生在车内)。但是,我不能更改枚举,因为此时我们只有一辆车,但在接下来的春天会有更多

如何在运行时添加另一辆车,仅用于测试

编辑:

这是我尝试过但不起作用的方法:

新车(2);->没有新的枚举器实例

创建第二个名为SpecialCar的类,其中包含2个SpecialCar,并在测试期间替换它们

类SpecialCar扩展->无法扩展枚举

模拟Car中的values()方法。 所以

问题:没有更多的汽车要添加到阵列。

Car.values()
。因此,要么等待编写单元测试,要么:

添加第二个Car值,根据
values()
编写单元测试,与特定常数无关。 删除第二个Car值,并将所有内容检入版本控制系统


某些测试可能会因为只有一个值而被解除,甚至可能需要检查
if(Car.values().length!=0)

您可以让您的枚举实现一个接口,并让一个测试枚举也实现该接口,然后将相应枚举的类传递到测试中

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());
    }
}
公共接口车辆{
public int getValue();
}
公共车辆{
A(1);
public int getValue(){
返回值;
}
私有最终整数值;
汽车(最终整数值){
这个值=值;
}
}
公共枚举测试车辆{
A(1),B(2);;
public int getValue(){
返回值;
}
私有最终整数值;
汽车(最终整数值){
这个值=值;
}
}

public void test(针对
中的ClassIf logic)只使用像
getValue
这样的方法,您可以将这些方法拉到一个接口上,让您的
enum
实现它。在测试时,您可以根据需要创建尽可能多的实现。它能解决问题吗?
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());
    }
}