Invoking a variable-length argument method in an abstract class using the "Mockito" spy to exclude a subclass

I am using Mockito 1.8.0, so I do not AnyVararg. Upgrading to a later version of Mockito is not on my team’s maps right now. So please bear with me

What the class looks like:

public abstract class Parent {

      public void someMethod(String a, String b)
      {
          //.....
      }

      public void foo(String a, String... b)
      {
          //.....
      }
}

public class Child extends Parent{
      public void bar() {
          someMethod(a,b);
          foo(a,b,c);
          methodToFailUsingSpy();
      }
}

Unit tests

 @Test
 public void someTest() {
       private spyOfChild  = //initialize here;
       doReturn("Something")).when(spyOfChild).methodToFailUsingSpy();
       /*  Tried using this, but did not help.
       doCallRealMethod().when(spyOfChild).foo(anyString());
       */
       spyOfChild.bar();
 }

The problem is

When the spy sees someMethod(), he calls the real method in the abstract class. But when he sees foo(), he tries to find the corresponding encoded method. Ie control goes into Mockito MethodInterceptorFilter, since it cannot find the layout, it throws java.lang.reflect.InvocationTargetException.

, foo() . , , someMethod(). - , - ?

+4
1

Mockito.

https://groups.google.com/forum/#!msg/mockito/P_xO5yhoXMY/FBeS4Nf4X9AJ

, :

class SimpleClass {

    public String varargsMethod(String... in) {
        return null;
    }

    public void testSpyVarargs() {
        SimpleClass sc = Mockito.spy(new SimpleClass());
        sc.varargsMethod("a", "b");
    }
}

, , , , .

, , Mockito. 1.9.5 , varargs, ( , , , Mockito sparg varargs).

, 1.8.0 1.9.5, .

+2

Source: https://habr.com/ru/post/1525889/


All Articles