PowerMock expectNew how to specify the type of the parameters

穿精又带淫゛_ 提交于 2019-12-08 07:20:38

问题


When I use PowerMock to mock the constructor,I want to specify the type of the paremeters. I use the method

PowerMock.expectNew(Class<T> type, Class<?>[] parameterTypes, Object... arguments)

If I want to specify the String type, what shoud I fill in the parameterTypes?


回答1:


Given these example classes:

public class MyClass {
    public MyClass(String theParam) {
        //Some interesting code...
    }
}

public class MyFactory {
    public MyClass createMyClass() {
        return new MyClass("foo");
    }
}

Then you would do an expectNew as follows:

public class MyFactoryTest {
    @Test
    public void testCreateMyClass() {
        //...

        PowerMock.expectNew(MyClass.class, new Class[] {String.class}, "foo");

        //...
    }
}


来源:https://stackoverflow.com/questions/18480796/powermock-expectnew-how-to-specify-the-type-of-the-parameters

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