3

so I'm trying to use Mockito on a method that has a static method in it. The reason is I cannot use PowerMock so I wrapped the method under non-static method.

public class WrapperUtil {

    public String getURLContent(String path) throws IOException{
        URL url = new URL(path);
        return IOUtils.toString(url);
    }
}

Now I tested the WrapperUtil class in two different ways. One test worked, but did not provide any coverage for WrapperUtil class, the other is throwing a null pointer exception related to the static method.

This is the one that works, but did not provide any coverage.

@RunWith(MockitoJUnitRunner.class)
public class WrapperUtilTest {

    @InjectMocks
    WrapperUtil ioutils;


    @Before
    public void setUp() throws Exception {

        ioutils = new WrapperUtil();
    }

    @Test
    public void testGetUrlContent() throws IOException {

        WrapperUtil ioutilsSpy = Mockito.spy(ioutils);
        Mockito.doReturn("test").when(ioutilsSpy).getURLContent(Mockito.anyString());
        assertTrue(ioutils2.getURLContent("test").contains("test"));

    }

}

This is the one that does not work:

@RunWith(MockitoJUnitRunner.class)
public class WrapperUtilTest {

    @InjectMocks
    WrapperUtil ioutils;


    @Before
    public void setUp() throws Exception {

        ioutils = new WrapperUtil();
    }

    @Test
    public void testGetUrlContent() throws IOException {

        WrapperUtil ioutilsSpy = Mockito.spy(ioutils);
        Mockito.when(ioutilsSpy).getURLContent(Mockito.anyString()).thenReturn("test");
        assertTrue(ioutils2.getURLContent("test").contains("test"));

    }

}

How can I make this work, and achieve code coverage without using PowerMockito? Thank you so much for your help.

E.D
  • 43
  • 7
  • Unrelated: you got typos in your code example. You declare `ioutils` - but then you use `ioutils2`. – GhostCat Jun 21 '17 at 06:58
  • Besides that little unclear-ness: great first question. I especially like your attitude to achieve high quality (although my answer goes in a different direction here) **and** your understanding that you want to avoid PowerMock. I wish I could upvote you three more times for this! – GhostCat Jun 21 '17 at 07:04
  • Finally, unrelated again: assuming you are using Apache IOUtils.toString() - please note that this method is *deprecated*, and that you should use the one taking an encoding instead! – GhostCat Jun 21 '17 at 07:06

1 Answers1

2

My two cent here:

  • I would even go one step further and define an interface to denote the functionality
  • On the other hand, I would not go "overboard" testing the wrapper implementation

Point is: there is just a tiny bit of glue code here. If you are able to test this code to verify that this glue code works - then you are fine.

In other words: avoid getting hung up on achieving 100% coverage! Coverage is a tool, designed to help you achieving code quality.

100% coverage does not lead to "100% code quality"!

You achieve code quality by trying to "do the right thing all the time".

Here, the "right thing" is not to strive for 100% coverage.

As I guess that you will not achieve that goal without turning to PowerMock(ito). And as avoiding PowerMock(ito) is by itself a good thing - my suggestion is: simply accept that you can't get to 100% coverage for this class.

If at all, I would spend my time trying to exclude this class from coverage runs.

GhostCat
  • 137,827
  • 25
  • 176
  • 248