JUnit tests: disallow enumeration constructor by mocking?

I know that you can make fun of a single enumeration (using How to mock a singleton enum class using Mockito / Powermock? ), But I have 1000 enum and they can call 5 different constructors. Enumeration values ​​often change during development.

I want to really make fun of only one or two for my JUnit test. I don’t care about the others, but they are still created, which causes some unpleasant things that load values ​​for listing from the file system.

Yes, I know this is a very bad design. But so far I do not have time to change it.

We are currently using Mockito / powermock. But any structure that can solve this problem, I mean, poor design is welcome.

Let's say I have an enumeration 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 */ } 
+7
java enums junit mocking suppression
source share
1 answer

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 } 

Now you can use the usual mockito layout capabilities without relying on Powermock

 MyInterface mock = Mockito.mock(MyInterface.class); when(mock.foo()).thenReturn(...); //..etc 
+1
source share

All Articles