问题
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:
You need to first tell Powermockito what class contains static methods that you want to mock by:
PowerMockito.mockStatic(Classname.class);
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