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

ClassFile$NotFoundException Expectations in Junit4 test #350

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

Comments

2 participants
@novaterata

novaterata commented Sep 29, 2016

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

This comment has been minimized.

Show comment
Hide comment
@novaterata

novaterata Sep 29, 2016

The problem disapears if I remove the

@Mock File file

from the equation

novaterata commented Sep 29, 2016

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

This comment has been minimized.

Show comment
Hide comment
@rliesenfeld

rliesenfeld Sep 29, 2016

Member

Simpler reproducer test:

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

rliesenfeld commented Sep 29, 2016

Simpler reproducer test:

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

This comment has been minimized.

Show comment
Hide comment
@novaterata

novaterata Sep 30, 2016

@rliesenfeld I really appreciate how your agility on this issue

novaterata commented Sep 30, 2016

@rliesenfeld I really appreciate how your agility on this issue

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