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

玩花招的PowerMock

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

02
您找到你想要的搜索结果了吗?
是的
没有找到

C++:无法解析的外部符号问题 与 头文件包含注意要点

由于种种原因,很长时间没有完整地编写一个C++程序。近期编写的程序都是简单地算法实现程序和简略的模拟程序,对于C++的许多特性都变得模糊不清。为了完成暑假的操作系统大作业——文件系统的模拟实现,从0开始写一个完成的程序。开始都进行得十分顺利,但编写完主要的头文件与cpp文件后,准备开始测试函数,进行Debug时,VS却提示大量错误信息,其中大都是:无法解析的外部符号。几天(暑假时间,不是没天都有大量时间认真编程,见笑了)时间过去后,尝试了多种解决方法终于找到了问题所在。于是有了写下搜寻过程的想法,要是有人能看这篇文章快速解决自己的问题,那就更好了。 结论:真正引起的错误的原因在于头文件的包含是否得当!

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
领券