Using Mockito: Matching multiple arguments in a private static method?

user3537932

I've been trying to use Mockito and PowerMockito to test my code. I have something akin to the following class:

public class asdfClass{

    public static String methodToMock(String item, String otheritem){
      return "asdf";
    }

    public static String methodToMock(String item){
      return "asdf";    
    }
}

For whatever reason, though, running the following:

PowerMockito.spy(asdfClass.class);

PowerMockito.when(asdfClass.methodToMock(Mockito.any())).thenReturn("asdfghj");

appears to compile correctly but running

PowerMockito.spy(asdfClass.class);

PowerMockito.when(asdfClass.methodToMock(Mockito.any(), Mockito.any())).thenReturn("asdfghj");

does not and spits out a "'void' type not allowed here" error on the Mockito.any()s.

Does anyone know what to do about this? The only other result I saw on stackoverflow suggested that the reader take a look at the documentation, though I don't think it said anything about multiple arguments in a private static method.

(In the end I'm hoping to mock a void result with a doNothing though I've boiled the issue I'm having down to the fact that all of my void methods take multiple arguments)

EDIT: Never mind, got it: Is it possible to use partial mocking for private static methods in PowerMock? (Comment 4 on the chosen answer). Curiously this didn't work before but that might've been a typo on my part for all I know)

user3537932

As per Is it possible to use partial mocking for private static methods in PowerMock?, PowerMockito.doReturn(mockData).when(DataProvider.class, "readFile", param1, param2, ...) does the trick.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Mockito: argThat for methods taking multiple arguments

分類Dev

How to mock a Files static method with Mockito

分類Dev

Static method signature type arguments and partial application

分類Dev

unit test for private static method in non-static class

分類Dev

How to stub overloaded method in Mockito using Groovy?

分類Dev

How to stub overloaded method in Mockito using Groovy?

分類Dev

C# Using Multiple Arguments

分類Dev

Access method of private class inside static inner class from main()

分類Dev

Access method of private class inside static inner class from main()

分類Dev

Java Classloaders - Invoking a static method inside a private class

分類Dev

Workaround for accessing class type arguments in static method in Typescript

分類Dev

making a variable static private to each thread using openmp

分類Dev

How to fetch sms using static method

分類Dev

Accessing static method using null object

分類Dev

Initializing a static final int from static block or method and using it in annotations

分類Dev

Mockito and private field / injected bean

分類Dev

Is there a Necessity for private static Methods?

分類Dev

How to access private class's property from public static method in PHP

分類Dev

actionPerformed-methodでJFrameを閉じます(JFrameはprivate static void createGUI())

分類Dev

Chaining static .plugin() class method to extend instance APIs multiple times

分類Dev

Does repetitively calling a static method that enqueues a Retrofit network call maintain the value of the passed arguments correspondingly?

分類Dev

Private variables from arguments list

分類Dev

How to access a Static Class Private fields to unit test its methods using Microsoft Fakes in C#

分類Dev

`alias_method` a private method

分類Dev

Mocking private method with PowerMock

分類Dev

Matching parameters base on generic types with Mockito

分類Dev

Mockito : Testing boolean method with when()..then()

分類Dev

invoke the extension method of a class without using static class but by using class iteslf using reflection

分類Dev

Modify class method arguments

Related 関連記事

  1. 1

    Mockito: argThat for methods taking multiple arguments

  2. 2

    How to mock a Files static method with Mockito

  3. 3

    Static method signature type arguments and partial application

  4. 4

    unit test for private static method in non-static class

  5. 5

    How to stub overloaded method in Mockito using Groovy?

  6. 6

    How to stub overloaded method in Mockito using Groovy?

  7. 7

    C# Using Multiple Arguments

  8. 8

    Access method of private class inside static inner class from main()

  9. 9

    Access method of private class inside static inner class from main()

  10. 10

    Java Classloaders - Invoking a static method inside a private class

  11. 11

    Workaround for accessing class type arguments in static method in Typescript

  12. 12

    making a variable static private to each thread using openmp

  13. 13

    How to fetch sms using static method

  14. 14

    Accessing static method using null object

  15. 15

    Initializing a static final int from static block or method and using it in annotations

  16. 16

    Mockito and private field / injected bean

  17. 17

    Is there a Necessity for private static Methods?

  18. 18

    How to access private class's property from public static method in PHP

  19. 19

    actionPerformed-methodでJFrameを閉じます(JFrameはprivate static void createGUI())

  20. 20

    Chaining static .plugin() class method to extend instance APIs multiple times

  21. 21

    Does repetitively calling a static method that enqueues a Retrofit network call maintain the value of the passed arguments correspondingly?

  22. 22

    Private variables from arguments list

  23. 23

    How to access a Static Class Private fields to unit test its methods using Microsoft Fakes in C#

  24. 24

    `alias_method` a private method

  25. 25

    Mocking private method with PowerMock

  26. 26

    Matching parameters base on generic types with Mockito

  27. 27

    Mockito : Testing boolean method with when()..then()

  28. 28

    invoke the extension method of a class without using static class but by using class iteslf using reflection

  29. 29

    Modify class method arguments

ホットタグ

アーカイブ