简体   繁体   English

如何使用Mockito模拟外部方法调用

[英]How do I mock external method call with Mockito

paymentBusinessService class is avaiable in BusinessService class dependency injection. paymentBusinessService类在BusinessService类依赖项注入中可用。 The Application sc = Applicaition.applicationValidation(this.deal); 应用程序sc = Applicaition.applicationValidation(this.deal); suppose to be 应该是
Application sc = BusinessService.applicationValidation(this.deal); 应用程序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);  

} }

It is not clear how your Application class references the PaymentBusinessService. 目前尚不清楚您的Application类如何引用PaymentBusinessService。 I'm guessing that your Application class has a static reference to a PaymentBusinessService. 我猜您的Application类具有对PaymentBusinessService的静态引用。 In that case, you must make sure that reference points to the mock you are creating. 在这种情况下,您必须确保引用指向您正在创建的模拟。

Update 更新资料

Given that your PaymentBusinessService is a private field which is not initialized in the constructor, you'll need to reflectively inject your mock object into your Application class. 假设您的PaymentBusinessService是一个私有字段,并且尚未在构造函数中初始化,则需要将模拟对象反射性地注入Application类。

Thanks Aurand for giving me the idea of injecting mock object into application class. 感谢Aurand给我的想法是将模拟对象注入到应用程序类中。 Using @InjectMock works fine. 使用@InjectMock可以正常工作。

@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);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM