当我访问公共方法时,模拟对象工作正常。但是当我访问私有方法时它不起作用。
我的模拟课:
@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)
有什么建议吗?我怎样才能让它工作?
茅侃侃
慕标琳琳
相关分类