jmockit MockUp <paths> mockit.internal.ClassFile $ NotFoundException

时间:2016-09-28 22:26:27

标签: junit4 jmockit

我有一个junit测试试图使用MockUp()和@Mock Path get(String pathStr)来伪造Paths.get(String pathStr),它返回一个@Mocked Path路径; 在JMockit 1.23和1.22中,这工作正常,但是从JMockit 1.24开始到1.28,它抛出了一个mockit.internal.ClassFile $ NotFoundException,它引用的类也是TestClassName $ 2

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

对JMockit的每次更新似乎都会删除功能,但我真的不知道这里的主要罪行是什么。

更新:我现在知道MockUp是一个红色鲱鱼,问题是Expectations块不是作为匿名内部类创建的。我不明白为什么。

更新: 重现的代码:

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))));
    }

}

1 个答案:

答案 0 :(得分:1)

这是一个更简单的复制器测试:

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

因此,使用Expectations无关紧要。实际的错误在于模仿File