我试图模拟一个可运行对象,该对象接收主对象的引用并尝试获取一些将在主对象中初始化的参数。但是,我得到了空指针异常。
我试图将hadoop配置对象传递给我计划访问的嵌套对象。我还有其他错误,我相信它与整个模拟概念背道而驰。
ClassOutside.java
public ClassOutside implements runnable {
protected ClassInside opts;
public setOpts(ClassInside opts) {
this.opts = opts;
}
public getOpts() {
return this.opts;
}
@Override
public void run() {
executorService = Executors.newFixedThreadPool(getOpts().getNumThreads);
executorService.run(new ClassDoSomething(this, arg1, arg2));
}
}
ClassDoSomething.java
public ClassDoSomething implements runnable() {
String arg1;
String arg2;
String arg3;
public ClassDoSomething(final ClassOutside classOutside, final String arg1, final String arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
arg3 = classOutside.getOpts().outputDir;
}
}
ClassDoSomethingTest.java
public ClassDoSomethingTest {
ClassOutside classOutside;
@Before
public void setUp() {
classOutside = mock(ClassOutside.class);
}
@Test
public void testClassDoSomethingFunctionality() {
when(classOutside.getOpts().outputDir).thenReturn("/input");
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(classOutside, arg1, arg2);
}
我希望when(..)。thenReturn()仅返回“ / input”,但是由于我正在尝试访问Object的变量variable,所以它给出了NullPointerException 我不确定如何模拟这种情况