英文:
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<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);
}
}
}
}
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 = "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());
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论