1

I am using PowerMockito to mock a call to a static class and the method has an argument that is an array of Objects. So the call should look something like this:

String temp = MyClass.doSomething(MyObject[] objArray1);

But when I try to mock with PowerMockito like this:

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class)
public class TestClass {

@Test
public void myTest {
    MyObject[] myObjArray1 = new MyObject[1];
    myObjArray1[0] = new MyObject();

    PowerMockito.mockStatic(MyClass.class);
    PowerMockito.when(MyClass.class, "doSomething", myObjArray1).thenReturn("A String");

    ...
}

This gives me a warning in Eclipse:

The argument of type MyObject[] should explicitly be cast to Object[] for the invocation of the varargs method when(Class, String, Object...) from type PowerMockito. It could alternatively be cast to Object for a varargs invocation But when I cast to an Object like this:

PowerMockito.when(MyClass.class, "doSomething", (Object) objArray1).thenReturn("A String");

I am not having that string returned when this method executes, I'm assuming that this is because the Object type parameter causes the method to not be recognized because it is expecting something of type MyObject as a parameter.

Any ideas of how to pass a non-primitive without casting as Object or how to get the method to be recognized with the cast?

4
  • Is it really String temp = MyClass.doSomething(Object[] objArray1); ? Jul 29, 2015 at 16:41
  • No, the method is in a static class and its actual definition looks like this: public static String doSomething(MyObject[] objArray) { ...} Jul 29, 2015 at 16:50
  • The casting is only needed to compile TestClass so that the proper method signature of when is invoked; it does not change the type of the argument itself. The problem is likely in doSomething. What are the signatures of MyClass.doSomething?
    – Kenney
    Jul 29, 2015 at 16:50
  • I'm just mocking the MyClass method so it isn't actually executing any code inside of doSomething(). Isn't it just recognizing that when MyClass.doSomething is called with the parameter of type MyObject[] and then returning "A String"? Jul 29, 2015 at 16:54

2 Answers 2

1

try this

PowerMockito.doReturn("A String").when(MyClass.class, "doSomething", Matchers.anyObject());

0
0

This is normal. You need to pick Object... objs or Object[] objs.

Example...

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String[] inputs = {" ", " ", " "};

        method((Object) inputs);
        method((Object[]) inputs);

    }

    static void method(Object... obj) {
        System.out.println("obj.length = " + obj.length);
    }

}

This prints..

obj.length = 1   
obj.length = 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.