@PrepareForTest的使用场景

本文介绍了如何使用 PowerMockito 的 whenNew 方法来模拟 Java 中的新对象创建过程,并详细解释了在不同情况下(如 final 方法、静态方法、私有方法及系统类的静态方法)进行模拟所需的注解配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

当使用PowerMockito.whenNew方法时,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是需要mock的new对象代码所在的类。


当需要mock final方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是final方法所在的类。 


当需要mock静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解@PrepareForTest里写的类是静态方法所在的类。


当需要mock私有方法的时候, 只是需要加注解@PrepareForTest,注解里写的类是私有方法所在的类


当需要mock系统类的静态方法的时候,必须加注解@PrepareForTest和@RunWith。注解里写的类是需要调用系统方法所在的类
@RunWith(PowerMockRunner.class) @PowerMockIgnore({"javax.management.*"}) @PrepareForTest({ DateAS.class, ExtractorFactory.class, BankSerialNumberGenerator.class, ServiceLocator.class, LocaleMessageUtil.class }) class QueryServiceOrderLogicTest { @Mock(answer = Answers.RETURNS_DEEP_STUBS) private OrderService orderService; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private ProductService productService; @Mock(answer = Answers.RETURNS_DEEP_STUBS) private FrameWorkContext frameWorkContext; @InjectMocks private QueryServiceOrderLogic queryServiceOrderLogic; private AutoCloseable mockitoCloseable; public QueryServiceOrderLogicTest() {} @BeforeEach void setUp() throws Exception { mockitoCloseable = MockitoAnnotations.openMocks(this); } @AfterEach void tearDown() throws Exception { mockitoCloseable.close(); } @Test void test_execute_should_return_not_null_when_condition() { List<ServiceOrderModel> serviceOrderModels = new ArrayList<>(); ServiceOrderModel serviceOrderModel = ServiceOrderModel.builder().initiatorIdentityType("string").identityType("string").status(ServiceOrderStatus.PENDING_VERIFICATION).build(); serviceOrderModels.add(serviceOrderModel); when(orderService.queryServiceOrderByIdentityId(anyString(), eq(null), eq(null))).thenReturn(serviceOrderModels); ServiceOrderModel serviceOrderModel1 = ServiceOrderModel.builder().initiatorIdentityType("string").identityType("string").status(ServiceOrderStatus.PENDING_VERIFICATION).build(); when(orderService.queryServiceOrderByOrderId(anyString())).thenReturn(serviceOrderModel1); SavingProduct savingProduct = new SavingProduct(); savingProduct.setProductName("string"); when(productService.getSavingProductByProductId(anyString())).thenReturn(savingProduct); BankingBusinessContext bankingBusinessContext = mock(BankingBusinessContext.class); Mockito.doReturn(buildQueryServiceOrderRequest()).when(bankingBusinessContext).getRequestData(); Mockito.doReturn(new QueryServiceOrderResponse()).when(bankingBusinessContext).getResult(QueryServiceOrderResponse.class); // run the test BusinessResult result = queryServiceOrderLogic.execute(bankingBusinessContext); // verify the results assertNotNull(result); } private QueryServiceOrderRequest buildQueryServiceOrderRequest() { QueryServiceOrderRequest request = new QueryServiceOrderRequest(); request.setServiceOrderId("test"); request.setIdentityId("test"); return request; } }
08-15
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值