Skip to content
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
charlesritchea opened this issue Sep 29, 2016 · 3 comments
Closed

ClassFile$NotFoundException Expectations in Junit4 test #350

charlesritchea opened this issue Sep 29, 2016 · 3 comments
Assignees
Labels

Comments

@charlesritchea
Copy link

charlesritchea 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?

@charlesritchea
Copy link
Author

The problem disapears if I remove the

@Mock File file

from the equation

@rliesenfeld rliesenfeld self-assigned this Sep 29, 2016
@rliesenfeld
Copy link
Member

Simpler reproducer test:

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

@charlesritchea
Copy link
Author

@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
Labels
Development

No branches or pull requests

2 participants