java - 没有 @RunWith(PowerMockRunner.class) 的模拟静态

标签 java junit mocking mockito powermock

我有以下代码:

        @RunWith(PowerMockRunner.class)
        @PrepareForTest({RequestUtils.class, OsgiUtil.class})
        @PowerMockIgnore({"*"})
        public class MyTest
            ...
             @Test
             public somMethod(){    
                ....  
                mockStatic(RequestUtils.class);
                when(RequestUtils.getLocale(request)).thenReturn(locale);
            }
        }  

如何替换此代码,使其在没有 @RunWith(PowerMockRunner.class) 的情况下工作?

根据以下描述的原因link我不能使用 @RunWith(PowerMockRunner.class)

最佳答案

看看这个讨论:issues while using @RunWith Annotation and powerMock

其中一个答案建议使用 PowerMockRule 而不是 runner。这个解决方案应该对你有好处。

关于java - 没有 @RunWith(PowerMockRunner.class) 的模拟静态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24141580/

相关文章:

java - 从文件读取列表时发生类转换异常

java - 为 Postgres 安装 JDBC 时遇到问题

java - JUnit 测试结果在测试失败时显示 0,但在测试正常时显示 1

.net - 在测试期间使用模拟是否值得付出努力?

Java "too many constants"JVM 错误

java - 在简单的嵌入式数据库中要保持什么内容以及要重新创建什么内容?

java - junit 断言语句

eclipse - Selenium Webdriver JUNIT Maven Eclipse 运行测试用例失败

java - @InjectMocks 属性不能调用 Mockito 的方法 thenReturn 吗?

python - 如何模拟装饰函数