powermockito无法模拟私有方法

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

我试着测试下面的方法 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 {

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

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

  1. SingleTonClass engineTest;
  2. @Test
  3. public void testMethodA() {
  4. engineTest = SingleTonClass.getInstance();
  5. DBSession mockSession = PowerMockito.mock(DBSession.class);
  6. DBAdmin mockDBA = PowerMockito.mock(DBAdmin.class);
  7. when(mockSession.getLocalDbAdmin()).thenReturn(mockDBA);
  8. SingleTonClass instance = SingleTonClass.getInstance();
  9. SingleTonClass spy = PowerMockito.spy(instance);
  10. PowerMockito.doReturn(new Instance()).when(spy, method(SingletonClass.class, "methodB",DBSession.class, String.class, JSONObject.class, JSONObject.class, JSONObject.class))
  11. .withArguments(any(DBSession.class),anyString(),any(JSONObject.class),any(JSONObject.class),any(JSONObject.class));
  12. engineTest.methodA(mockSession, "hello", new JSONObject(), new JSONObject(), new JSONObject());
  13. }

}

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

暂无答案!

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

相关问题