TestNG + Spring + Power mock 单元测试

7
我有一个基于Spring的应用程序,正在进行单元测试。我使用TestNG进行单元测试。在我的测试中,我需要使用PowerMockito来模拟一些静态方法。我还需要使用一个测试Spring配置文件仅针对我的单元测试。
我无法将所有三个内容组合编写我的单元测试,即TestNg、PowerMock和Spring。我可以通过扩展AbstractTestNGSpringContextTests类将TestNG和Spring结合起来,但无法模拟静态方法,而是执行实际的静态方法。就像以下内容:
@PrepareForTest(MyUtils.class)
@ContextConfiguration(locations = { "classpath:config/test-context.xml"})
public class MyImplTest extends AbstractTestNGSpringContextTests{

.....

}

我可以通过扩展PowerMockTestCase类将TestNG与PowerMockito结合使用。但是,测试Spring配置文件无法解析。就像下面这样:
@PrepareForTest(MyUtils.class)
@ContextConfiguration(locations = { "classpath:config/test-context.xml"})
public class MyImplTest extends PowerMockTestCase{

.....

}

有没有办法让我编写结合TestNG、PowerMockito和Spring上下文的单元测试?

1个回答

0
与其扩展PowerMockTestCase,不如尝试编写以下方法使用PowerMockObjectFactory。然后您可以扩展AbstractTestNGSpringContextTests
@ObjectFactory
public IObjectFactory getObjectFactory() {
    return new org.powermock.modules.testng.PowerMockObjectFactory();
}

这是由Powermock GitHub文档建议的。


有人解决了这个问题吗? - Sujeet

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接