第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

MMTTMM 2022-07-27 21:58:03
當(dāng)我訪(fǎng)問(wèn)公共方法時(shí),模擬對(duì)象工作正常。但是當(dāng)我訪(fǎng)問(wèn)私有方法時(shí)它不起作用。我的模擬課:@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}}我的測(cè)試用例: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");}}我收到以下錯(cuò)誤。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)at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)有什么建議嗎?我怎樣才能讓它工作?
查看完整描述

2 回答

?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超22個(gè)贊

在調(diào)用方法中,您使用string.class[paramter]而不是 Class[][parameterArray]。


無(wú)需為 Test2.class 創(chuàng)建另一個(gè)對(duì)象,您已使用 @injectMocks 只需使用調(diào)用方法中的變量即可。


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");


    }

}


查看完整回答
反對(duì) 回復(fù) 2022-07-27
?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊

寫(xiě)作測(cè)試演示課程


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);

    }

}

演示類(lèi)的測(cè)試類(lèi)


@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));

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-07-27
  • 2 回答
  • 0 關(guān)注
  • 123 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)