问题描述
我有一个带有一些静态方法的类,我需要模拟这些静态方法。我知道PowerMock可以做到这一点,但是我找不到能够对 Spock + PowerMock集成有所了解的任何教程/材料。我更喜欢Spock而不是Junit,因此遇到了难题。有没有办法让这两个框架发挥作用?任何帮助都值得赞赏。示例代码,甚至更多。
I have a class with a few static methods.I need to Mock these static methods. I know PowerMock does this,but I was not able to find any tutorials/materials that shed some light on "Spock+PowerMock" integration. I prefer Spock to Junit,hence the conundrum. Is there a way of getting these 2 frameworks to play ball?Any help is much appreciated.Sample code,even more so.
更新:该方法的现状
。
I was stuck here for a while too. After searching for hours, I saw this github repo: https://github.com/kriegaex/Spock_PowerMock.
我尝试添加PowerMockRule,这实际上使我能够将PowerMock与Spock一起使用。我必须添加这些依赖项。版本为1.5.4
I tried adding a PowerMockRule which essentially enabled me to use PowerMock together with Spock. I had to add these dependencies. Version is at 1.5.4
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
我的课程如下:
import org.junit.Rule
import org.mockito.Mockito
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.rule.PowerMockRule
import spock.lang.Specification
@PrepareForTest([SomeStaticClass.class])
public class FlightFormSpec extends Specification {
@Rule PowerMockRule powerMockRule = new PowerMockRule();
def "When mocking static"() {
setup :
PowerMockito.mockStatic(SomeStaticClass.class)
when :
Mockito.when(SomeStaticClass.someStaticMethod()).thenReturn("Philippines!");
then :
SomeStaticClass.someStaticMethod() == "Philippines!"
}
}
这里是另一种资源:
这篇关于将PowerMock与Spock结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!