恒定场仅在Junit5下初始化一次

发布于 2025-02-07 19:00:30 字数 840 浏览 3 评论 0原文

我们正在迁移到Junit5并删除PowerMockito,因为Mockito现在支持Mockstatic。之前,PowerMockito提供了Whitebox.SetInternalState(..),使设置值最终字段(常数)
我有一个类似的课程

public class MyClass {
    
    private static final String myField = Props.getString("myField");
    
    public boolean myMethod() {
        //uses my field
    }
    
}

,我的测试看起来像这样的

class MyClassTest {

    @Test
    void myTest() {
        try (MockedStatic<Props> propsMockedStatic = Mockito.mock(Props.class)) {
            propsMockedStatic.when(() -> Props.getString("myField")).thenReturn("Hello!!");
            MyClass myClass = new MyClass();
            //more code
        }
    }
}

问题是,当我引入更多测试时,我会看到myfield在运行测试时仅一次模拟一次,尽管我每次都使用新实例。这会导致制动测试,因为在其他情况下,我希望myfield具有不同的值,但是当我单独运行它们时,一切都成功完成

We are migrating to JUnit5 and removing PowerMockito since Mockito now supports mockStatic. Before, PowerMockito offered Whitebox.setInternalState(..) making it possible to set values final fields (constants)
I have a class like

public class MyClass {
    
    private static final String myField = Props.getString("myField");
    
    public boolean myMethod() {
        //uses my field
    }
    
}

and my Test looks like this

class MyClassTest {

    @Test
    void myTest() {
        try (MockedStatic<Props> propsMockedStatic = Mockito.mock(Props.class)) {
            propsMockedStatic.when(() -> Props.getString("myField")).thenReturn("Hello!!");
            MyClass myClass = new MyClass();
            //more code
        }
    }
}

The problem is that when I introduce more tests I see that the myField gets mocked only once when I run the Tests although I use a new instance every time. This results in braking tests since on other cases I want myField to have different values but when I run them separately everything completes successfully

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情勿念 2025-02-14 19:00:30

我在堆栈溢出帖子上发现的临时解决方法是 >。它与JDK17,Junit5和Mockito 4.6一起使用。在这里回答,如果有人搜索同一问题,对我来说,花了很多时间才能碰到这个答案。这可能是暂时的,但就目前而言,它

可以综上所述,对我来说有用的是打开对某些软件包的访问,可以看出Bellow。要查找此处需要包含哪些软件包

test {
test.jvmArgs = ["--add-exports","java.base/jdk.internal.access=ALL-UNNAMED",
                "--add-exports","java.base/sun.security.jca=ALL-UNNAMED",
                "--add-exports","java.xml/jdk.xml.internal=ALL-UNNAMED",
                "--add-exports","java.base/sun.security.action=ALL-UNNAMED",
                "--add-opens","java.base/sun.security.util=ALL-UNNAMED",
                "--add-opens","java.base/sun.util.calendar=ALL-UNNAMED",
                "--add-opens","java.base/java.lang.reflect=ALL-UNNAMED",
                "--add-opens","java.base/java.security.cert=ALL-UNNAMED",
                "--add-opens","java.base/java.text=ALL-UNNAMED",
                "--add-opens","java.base/java.math=ALL-UNNAMED",
                "--add-opens","java.sql/java.sql=ALL-UNNAMED",
                "--add-opens","java.base/java.security=ALL-UNNAMED",
                "--add-opens","java.base/java.util.regex=ALL-UNNAMED",
                "--add-opens","java.base/java.util.stream=ALL-UNNAMED",
                "--add-opens","java.base/java.util.concurrent=ALL-UNNAMED",
                "--add-opens","java.base/java.time=ALL-UNNAMED",
                "--add-opens","java.base/java.io=ALL-UNNAMED"]
}

A temporary workaround that I found on a Stack overflow post is this one https://stackoverflow.com/a/56043252/17984773. It works with JDK17, Junit5, and Mockito 4.6. Answering that here in case someone searches for the same issue, for me it took a lot of time to bump into this answer. It may be temporary but for now it works

To sum up, what worked for me was opening access to some of the packages as can be seen bellow. To find exact what packages need to be included here, I just run the test each time and error by error I created this array

test {
test.jvmArgs = ["--add-exports","java.base/jdk.internal.access=ALL-UNNAMED",
                "--add-exports","java.base/sun.security.jca=ALL-UNNAMED",
                "--add-exports","java.xml/jdk.xml.internal=ALL-UNNAMED",
                "--add-exports","java.base/sun.security.action=ALL-UNNAMED",
                "--add-opens","java.base/sun.security.util=ALL-UNNAMED",
                "--add-opens","java.base/sun.util.calendar=ALL-UNNAMED",
                "--add-opens","java.base/java.lang.reflect=ALL-UNNAMED",
                "--add-opens","java.base/java.security.cert=ALL-UNNAMED",
                "--add-opens","java.base/java.text=ALL-UNNAMED",
                "--add-opens","java.base/java.math=ALL-UNNAMED",
                "--add-opens","java.sql/java.sql=ALL-UNNAMED",
                "--add-opens","java.base/java.security=ALL-UNNAMED",
                "--add-opens","java.base/java.util.regex=ALL-UNNAMED",
                "--add-opens","java.base/java.util.stream=ALL-UNNAMED",
                "--add-opens","java.base/java.util.concurrent=ALL-UNNAMED",
                "--add-opens","java.base/java.time=ALL-UNNAMED",
                "--add-opens","java.base/java.io=ALL-UNNAMED"]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文