這裏的問題:測試抽象類
這是我正在嘗試測試用例的類。我試圖創建ViewHandler(抽象類)的實例,我知道哪些是不可能的但爲了測試我必須使用setView(視圖視圖)。
這裏是我的課:
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)
感謝您閱讀完所有的問題: - )希望已經夠清楚了。
發生此異常的哪一行?你能附上完整的痕跡嗎? – kupsef
並且是從數據庫返回的視圖null? –
是的,我檢查這個視圖不是null,我甚至通過使用ViewEntryCollection獲取所有元素從視圖coll = view.getAllEntries(); int count1 = coll.getCount();我得到count1 = 2這是正確的。因此,視圖不爲空, –