Mocking a static method with generic parameter

天大地大妈咪最大 提交于 2019-12-25 12:03:32

问题


i am trying to mock a static method with sigature

 public static <T extends Object> T get( String name, Class<T> i )
  {
    return null
  }

and i am using PowerMockito, and below is my expected code

PowerMockito.when(ClassName.class, "get", "name", Class.class).thenReturn("Hi");

but its throwing

org.powermock.reflect.exceptions.MethodNotFoundException: No method found with name 'get' with parameter types: [ java.lang.String, java.lang.Class ]

Can anyone plz give the clue how to mock this using powermockito


回答1:


  1. You need to first tell Powermockito what class contains static methods that you want to mock by:

    PowerMockito.mockStatic(Classname.class);
    
  2. Then you can mock the method using:

    PowerMockito.when(Classname.get("name", Clas.class)).thenReturn("Hi");
    


来源:https://stackoverflow.com/questions/23702506/mocking-a-static-method-with-generic-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!