New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocked file.getAbsolutePath() NullPointerException #376

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

Comments

2 participants
@novaterata

novaterata commented Dec 23, 2016

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

This comment has been minimized.

Show comment
Hide comment
@novaterata

novaterata Dec 23, 2016

I was able to workaround this using

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

But this still seems like a bug to me.

novaterata commented Dec 23, 2016

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

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld Dec 23, 2016

Member

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

Member

rliesenfeld commented Dec 23, 2016

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

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld Jan 3, 2017

Member

Mocking of "File#getAbsolutePath()" restored.

Member

rliesenfeld commented Jan 3, 2017

Mocking of "File#getAbsolutePath()" restored.

@rliesenfeld rliesenfeld closed this Jan 3, 2017

@novaterata

This comment has been minimized.

Show comment
Hide comment
@novaterata

novaterata Jan 3, 2017

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

novaterata commented Jan 3, 2017

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