2015-05-14 118 views
0

這裏的問題:測試抽象類

這是我正在嘗試測試用例的類。我試圖創建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) 

感謝您閱讀完所有的問題: - )希望已經夠清楚了。

+2

發生此異常的哪一行?你能附上完整的痕跡嗎? – kupsef

+1

並且是從數據庫返回的視圖null? –

+0

是的,我檢查這個視圖不是null,我甚至通過使用ViewEntryCollection獲取所有元素從視圖coll = view.getAllEntries(); int count1 = coll.getCount();我得到count1 = 2這是正確的。因此,視圖不爲空, –

回答

1

at de.bcode.domino.TestViewHandler.testHandleDoc(TestViewHandler.java:58)

正如你所看到的例外發生在第58行這可能是該viewHandler是空的,但我不能告訴它肯定,因爲你忽略大多數的文件。

(它甚至沒有58行)。

而且,看看this話題,你應該能夠通過檢查跟蹤調試這兩種錯誤的。它會告訴你在哪一行拋出了Exception。

+0

是的,我知道它是空的,因爲在java中我們不能實例化抽象類,在我的情況下,TestViewHandler已經擴展InitPropsAndLog和測試ViewHandler我必須創建它的對象,並使用他的方法進行測試,但不可能, –

1

該解決方案,我發現有了這個,我達到了我的目標,但沒有任何有效的:

這是類一定要考:

public abstract class ViewHandler 
    { 

     public boolean method 1 
     { 
      defined; 
     } 

     protected abstract boolean method 2; (not defined) 
    } 

這是我的測試類,但是,這exdending其他類 所以我創建了一個類,如臨時班。

class temp extends ViewHandler 
    { 
     @override 
     public boolean method 1 
     { 
       defined same as in ViewHandler to test it; 

     } 

     @override 
     protected abstract boolean method 2 
     { 

     } 

} 

現在測試方法1和方法2的viewhandler我做到了這一點。

class testviewHandler extends InitPropsAndLog 

    { 

    @Before 
    { 
    temp t = new temp(); 
    } 

     @Test 
     { 
      t.method1; // done testing by usning some asserts on it 

     } 



    } 

現在我沒有得到NullPointerExpetion,但有任何其他有效的方法來做到這一點。