EasyMock PowerMock 的简单使用(with spring Autowired)
2010-11-05 19:21
399 查看
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.setField(unwrap(gatherResultService), "paymentReconService", mock); return mock; } @SuppressWarnings("unchecked") public static <T> T unwrap(T proxiedInstance) { if (proxiedInstance instanceof Advised) { try { return unwrap((T) ((Advised) proxiedInstance).getTargetSource().getTarget()); } catch (Exception e) { Assert.fail("对代理对象进行unwrap发生异常:" + proxiedInstance.getClass()); } } return proxiedInstance; } }
相关文章推荐
- 使用Spring2.5的Autowired实现注释型的IOC
- Spring、使用注解方式装配对象(@Resource、@Autowired)
- 使用Spring2.5的Autowired实现注释型的IOC
- [Spring Boot实战系列] - No.4 Spring boot 中如何在Utils类中使用@Autowired注入bean
- springboot使用aop拦截controller干一些事导致service们@Autowired全部注入失败
- Mockito与Spring中@Autowired与@InjectMocks组合
- spring问题:Error creating bean with name 'articleController': Injection of autowired dependencies fail
- Spring、使用注解方式装配对象(@Resource、@Autowired)
- 通过 @Autowired的使用来消除 set ,get方法 ---- Spring · JPA ·hibernate 的使用方法
- 单元测试及框架简介 --junit、jmock、mockito、powermock的简单使用
- Spring Injection with @Resource, @Autowired and @Inject
- Spring、使用注解方式装配对象(@Resource、@Autowired)
- 关于Struts在Spring中使用autowired 自动注入的问题
- Spring中使用@Autowired注解静态实例对象
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(三):通过 @Autowired的使用来消除 set ,get方法。
- spring给静态变量使用@Autowired注入
- 使用Spring2.5的Autowired实现注释型的IOC 161641
- 通过反射调用的类中使用Spring@Autowired自动装配的成员为空的解决办法不依赖servlet context获取Spring Application Contexts
- spring注入之使用标签 @Autowired @Qualifier
- spring boot 读取配置文件问题,不能使用@Autowired