PowerMock是一个Java测试框架,它扩展了Mockito和EasyMock,可以用于模拟和存根静态方法、私有方法、构造函数和final类等。使用PowerMock来存根Instant对象的步骤如下:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
@RunWith(PowerMockRunner.class)
@PrepareForTest({YourClass.class}) // 需要存根的类
public class YourTestClass {
// 测试方法
}
@Test
public void testYourMethod() {
PowerMockito.mockStatic(YourClass.class);
// 存根静态方法的行为
YourClass yourClassMock = Mockito.mock(YourClass.class);
Mockito.when(yourClassMock.yourStaticMethod()).thenReturn(yourExpectedResult);
PowerMockito.when(YourClass.class, "yourStaticMethod").thenReturn(yourExpectedResult);
// 调用被测试方法
YourClass.yourStaticMethod();
// 断言结果
// ...
}
@Test
public void testYourMethod() throws Exception {
YourClass yourClassMock = Mockito.mock(YourClass.class);
PowerMockito.whenNew(YourClass.class).withNoArguments().thenReturn(yourClassMock);
// 调用被测试方法
YourClass instance = new YourClass();
// 断言结果
// ...
}
这样,你就可以使用PowerMock来存根Instant对象了。记得在测试类上使用PowerMockRunner运行器注解,并在需要存根的类上使用@PrepareForTest注解。通过PowerMockito.mockStatic()方法模拟静态方法,使用PowerMockito.when()方法定义存根的行为。如果需要存根构造函数,可以使用PowerMockito.whenNew()方法。
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.util.ReflectionTestUtils;
@RunWith(PowerMockRunner.class)
@PrepareForTest( { PaymentReconService.class })
@PowerMockIgnore(“org.apache.log4j.*”)
public class PaymentGatherServiceTest extends PaymentServiceTestBase {
@Autowired
private GatherService gatherResultService;
@Autowired
private PaymentBaseDAO baseDAO;
/**
* 测试正常postback
*/
public void testPaymentSucc() {
PaymentReconService mock = mock();
Long pbId = 10004L;
String pbStatus = PaymentBaseEO.PB_STATUS_GATHER_SUCC;
BigDecimal succAmount = new BigDecimal(“99.3”);
try {
GatherOrderRO ro = gatherResultService.processPaymentGather(pbId, pbStatus, succAmount, succAmount);
assertNotNull(ro);
} catch (SystemException e) {
fail(e.getLocalizedMessage());
} catch (BusinessException e) {
fail(e.getBusinessCode());
}
EasyMock.verify(mock);
}
/**
* MOCK PaymentReconService实现
* @return
*/
private PaymentReconService mock() {
PaymentReconRO mockRO = new PaymentReconRO(PaymentReconRO.Status.SUCESS, “OK”);
PaymentReconService mock = EasyMock.createMock(PaymentReconServiceImpl.class);
EasyMock.expect(mock.paymentSuccessRecon(EasyMock.anyObject(Long.class))).andReturn(mockRO);
EasyMock.replay(mock);
//这里把依赖的数据注进去
ReflectionTestUtils.s
领取专属 10元无门槛券
手把手带您无忧上云