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

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


我的模拟课:


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


任何建议?我怎样才能让它工作?



慕尼黑8549860
浏览 70回答 2
2回答

肥皂起泡泡

在调用方法中,你使用字符串.class[paramter]而不是Class[][parameterArray]。无需为 Test2 创建其他对象.class已经使用过@injectMocks只需在 invoke 方法中使用变量即可。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