如何将模拟注入私有方法?

当我访问公共方法时,模拟对象工作正常。但是当我访问私有方法时它不起作用。


我的模拟课:


@component

public class Test{


public List<String> list(){

 // some function}

}

我的主要课程:


@component

public class Test2{

private string method(String method){

//here where i have to use mock object

//some function

}

}

我的测试用例:


public class JunitTestCases{


@Mock

Test test;


@Autowired

@InjectMocks

Test2 test2

public void Oncall{

Test2 test=new Test2();

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

method.setAccessible(true);

method.invoke(test, "data");}

}

我收到以下错误。


java.lang.reflect.InvocationTargetException

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

有什么建议吗?我怎样才能让它工作?


MMTTMM
浏览 95回答 2
2回答

茅侃侃

在调用方法中,您使用string.class[paramter]而不是 Class[][parameterArray]。无需为 Test2.class 创建另一个对象,您已使用 @injectMocks 只需使用调用方法中的变量即可。public class JunitTestCases{@MockTest test;@InjectMocksTest2 test2;@Testpublic void Oncall{&nbsp; &nbsp; &nbsp;MockitoAnnotations.initMocks(this);&nbsp; &nbsp; &nbsp; Class<?>[] params = new Class<?>[]{String.class};&nbsp; &nbsp; &nbsp; Method method=Test2.class.getDeclaredMethod("method",params);&nbsp; &nbsp; &nbsp; &nbsp;method.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp;method.invoke(test2, "data");&nbsp; &nbsp; }}

慕标琳琳

写作测试演示课程public class PowerMockDemo {&nbsp; &nbsp; public Point callPrivateMethod() {&nbsp; &nbsp; &nbsp; &nbsp; return privateMethod(new Point(1, 1));&nbsp; &nbsp; }&nbsp; &nbsp; private Point privateMethod(Point point) {&nbsp; &nbsp; &nbsp; &nbsp; return new Point(point.getX() + 1, point.getY() + 1);&nbsp; &nbsp; }}演示类的测试类@RunWith(PowerMockRunner.class)@PrepareForTest(PowerMockDemo.class)public class PowerMockDemoTest {&nbsp; &nbsp; private PowerMockDemo powerMockDemoSpy;&nbsp; &nbsp; @Before&nbsp; &nbsp; public void setUp() {&nbsp; &nbsp; &nbsp; &nbsp; powerMockDemoSpy = PowerMockito.spy(new PowerMockDemo());&nbsp; &nbsp; }&nbsp; &nbsp; @Test&nbsp; &nbsp; public void testMockPrivateMethod() throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; Point mockPoint = mock(Point.class);&nbsp; &nbsp; &nbsp; &nbsp; PowerMockito.doReturn(mockPoint)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .when(powerMockDemoSpy, "privateMethod", anyObject());&nbsp; &nbsp; &nbsp; &nbsp; Point actualMockPoint = powerMockDemoSpy.callPrivateMethod();&nbsp; &nbsp; &nbsp; &nbsp; assertThat(actualMockPoint, is(mockPoint));&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java