Mocked file.getAbsolutePath() NullPointerException #376

novaterata opened this Issue Dec 23, 2016 · 4 comments


None yet

2 participants


Please provide the following information:

  • Version of JMockit that was used:


  • 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.
    public void shouldFoo(@Mocked Bar bar, @Mocked File file, @Mocked Path path) {
        String pathStr = "/some/test";

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

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

        new Expectations() {{
            result = file;
            result = pathStr;

        assertThat(bar.getMessage(), is(equalTo("baz" + pathStr)));
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.


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

Mocking of "File#getAbsolutePath()" restored.

@rliesenfeld rliesenfeld closed this 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