亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將模擬注入私有方法?

如何將模擬注入私有方法?

慕尼黑8549860 2022-08-03 10:27:29
模擬對象在我訪問公共方法時工作正常。但是當我訪問私有方法時它不起作用。我的模擬課:@componentpublic class Test{public List<String> list(){ // some function}}我的主類:@componentpublic class Test2{private string method(String method){//here where i have to use mock object//some function}}我的測試用例:public class JunitTestCases{@MockTest test;@Autowired@InjectMocksTest2 test2public void Oncall{Test2 test=new Test2();Method method=Test2.class.getDeclaredMethod("method",String.class);method.setAccessible(true);method.invoke(test, "data");}}我得到以下錯誤。java.lang.reflect.InvocationTargetExceptionat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at com.TestCases.method(TestClass.java:198)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)at java.lang.reflect.Method.invoke(Unknown Source)at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)任何建議?我怎樣才能讓它工作?
查看完整描述

2 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

在調用方法中,你使用字符串.class[paramter]而不是Class[][parameterArray]。


無需為 Test2 創建其他對象.class已經使用過@injectMocks只需在 invoke 方法中使用變量即可。


public class JunitTestCases{


@Mock

Test test;


@InjectMocks

Test2 test2;


@Test

public void Oncall{


     MockitoAnnotations.initMocks(this);

      Class<?>[] params = new Class<?>[]{String.class};

      Method method=Test2.class.getDeclaredMethod("method",params);

       method.setAccessible(true);

       method.invoke(test2, "data");


    }

}


查看完整回答
反對 回復 2022-08-03
?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

用于編寫測試的演示類


public class PowerMockDemo {


    public Point callPrivateMethod() {

        return privateMethod(new Point(1, 1));

    }


    private Point privateMethod(Point point) {

        return new Point(point.getX() + 1, point.getY() + 1);

    }

}

演示類 的測試類


@RunWith(PowerMockRunner.class)

@PrepareForTest(PowerMockDemo.class)

public class PowerMockDemoTest {


    private PowerMockDemo powerMockDemoSpy;


    @Before

    public void setUp() {

        powerMockDemoSpy = PowerMockito.spy(new PowerMockDemo());

    }


    @Test

    public void testMockPrivateMethod() throws Exception {

        Point mockPoint = mock(Point.class);


        PowerMockito.doReturn(mockPoint)

            .when(powerMockDemoSpy, "privateMethod", anyObject());


        Point actualMockPoint = powerMockDemoSpy.callPrivateMethod();


        assertThat(actualMockPoint, is(mockPoint));

    }

}


查看完整回答
反對 回復 2022-08-03
  • 2 回答
  • 0 關注
  • 88 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號