测试抽象类

时间:2015-05-14 11:00:12

标签: java junit lotus-domino

问题在于:

这是我正在尝试测试用例的类。

在此,首先我尝试创建ViewHandler(抽象类)的实例我知道哪个不可能但是对于测试我必须使用setView(View视图)。

这是我的班级:

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 个答案:

答案 0 :(得分:1)

  

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

正如您所看到的,Exception发生在第58行。可能viewHandler为空,但由于您省略了大部分文件,我无法确定。

(它甚至没有58行。)

另外,看一下this主题,您应该能够通过检查跟踪来调试这类错误。它会告诉您抛出Exception的行。

答案 1 :(得分:1)

我发现这个解决方案我达到了我的目标但是有效率:

这是我必须测试的课程:

 public abstract class ViewHandler 
  {

      public boolean method 1 
        {
          defined;
        }

      protected abstract boolean method 2; (not defined) 
  } 

这是我的测试课,但是,它超过了其他一些课程  所以我创建了一个类似temp class的类。

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

         }

      @override 
      protected abstract boolean method 2
        {  

        }

}

现在测试viewhandler的方法1和方法2我做了这个。

  class testviewHandler extends InitPropsAndLog

  {  

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

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

        }



  } 

现在我没有得到NullPointerExpetion,但还有其他任何有效的方法可以实现这一点。