ClassFile$NotFoundException Expectations in Junit4 test #350

Closed
novaterata opened this Issue Sep 29, 2016 · 3 comments

Projects

None yet

2 participants

@novaterata
novaterata commented Sep 29, 2016 edited

To Reproduce:

package foo;

import mockit.Expectations;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;
import org.junit.Test;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;

public class ReproJMockitBug {

    private Duration foo(File file, Path path, Duration duration) {
        final boolean exists = Files.exists(path);
        if (!exists && Paths.get("").equals(path)) {
            System.out.print(file.getAbsolutePath());
            return duration.plusMinutes(1);
        }
        return duration;
    }

    @Test
    public void shouldReturnErrorForInvalidFilePath(@Mocked Duration duration, @Mocked File file, @Mocked Path path) {
        String pathStr = "/some/test";

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

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

        new Expectations() {{
            onInstance(duration).plusMinutes(1);
            result = Duration.ofMinutes(3);
            onInstance(file).getAbsolutePath();
            result = pathStr;
        }};

        assertThat(foo(file, path, duration), is(equalTo(Duration.ofMinutes(3))));
    }

}

Basically I have a JUnit4 test with a couple of MockUps of Paths and Files followed by an Expectations{{ block. The anonymous inner class that is supposed to be the Expectations block is not found by the class loader; therefore, a ClassFile$NotFoundException is being thrown. I would at least expect an error explaining what was wrong with the Expectations block as each new release seems to introduce something else we are doing wrong.

It worked in 1.23 but not in 1.24-1.28

How would I even go about troubleshooting this?

@novaterata

The problem disapears if I remove the

@Mock File file

from the equation

@rliesenfeld rliesenfeld added the bug label Sep 29, 2016
@rliesenfeld rliesenfeld self-assigned this Sep 29, 2016
@rliesenfeld
Member

Simpler reproducer test:

    @Test
    public void issue350Reproducer(@Mocked File file) {
        new MockUp<Runnable>() {};
    }
@novaterata

@rliesenfeld I really appreciate how your agility on this issue

@rliesenfeld rliesenfeld added a commit that closed this issue Oct 1, 2016
@rliesenfeld rliesenfeld Black listed the constructors and some methods of the java.io.File cl…
…ass, in order to avoid class loading conflicts when mocking it; closes #350.
2c6c0a2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment