0

我目前正在尝试将项目迁移到 Java 13(包括module-info.java这对问题很重要)、JUnit 5、最新的 Mockito 版本 3.3.3 和 Maven。

但是当我使用 Maven 运行测试时,使用注入模拟显然@Mock不起作用。

我试图从一个界面创建一个模拟实例。我的测试如下所示:

@ExtendWith(MockitoExtension.class)
public class MockitoTest {
    @Mock
    protected MyInterface myMock;
    @Test
    public void shouldMock() {
      // Do something
    }
}

错误如下:

[INFO] Running com.github.schuettec.mocktest.MockitoTest
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.416 s <<< FAILURE! - in com.github.schuettec.mocktest.MockitoTest
[ERROR] com.github.schuettec.mocktest.MockitoTest.shouldMock Time elapsed: 0.401 s  <<< ERROR!
org.mockito.exceptions.base.MockitoException: Problems setting field myMock annotated with @org.mockito.Mock(name="", stubOnly=false, extraInterfaces={}, answer=RETURNS_DEFAULTS, serializable=false, lenient=false)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field protected com.github.schuettec.mocktest.MyInterface  com.github.schuettec.mocktest.MockitoTest.myMock accessible: module mocktest does not "opens com.github.schuettec.mocktest" to unnamed module @6b9ce1bf

[INFO] 
[INFO] Results:
[INFO] 
[ERROR] Errors: 
[ERROR]   MockitoTest.shouldMockBothWithoutOpens » Mockito Problems setting field innerM...
[INFO] 
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

我在 JDK 13 上运行,所以我添加了一个module-info.java. 这很简单:

module mocktest {
    exports com.github.schuettec.mocktest;
}

当我添加open module测试工作正常。但我不想声明我的模块为反射打开。

我错过了什么吗?

如果你想看看,我上传了一个最小的例子:https ://github.com/schuettec/junit5-java13-test

构建日志在这里:https ://travis-ci.org/github/schuettec/junit5-java13-test/builds/683407777

4

0 回答 0