Mocked object is assigned null value from method call inside method under test

1,947 views
Skip to first unread message

manish...@gmail.com

unread,
Aug 4, 2014, 10:13:23 AM8/4/14
to powe...@googlegroups.com
I have a class that I need to test with mockito.
Below is the class and the Mockito Test.

dbBuilder.parse(file) always returns null because it calls several classes which in turn calls several methods in a jar file.Even though i mocked all of them it always returns null.I couldn't track where the null value is coming from.
I tried to suppress the methods but still no use.Since  this method call returns null, the doc value is null.
And the doc calls getElementsByTagName method and running the mockito tests fails with null pointer exception.There are several lines of code  after this code in this method i need to test.
I used spy and mock to test.Still null value is returned.Is there a way I can skip this particular line of code and still keep the mocked doc value not assigned a null value?
How do I get around this issue?



class document{

public void docMethod(){

   DocumentBuilder dbBuilder= new DocumentBuilder();

   Document doc=new Document();

   FileStream file= new FileStream(new File(some path));

   doc= dbBuilder.parse(file);

  NodeList nodes = doc.getElementsByTagName("documents");

}

}

@Test
public documentTest(){

   DocumentBuilder dbBuilder= mock(DocumentBuilder.class);

   Document doc=mock(Document.class);
FileStream file=PowerMockito.mock(FileStream.class);

PowerMockito.whenNew(FileStream.class).withAnyArguments().thenReturn(file);

PowerMockito.doReturn(doc).when(dbBuilder).parse(file);




}

KARR, DAVID

unread,
Aug 9, 2014, 1:10:15 AM8/9/14
to powe...@googlegroups.com

Test methods typically have three conceptual sections.  First a “setup” section where you wire your mocks and other test data, then an “execute” section where you actually execute your code under test, and then a “verify” section, where you assert the side effects of the CUT execution.

 

Your test method appears to only have the “setup” section.  You’re wiring your mocks, but you never actually run anything that needs to be tested.

 

I would hope that this is just a mistake in what you posted here.

 

In any case, if you don’t understand what is happening in your test, you should step through the code of the test and CUT in your debugger (Eclipse, Netbeans, et cetera).  That should help you understand what code is being executed, and where you are getting values from.

 

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages