我有一个叫价与构造类,我动态通过反射加载:如何嘲笑的动态加载的罐子方法
public Price(Context context, String pair) {
this.context = context;
this.value1 = pair.substring(0, 3);
this.value2 = pair.substring(3, 6);
this.dps = context.getService().getm1(value1, value2).getm2();
}
但是我想嘲笑上下文对象
,我想
context.getService().getm1(value1, value2).getm2()
返回5
这是我曾尝试
//mocking the Context class
Class<?> contextClass = urlClassLoader.loadClass("com.algo.Context");
constructor =contextClass.getConstructor();
Object context = Mockito.mock(contextClass);
//trying to instantiate the Price class
Class<?> priceClass = urlClassLoader.loadClass("com.algo.Price");
constructor = priceClass.getConstructor(contextClass,String.class);
Mockito.when(context.getService().getm1(value1, value2).getm2().thenReturn(5));
Object price = constructor.newInstance(context,"PRICES");
不过我下
context.getService()
红线错误说
The method getService() is undefined for the type Object
我怎样才能解决这个搞定了,我的最终目标是与变量建立价格目标
dps
是一个int 5,这就是为什么我想模拟上下文对象。
上下文是'Object'类型 - 只是施放它。顺便说一句:你在谈论编译器错误 – home
这是一个编译错误,铸造不解决它 – user3809938
为什么你的'上下文'变量的类型'对象',而不是'上下文' – sidgate