Home > Blockchain >  Unable to mock enum static method with null parameter
Unable to mock enum static method with null parameter

Time:12-20

I am trying to mock a static method of enum which has null value like below

 try (MockedStatic<SomEnum> e = Mockito.mockStatic(SomEnum.class)) {
            e.when(() -> SomEnum.methodWhichAcceptingNullParam(any())).thenReturn(somValue);
        }

here any() is not working... i am not sure I am passing null parameter inside method

methodWhichAcceptingNullParam

I have tried both any and isNull.The fact is SomEnum.methodWhichAcceptingNullParam is always get called however it should not because I provided a mocked value already

any help?

CodePudding user response:

Is your code called with a null or not-null object? This information is missing (at least for me).

Assuming you are calling SomeEnum.methodWhichAcceptingNullParam(null), then you need to use

ArgumentMatchers.isNull()

instead of any().

  • Related