本文介绍了如何使用Mockito模拟外部方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
paymentBusinessService类在BusinessService类依赖项注入中可用.应用程序sc = Applicaition.applicationValidation(this.deal);应该是
应用程序sc = BusinessService.applicationValidation(this.deal);
paymentBusinessService class is avaiable in BusinessService class dependency injection. The Application sc = Applicaition.applicationValidation(this.deal); suppose to be
Application sc = BusinessService.applicationValidation(this.deal);
package com.core.business.service.dp.fulfillment;
import com.core.business.service.dp.payment.PaymentBusinessService;
public class BusinessServiceImpl implements BusinessService { // Actual Impl Class
private PaymentBusinessService paymentBusinessService = PluginSystem.INSTANCE.getPluginInjector().getInstance(PaymentBusinessService.class);
@Transactional( rollbackOn = Throwable.class)
public Application applicationValidation (final Deal deal) throws BasePersistenceException {
Application application = (Application) ApplicationDTOFactory.eINSTANCE.createApplication();
//External Call we want to Mock
String report = paymentBusinessService.checkForCreditCardReport(deal.getId());
if (report != null) {
application.settingSomething(true);
}
return application;
}
}
@Test(enabled = true)// Test Class
public void testReCalculatePrepaids() throws Exception {
PaymentBusinessService paymentBusinessService = mock(PaymentBusinessService.class);
//Mocking External Call
when(paymentBusinessService.checkForCreditCardReport(this.deal.getId())).thenReturn(new String("Decline by only Me"));
String report = paymentBusinessService.checkForCreditCardReport(this.deal.getId());
//Calling Impl Class whose one external call is mocked
Application sc = BusinessService.applicationValidation(this.deal);
}
推荐答案
感谢Aurand给了我将模拟对象注入应用程序类的想法.使用@InjectMock可以正常工作.
Thanks Aurand for giving me the idea of injecting mock object into application class. Using @InjectMock works fine.
@MOCK
PaymentBusinessService paymentBusinessService;
@InjectMock
Application application = PluginSystem.INSTANCE.getPluginInjector().getInstance(Application.class);
@Test(enabled = true)// Test Class
public void testReCalculatePrepaids() throws Exception {
//Mocking External Call
when(paymentBusinessService.checkForCreditCardReport(this.deal.getId())).thenReturn(new String("Decline by only Me"));
String report = paymentBusinessService.checkForCreditCardReport(this.deal.getId());
//Calling Impl Class whose one external call is mocked
Application sc = BusinessService.applicationValidation(this.deal);
这篇关于如何使用Mockito模拟外部方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!