How to mock the default constructor using PowerMock with Mockito?

二次信任 提交于 2019-12-11 19:04:28

问题


How should I mock the default constructor using PowerMock-Mockito(No EasyMock) ?

I want to access the values of the object by doing this.

For example :

Class A {

public A()
{
}

}

回答1:


PowerMockito.whenNew API should be used to perform this. See this link for further information: How to mock construction of new objects




回答2:


from docs

@RunWith(PowerMockRunner.class)
@PrepareForTest(X.class)
public class XTest {
        @Test
        public void test() {
                whenNew(MyClass.class).withNoArguments().thenThrow(new IOException("error message"));

                X x = new X();
                x.y(); // y is the method doing "new MyClass()"

                ..
        }
}


来源:https://stackoverflow.com/questions/15501572/how-to-mock-the-default-constructor-using-powermock-with-mockito

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