无法使用Mkdirs()调用Java void方法模拟创建目录

发布于 2025-02-08 11:20:45 字数 3247 浏览 1 评论 0原文

在尝试运行以下代码的测试时。下面我会详细介绍一些错误。

当我尝试运行测试时,我观察到它在我的代码投掷 IllegalArgumentException thrws thrws中的mkdirs()调用时观察到。

myclass.java

package com.javaeasily.demos.junit;

import java.util.ArrayList;

public class MyClass {
    private final NodeHelper nodeHelper;
    private static final ArrayList<String> ACTIVE_SERVICES_POST_RECONFIGURE = new ArrayList<>();

    // Only allow construction if number is greater than one
    MyClass() {
        ACTIVE_SERVICES_POST_RECONFIGURE.add("my-node-" + NodeUtils.getMyNode());
        nodeHelper = new NodeHelper();
    }

    public void reconfigureNode() {
        if (ACTIVE_SERVICES_POST_RECONFIGURE.isEmpty()) {
            return;
        }
        try {
            nodeHelper.createStagingDir();
        } catch (Exception e) {
            throw new RuntimeException("Cannot reconfigure node");
        }
    }
}


nodeutils.java

package com.javaeasily.demos.junit;


import org.apache.commons.lang3.StringUtils;

public class NodeUtils {
    private static final String HOSTNAME_PREFIX = "my-node-";

    public static String hostnameToNode(String hostname) {
        if (!hostname.startsWith(HOSTNAME_PREFIX)) {
            throw new IllegalArgumentException(hostname + " is not recognized hostname");
        }
        return StringUtils.removeStart(hostname, HOSTNAME_PREFIX);
    }

    public static String getHostname() {
        return System.getenv("HOSTNAME");
    }

    public static String getMyNode() {
        return hostnameToNode(getHostname());
    }
}


nodehelper.java

package com.javaeasily.demos.junit;

import java.io.File;

public class NodeHelper {
    private static final String STAGING_DIR = "/staging/";

    public void createStagingDir() {
        File stagingDir = new File(STAGING_DIR);
        if (!stagingDir.exists() && !stagingDir.mkdirs()) {
            throw new IllegalArgumentException("Could not create staging dir");
        }
    }
}

myclasstest.java

package com.javaeasily.demos.junit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class MyClassTest {
    private MyClass myclass;

    @BeforeEach
    public void SetUp() {
        try (MockedStatic<NodeUtils> nodeUtilsMockedStatic = Mockito.mockStatic(NodeUtils.class);) {
            nodeUtilsMockedStatic.when(NodeUtils::getMyNode).thenReturn("foo");
            myclass = new MyClass();
        }
    }

    @Test
    public void testReconfigureNode() {
        myclass.reconfigureNode();
    }
}

当我尝试进行测试时,我会关注错误:

java.lang.RuntimeException: Cannot reconfigure node

    at com.javaeasily.demos.junit.MyClass.reconfigureNode(MyClass.java:22)

邮政调试,我看到了:

java.lang.IllegalArgumentException: Could not create staging dir

有人在乎启发我关于我做错了什么吗?

While trying to run my test for the following code. I am getting some error detailed below.

I observed at the time it makes mkdirs() calls in my code throws IllegalArgumentException when I try to run my test.

MyClass.java

package com.javaeasily.demos.junit;

import java.util.ArrayList;

public class MyClass {
    private final NodeHelper nodeHelper;
    private static final ArrayList<String> ACTIVE_SERVICES_POST_RECONFIGURE = new ArrayList<>();

    // Only allow construction if number is greater than one
    MyClass() {
        ACTIVE_SERVICES_POST_RECONFIGURE.add("my-node-" + NodeUtils.getMyNode());
        nodeHelper = new NodeHelper();
    }

    public void reconfigureNode() {
        if (ACTIVE_SERVICES_POST_RECONFIGURE.isEmpty()) {
            return;
        }
        try {
            nodeHelper.createStagingDir();
        } catch (Exception e) {
            throw new RuntimeException("Cannot reconfigure node");
        }
    }
}


NodeUtils.java

package com.javaeasily.demos.junit;


import org.apache.commons.lang3.StringUtils;

public class NodeUtils {
    private static final String HOSTNAME_PREFIX = "my-node-";

    public static String hostnameToNode(String hostname) {
        if (!hostname.startsWith(HOSTNAME_PREFIX)) {
            throw new IllegalArgumentException(hostname + " is not recognized hostname");
        }
        return StringUtils.removeStart(hostname, HOSTNAME_PREFIX);
    }

    public static String getHostname() {
        return System.getenv("HOSTNAME");
    }

    public static String getMyNode() {
        return hostnameToNode(getHostname());
    }
}


NodeHelper.java

package com.javaeasily.demos.junit;

import java.io.File;

public class NodeHelper {
    private static final String STAGING_DIR = "/staging/";

    public void createStagingDir() {
        File stagingDir = new File(STAGING_DIR);
        if (!stagingDir.exists() && !stagingDir.mkdirs()) {
            throw new IllegalArgumentException("Could not create staging dir");
        }
    }
}

MyClassTest.java

package com.javaeasily.demos.junit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class MyClassTest {
    private MyClass myclass;

    @BeforeEach
    public void SetUp() {
        try (MockedStatic<NodeUtils> nodeUtilsMockedStatic = Mockito.mockStatic(NodeUtils.class);) {
            nodeUtilsMockedStatic.when(NodeUtils::getMyNode).thenReturn("foo");
            myclass = new MyClass();
        }
    }

    @Test
    public void testReconfigureNode() {
        myclass.reconfigureNode();
    }
}

When I try to run my test I get following error:

java.lang.RuntimeException: Cannot reconfigure node

    at com.javaeasily.demos.junit.MyClass.reconfigureNode(MyClass.java:22)

Post debugging I see that:

java.lang.IllegalArgumentException: Could not create staging dir

Does anyone care to enlighten me as to what I am doing wrong?

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

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

发布评论

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

评论(1

左秋 2025-02-15 11:20:45

要模拟NodeHelper您不应在myClass中创建它,而应通过构造函数从外部注入它。即,

MyClass(Nodehelper nodeHelper) {
    ACTIVE_SERVICES_POST_RECONFIGURE.add("my-node-" + NodeUtils.getMyNode());
    this.nodeHelper = nodeHelper;
}

这使您可以在测试中创建模拟的nodehelper,将其传递到myClass并将所需的行为设置为期望。

To mock NodeHelper you should not create it inside MyClass, but inject it from the outside via the constructor. i.e.

MyClass(Nodehelper nodeHelper) {
    ACTIVE_SERVICES_POST_RECONFIGURE.add("my-node-" + NodeUtils.getMyNode());
    this.nodeHelper = nodeHelper;
}

This allows you to create a mocked NodeHelper in your test, pass it to MyClass and set the desired behavior as expectations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文