JUnit测试:通过模拟抑制枚举构造函数?
I know that it is possible to mock a single enum(using How to mock an enum singleton class using Mockito/Powermock?), but I have like 1000 of enum values and they can call 5 different constructors. The enum values are often changing in development.
对于我的JUnit测试,我只想真正模拟一个或两个.我不在乎其余的,但是它们仍然被实例化,这调用了一些讨厌的东西,这些东西从文件系统中加载了枚举的值.
I want to really mock only one or two for my JUnit test. I don't care about the rest, but they are still instantiated, which calls some nasty stuff, which loads the values for the enum from the file system.
是的,我知道这是非常糟糕的设计.但是目前我还没有时间进行更改.
Yes I know It's very bad design. But for now I don't get the time to change it.
目前,我们正在使用Mockito/powermock.但是任何可以解决这个问题的框架,**我的意思是欢迎不良的设计.
At the moment we have Mockito/powermock in use. But any framework, which can solve this sh** I mean bad design is welcome.
假设我有一个与此类似的枚举:
Let's say I have an enum similar to this:
public static enum MyEnum {
A(OtherEnum.CONSTANT),
B("1"),
C("1", OtherEnum.CONSTANT),
//...and so on for again 1000 enum values :(
private double value;
private String defaultValue;
private OtherEnum value;
/* Getter/Setter */
/* constructors */
}
我同意Nick-Holt的建议,他建议添加一个接口:
I agree with Nick-Holt who suggested adding an interface:
public interface myInterface{
//add the getters/setters you want to test
}
public enum MyEnum implements MyInterface{
//no changes needed to the implementations
//since they already implement the methods you want to use
}
现在您可以使用Mockito的普通模拟功能,而不必依赖Powermock
Now you can use the normal mock abilities of Mockito without having to rely on Powermock
MyInterface mock = Mockito.mock(MyInterface.class);
when(mock.foo()).thenReturn(...);
//..etc