首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

如何使用powermock来存根Instant对象

PowerMock是一个Java测试框架,它扩展了Mockito和EasyMock,可以用于模拟和存根静态方法、私有方法、构造函数和final类等。使用PowerMock来存根Instant对象的步骤如下:

  1. 首先,确保你的项目中已经引入了PowerMock的依赖。可以在Maven或Gradle配置文件中添加以下依赖:
代码语言:txt
复制
<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>
  1. 在测试类上添加PowerMockRunner运行器注解,例如:
代码语言:txt
复制
@RunWith(PowerMockRunner.class)
@PrepareForTest({YourClass.class}) // 需要存根的类
public class YourTestClass {
    // 测试方法
}
  1. 在测试方法中,使用PowerMockito.mockStatic()方法来模拟静态方法的行为,例如:
代码语言:txt
复制
@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();
    
    // 断言结果
    // ...
}
  1. 如果需要存根构造函数,可以使用PowerMockito.whenNew()方法,例如:
代码语言:txt
复制
@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()方法。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

玩花招的PowerMock

当我们面对一个遗留系统时,常见的问题是没有测试。正如Michael Feathers在Working Effectively with Legacy Code一书中对“遗留代码”的定义。他将其简单归纳为“没有测试的代码”。真是太贴切了!正是因为没有测试,使得我们对遗留代码的任何重构都有些战战兢兢,甚至成为开发人员抵制重构的借口。从收益与成本的比例来看,对于这样的系统,我一贯认为不要盲目进行重构。因为重构的真正适用场景其实是发生在开发期间,而非维护期间。当然,提升自己的重构能力,尤其学会运用IDE提供的自动重构工具,可以在一定程度上保障重构的质量。然而,安全的做法,还是需要为其编写测试。

02

EasyMock PowerMock 的简单使用(with spring Autowired)


import java.math.BigDecimal;

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

03
领券