如何模拟私有静态属性,特别是针对ResourceBundle。

huangapple go评论74阅读模式
英文:

How to mock a private static property, specially for ResourceBundle

问题

我想模拟缺少 message.properties 文件的情况。

public class ClassToTest{
    private static ResourceBundle bundle = ResourceBundle.getBundle("message");
    public static String getMessage(String key){
        return bundle.getString(key);
    }
}

但是当我尝试以下代码时:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ResourceBundle.class)
public class ClassToTestTest{

    @Test
    public void myTest(){
        PowerMockito.spy(ResourceBundle.class);
        PowerMockito.when(ResourceBundle.class, "getBundle").thenThrow(new Exception("missing message.properties"));
    }

}

我还尝试了其他方法,但我总是得到 NoClassDefFoundError:org/mockito/exceptions/Reporter 的错误。

英文:

I want to mock the situation when missing message.properties.

public class ClassToTest{
    private static ResourceBundle bundle = ResourceBundle.getBundle("message");
    public static String getMessage(String key){
        return bundle.getString(key);
    }
}

But when I tried :

@RunWith(PowerMockRunner.class)
@PrepareForTest(ResourceBundle.class)
public class ClassToTestTest{

    @Test
    public void myTest(){
        PowerMockito.spy(ResourceBundle.class);
        PowerMockito.when(ResourceBundle.class, "getBundle").thenThrow(new Exception("missing message.properties"));
    }

}

I've also tried other ways, but I always get NoClassDefFoundError:org/mockito/exceptions/Reporter

答案1

得分: 0

以下是翻译好的代码部分:

使用 Mockito 3.4.x 来模拟静态方法:

public class ClassToTestTest {
    @Test
    public void test() {
        try (MockedStatic<ResourceBundle> dummyStatic = Mockito.mockStatic(ResourceBundle.class)) {
            dummyStatic.when(() -> ResourceBundle.getBundle(anyString()))
                       .thenThrow(new MissingResourceException("s", "className", "key"));
            // when
            try {
                var underTest = new ClassToTest();
            } catch (ExceptionInInitializerError ex) {
                var cause = ex.getCause();
                Assertions.assertEquals(MissingResourceException.class, cause);
            }
        }
    }
}

使用 PowerMock 进行相同的测试:

import org.junit.Assert;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.junit.Test;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ClassToTest.class})
public class ClassToTestTest {
    @Test
    public void myTest() {
        var exMessage = "m1";
        try {
            PowerMockito.mockStatic(ResourceBundle.class);
            PowerMockito.when(ResourceBundle.getBundle("message"))
                        .thenThrow(new MissingResourceException(exMessage, "className", "key"));
            var underTest = new ClassToTest();
        } catch (ExceptionInInitializerError ex) {
            Assert.assertEquals(MissingResourceException.class, ex.getCause().getClass());
            Assert.assertEquals(exMessage, ex.getCause().getMessage());
        }
    }
}

注意:翻译的内容已根据您的要求进行了提取,只包含代码部分。

英文:

I would go with the following (note - Mockito 3.4.x is needed to mock static methods)

public class ClassToTestTest {
    @Test
    public void test() {
        try (MockedStatic&lt;ResourceBundle&gt; dummyStatic = Mockito.mockStatic(ResourceBundle.class)) {
            dummyStatic.when(() -&gt; ResourceBundle.getBundle(anyString()))
                       .thenThrow(new MissingResourceException(&quot;s&quot;, &quot;className&quot;, &quot;key&quot;));
            // when
            try {
                var underTest = new ClassToTest();
            } catch (ExceptionInInitializerError ex) {
                var cause = ex.getCause();
                Assertions.assertEquals(MissingResourceException.class, cause);
            }
        }
    }
}

Same test with PowerMock:

import org.junit.Assert;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.junit.Test;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ClassToTest.class})
public class ClassToTestTest {
    @Test
    public void myTest() {
        var exMessage = &quot;m1&quot;;
        try {
            PowerMockito.mockStatic(ResourceBundle.class);
            PowerMockito.when(ResourceBundle.getBundle(&quot;message&quot;))
                        .thenThrow(new MissingResourceException(exMessage, &quot;className&quot;, &quot;key&quot;));
            var underTest = new ClassToTest();
        } catch (ExceptionInInitializerError ex) {
            Assert.assertEquals(MissingResourceException.class, ex.getCause().getClass());
            Assert.assertEquals(exMessage, ex.getCause().getMessage());
        }
    }
}

huangapple
  • 本文由 发表于 2020年8月17日 15:05:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63446110.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定