0

Here the problem:

This is the class for which i am trying test case.

In this, First i tried to create instance of ViewHandler(abstract class) i know which is not Possible But for testing i have to use setView(View view).

Here is the my 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();
}

}

This is my unit test method:

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();



    }
 }

This the Error:

 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)

Thank you for reading all the question :-) Hope it is clear enough.

shreeram banne
  • 526
  • 4
  • 12
  • 2
    At which line did this Exception occurred? Could you attach the full trace? – kupsef May 14 '15 at 11:05
  • 1
    And is the view returned from the database null? – Bill the Lizard May 14 '15 at 11:09
  • yes i checked this view is not null, i even get all the element from the view by using ViewEntryCollection coll = view.getAllEntries(); int count1 = coll.getCount(); and i am getting count1 = 2 which is right. hence view is not null, – shreeram banne May 14 '15 at 11:35

2 Answers2

1

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

As you can see the Exception happened at line 58. It is probably that the viewHandler is null, but I can't tell it for sure since you omitted most of the file.

(It does not even have 58 lines.)

Also, have a look at this topic, you should be able to debug this kinds of mistakes by inspecting the trace. It tells you at which line the Exception was thrown.

Community
  • 1
  • 1
kupsef
  • 3,357
  • 1
  • 21
  • 31
  • yes i know it is empty because in java we cant Instantiate abstract class, in my case TestViewHandler already extending InitPropsAndLog and to test ViewHandler i must have to create its object and use his method for testing but not possible, – shreeram banne May 15 '15 at 10:39
1

This solution i found with this i reached to my goal but is there is any efficient :

This is the class i have to test:

 public abstract class ViewHandler 
  {

      public boolean method 1 
        {
          defined;
        }

      protected abstract boolean method 2; (not defined) 
  } 

This is my testing class but, which exdending some other class so i created one more class like 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
        {  

        }

}

Now to test method 1 and method 2 of viewhandler i done this.

  class testviewHandler extends InitPropsAndLog

  {  

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

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

        }



  } 

Now i am not getting NullPointerExpetion but there is any other efficient way to do achieve this.

shreeram banne
  • 526
  • 4
  • 12