问题在这里:
这是我正在尝试测试用例的类。
在此,首先我尝试创建 ViewHandler(抽象类)的实例,我知道这是不可能的,但为了测试我必须使用 setView(View view)。
这是我的类(class):
public abstract class ViewHandler extends BOOLog {
protected View view;
protected ViewEntryCollection coll;
protected boolean stopNow = false;
private ViewEntry entry;
protected int position;
protected abstract boolean handleDoc(Document doc);
/**
* Start handling of view. Calls handleDoc for all documents.
*
* @return True, if view is set and all documents are handled ok.
*/
public boolean start() {
/ contain some code /
return ok;
}
protected int skip(int count) {
/*contain some code */
return count;
}
/**
* @param view
* The view to handle.
* @throws NotesException
* On any Notes error.
*/
public void setView(View view) throws NotesException {
this.view = view;
view.refresh();
this.coll = view.getAllEntries();
}
}
这是我的单元测试方法:
public class TestViewHandler extends InitPropsAndLog {
private static ViewHandler viewhandler;
private static StdLog log;
private static BOOSession ses;
protected View view;
@BeforeClass
public static void initSession() throws Exception {
assertTrue(Domino.reset());
DominoProps.initFromProperties();
log = new StdLog("TestViewHandler", null);
boolean l = Domino.init(log);
boolean l1 = DominoProps.isInitialized();
ses = (BOOSession) Domino.getSession();
}
/**
* Test method for
* {@link de.bcode.domino.ViewHandler#handleDoc(lotus.domino.Document)}.
*
* @throws Exception
*/
@Test
public void testHandleDoc() throws Exception {
System.out.println(ses.getServerName());
Database database = ses.getDatabase(ses.getServerName(),
"test_shree.nsf");
view = database.getView("form1");
if (view == null)
System.out.println("View is null");
viewHandler.setView(view);
viewhandler.start();
}
}
这是错误:
java.lang.NullPointerException
at de.bcode.domino.TestViewHandler.testHandleDoc(TestViewHandler.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
感谢您阅读所有问题:-)希望它足够清楚。
最佳答案
at de.bcode.domino.TestViewHandler.testHandleDoc(TestViewHandler.java:58)
正如您所看到的,异常发生在第 58 行。可能是 viewHandler
为 null,但我无法确定,因为您省略了文件的大部分内容。
(它甚至没有 58 行。)
此外,请查看 this主题,您应该能够通过检查跟踪来调试此类错误。它告诉您异常是在哪一行抛出的。
关于java - 测试抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30235656/