0

paymentBusinessService 类在 BusinessService 类依赖注入中可用。应用程序 sc = Applicaition.applicationValidation(this.deal); 假设是
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);  

}

4

2 回答 2

0

感谢 Aurand 给了我将模拟对象注入应用程序类的想法。使用 @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);  
于 2013-01-17T16:40:12.860 回答
0

尚不清楚您的 Application 类如何引用 PaymentBusinessService。我猜你的应用程序类有一个对 PaymentBusinessService 的静态引用。在这种情况下,您必须确保引用指向您正在创建的模拟。

更新

鉴于您的 PaymentBusinessService 是一个未在构造函数中初始化的私有字段,您需要将模拟对象反射性地注入到您的 Application 类中。

于 2013-01-11T00:32:58.670 回答