ClassFile$NotFoundException Expectations in Junit4 test #350

novaterata opened this Issue Sep 29, 2016 · 3 comments


None yet

2 participants

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.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;

import static;
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)) {
            return duration.plusMinutes(1);
        return duration;

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

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

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

        new Expectations() {{
            result = Duration.ofMinutes(3);
            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?


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

Simpler reproducer test:

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

@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 cl…
…ass, in order to avoid class loading conflicts when mocking it; closes #350.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment