2

I am trying to use Mockito to mock a method, however, I keep getting errors. I have tried to set up Mockito with these spring annotations -

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test")
@SpringBootTest
@PrepareForTest({MyClass.class})
public class TesterTest {

    @MockBean
    private MyClass myClass;

and then I use this Mockito call -

        Mockito.when(myClass.method(Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyMap(), Mockito.anyInt(), Mockito.anyInt()))
            .thenReturn(output);

To try and mock this method -

public Map<String,myObject> method(String string, Class<?> cc, ReflectionObject ro, Method method, Map<String, MyObject> objMap, int index, int maxIndex) { 

I get this error -

java.lang.NoSuchMethodError: com.hd.utils.MyClass.method(Ljava/lang/String;Ljava/lang/Class;Lcom/homedepot/supplychain/replenishment/service/ReflectionObject;Ljava/lang/reflect/Method;Ljava/util/Map;II)Ljava/util/Map;

at com.beam.TesterTest.test(TesterTest.java:159)
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.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$2.call(DelegatingPowerMockRunner.java:149)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$2.call(DelegatingPowerMockRunner.java:141)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.withContextClassLoader(DelegatingPowerMockRunner.java:132)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.run(DelegatingPowerMockRunner.java:141)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
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 argument matcher detected here:

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"))

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.

If I change my spring annotations to this -

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@ActiveProfiles("test")

I also get the same error. Does anyone know what I am doing wrong here? If I change the any calls to hard coded integers at the end, it doesn't complain about those anymore but I cannot do that for all my parameters...

GarudaAiacos
  • 161
  • 2
  • 17
  • see [this](https://stackoverflow.com/questions/23273230/misplaced-argument-matcher-detected-here-you-cannot-use-argument-matchers-outsi) – grape_mao Jul 05 '18 at 13:11
  • But according to this page https://javacodehouse.com/blog/mockito-tutorial/ you should be able to do something like this - when(daoMock.save(any(Customer.class))).thenReturn(new Customer()); to mock a method call to return something using the any's to say when any type of class or string etc is passed in return this, no? – GarudaAiacos Jul 05 '18 at 13:29
  • Could you show us the signature of the method? – grape_mao Jul 05 '18 at 14:01
  • public Map method(String string, Class> cc, ReflectionObject ro, Method method, Map objMap, int index, int maxIndex) { – GarudaAiacos Jul 05 '18 at 14:58
  • Nothing is wrong with your call to `when`; it looks valid, `method` is `public` and non-`final`, and I assume MyClass is also `public` and non-`final`. NoSuchMethodError is a new one to me in Matcher problems, though, and suggests a classpath or compilation problem. Can you please show us your whole test method, and consider a call to `validateMockitoUsage` immediately before your `when` call? [Mocks use static state](https://stackoverflow.com/a/22822514/1426891), and it's possible that the matcher stack is getting corrupted even before you interact with it. – Jeff Bowman Jul 05 '18 at 19:01

0 Answers0