powermockito无法模拟私有方法

iq3niunx  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(298)

我试着测试下面的方法 methodA ```
Public class SingleTonClass{

public JSONObject methodA(DBSession dbs, String processId, JSONObject workflow, JSONObject inputVar,JSONObject contextVar) throws Exception {
DBAdmin dba = dbs.getLocalDbAdmin();
Instance instance = methodB(dbs, processId, workflow, inputVar, contextVar);
//few code
return returnJo;
}
}

private Instance methodB(DBSession dbs, String processId, JSONObject workflow, JSONObject inputVar, JSONObject contextVar) throws Exception {

    DBAdmin dba = dbs.getLocalDbAdmin();
    //some code
    return methodC(dbs, process, inputVar, contextVar);
}
下面是我的测试方法

@RunWith(PowerMockRunner.class)
@PrepareForTest({ SingletonClass.class})
public class SingleTonClassTest extends TestCase {

SingleTonClass engineTest;

@Test
public void testMethodA() {
        engineTest = SingleTonClass.getInstance();
        DBSession mockSession = PowerMockito.mock(DBSession.class);
        DBAdmin mockDBA = PowerMockito.mock(DBAdmin.class);
        when(mockSession.getLocalDbAdmin()).thenReturn(mockDBA);
        SingleTonClass instance = SingleTonClass.getInstance();
        SingleTonClass spy = PowerMockito.spy(instance);
        PowerMockito.doReturn(new Instance()).when(spy, method(SingletonClass.class, "methodB",DBSession.class, String.class, JSONObject.class, JSONObject.class, JSONObject.class))
                .withArguments(any(DBSession.class),anyString(),any(JSONObject.class),any(JSONObject.class),any(JSONObject.class));         
        engineTest.methodA(mockSession, "hello", new JSONObject(), new JSONObject(), new JSONObject());
}

}

它在嘲笑methodb时总是抛出异常 `DBAdmin dba = dbs.getLocalDbAdmin();` 有人能告诉我是什么问题吗。因为我在嘲笑这个方法,所以它不应该执行这个方法。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题