0

I have a static void method inside a util class that I try to mock.

public class UtilClass {
   public static void foo(ClassA a, ClassB b) {
     async insert objects to a map
  }
}

And I try to return a costum Answer:

@test
    public void myTest() throws Exception {
    doAnswer(defaultAnswer).when(mock(UtilClass.class),
            method(UtilClass.class,"foo", ClassA.class, ClassB.class)).withArguments(any(ClassA.class),any(ClassB.class));
        myMock.somefunction();
      }
    }

      Answer<Object> defaultAnswer = new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
          BetweenSessionsFeatures betweenSessionsFeatures = (BetweenSessionsFeatures) invocation.getArgument(1);
          betweenSessionsFeatures.getUserSpecificFeatures().put("haha","hahh");
          return null;
        }
      };

Where somefunction calls UtilClass.foo
I recieve the following error

java.lang.NullPointerException
    at UtilClass.foo(UtilClass.java:138)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.powermock.api.mockito.internal.expectation.DefaultPrivatelyExpectedArguments.withArguments(DefaultPrivatelyExpectedArguments.java:44)
    at com.moblica.appsrv.session.runtime.PredictiveResources.BetweenSessionsFeaturesTest.testUserFeaturesDoExtract(BetweenSessionsFeaturesTest.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced or misused argument matcher detected here:

-> at UtilClass. myTest(UtilClass.java:84)
-> at UtilClass. myTest(UtilClass.java:84)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

This message may appear after an NullPointerException if the last matcher is returning an object 
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
    when(mock.get(any())); // bad use, will raise NPE
    when(mock.get(anyInt())); // correct usage use

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

I probably misuse the mock static method scheme, but doing it with doReturn instead of doAnswer works on other methods,
I need the static function to mock the behaviour of foo, hence can't just use deReturn.
Because of the duplication mark : please notice that I try to make a custom mock, and not just doReturn

DsCpp
  • 2,259
  • 3
  • 18
  • 46
  • 1
    Possible duplicate of [Mocking static methods with Mockito](https://stackoverflow.com/questions/21105403/mocking-static-methods-with-mockito) – Progman Sep 12 '18 at 16:42
  • Please read fully, published an edit to clarify – DsCpp Sep 12 '18 at 16:46
  • The duplicate Answer is completely irrelevant, as using that syntax for doAnswer doesn't work. Please drop it, or explain your intention. – DsCpp Sep 12 '18 at 19:20
  • If you want to use `doAnswer()` you can look at https://stackoverflow.com/questions/18069396/doanswer-for-static-methods-powermock – Progman Sep 13 '18 at 09:46
  • The documentation links are broken, and the method doesn't work, please drop the duplicate, and advise only if you are familiar with the subject, and can add added value, I know how to use the search also:) – DsCpp Sep 13 '18 at 09:50
  • Please edit your question to include the full source code of you test case. Provide a [MVCE](https://stackoverflow.com/help/mcve) of your test case which can be compiled and tested by others. It looks weird that you are using `mock(UtilClass.class)` for a method which will be called in a static way. `PowerMockito.doAnswer(defaultAnswer).when(UtilClass.class);` will call the `Answer` instance when used as in the linked questions. Also include your attempts on mocking the static method and how they failed. – Progman Sep 13 '18 at 11:43

0 Answers0