Mocked file.getAbsolutePath() NullPointerException #376

Closed
novaterata opened this Issue Dec 23, 2016 · 4 comments

Projects

None yet

2 participants

@novaterata

Please provide the following information:

  • Version of JMockit that was used:

1.30

  • Description of the problem:
    Sorry this isn't fleshed out, but imagine you have an interface Bar that has a getFile method that is used to report an error with the file path in the error message. Basically, when file.getAbsolutePath() is called, the Expectation is completely ignored, so that the actual isAbsolutePath() code is run so that tons of stuff are null. I suspect this is more of these filtered out java.* classes which I simply don't understand.
    @Test
    public void shouldFoo(@Mocked Bar bar, @Mocked File file, @Mocked Path path) {
        String pathStr = "/some/test";

        new MockUp<Paths>(Paths.class) {
            @Mock
            Path get(String first, String... more) {
                return path;
            }
        };

        new MockUp<Files>(Files.class) {
            @Mock
            boolean exists(Path path, LinkOption... options) {
                return false;
            }
        };

        new Expectations() {{
            bar.getFile();
            result = file;
            file.getAbsolutePath();
            result = pathStr;
        }};

        assertThat(bar.getMessage(), is(equalTo("baz" + pathStr)));
    }
@novaterata
novaterata commented Dec 23, 2016 edited

I was able to workaround this using

new MockUp<File>(){
   @Mock  String getAbsolutePath(){
         return pathStr;
    }
};

But this still seems like a bug to me.

@rliesenfeld
Member

To avoid the problem, simply don't mock File or Path.

@rliesenfeld rliesenfeld self-assigned this Jan 3, 2017
@rliesenfeld rliesenfeld reopened this Jan 3, 2017
@rliesenfeld
Member

Mocking of "File#getAbsolutePath()" restored.

@rliesenfeld rliesenfeld closed this Jan 3, 2017
@novaterata

Thanks, I will encourage my devs to avoid injecting File and Path directly so stuff like this won't be such a blocker, but it's hard for a n00b to convince old-timers to do anything

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment