gpt4 book ai didi

java-8 - 使用 Java 8 的 Powermockito

转载 作者:行者123 更新时间:2023-12-02 05:20:39 28 4
gpt4 key购买 nike

我正在使用 powermockito 在 Java 7 中模拟带有参数的静态方法。最近开始迁移到 Java 8。迁移后,powermockito 停止模拟静态方法并开始调用原始方法。

Pom.xml

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule-agent</artifactId>
<version>1.6.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.2-GA</version>
<scope>test</scope>
</dependency>
<小时/>
Test class
using spring runner with PowerMockRule
@RunWith(SpringJUnit4ClassRunner.class)

@Rule
public PowerMockRule rule = new PowerMockRule();

Below is the code used for mock/stubbing the static..

PowerMockito.stub(PowerMockito.method(ABCHelper.class, "prepareResult",Arg1.class,Arg2.class,Arg3.class,Arg4.class)).toReturn(mockedReturnedObject);

有人可以帮我解决这个问题吗?

最佳答案

下面的代码对我有用。请在新项目中尝试使用以下依赖项和代码。

pom.xml

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>PowerMockTest</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4-rule</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-classloading-xstream</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.2</version>
<scope>test</scope>
</dependency>

</dependencies>


</project>

测试类

import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.when;

import org.junit.Rule;
import org.junit.Test;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;

@PrepareForTest({UtilTest.class})
public class PowerMockRuleTest {

@Rule
public PowerMockRule powerMockRule = new PowerMockRule();

@Test
public void testStatic() throws Exception{
PowerMockito.mockStatic(UtilTest.class);
when(UtilTest.getName()).thenReturn("1234");
System.out.println(UtilTest.getName());

Main mainMock = mock(Main.class);

PowerMockito.whenNew(Main.class).withAnyArguments().thenReturn(mainMock);
when(mainMock.getMyTest()).thenReturn("12345");
System.out.println(mainMock.getMyTest());
}

}

类(class):

public class UtilTest {

public static String getName() {

return "test";
}

}


public class Main {

public static void main(String[] args) {
System.out.println("Hello World!");
}


public String getMyTest() {
return "main test";
}
}

关于java-8 - 使用 Java 8 的 Powermockito,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39392770/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com