0

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

}

jan
  • 91
  • 3
  • 6
  • The code context is unclear here: for one, from your test excerpt, `applicationValidation` looks like it is a static method of class `Application` but the code above does not show that. Care to clear up? – fge Jan 11 '13 at 00:12
  • You have to use the mocked object in your `applicationValidation` method. The way it look now, you don't. Try changing the method to `applicationValidation(final Deal deal, PaymentBusinessService parmentBusinessService)` and send the mocked object as a parameter. – atomman Jan 11 '13 at 00:33

2 Answers2

0

It is not clear how your Application class references the PaymentBusinessService. I'm guessing that your Application class has a static reference to a 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.

Aurand
  • 5,487
  • 1
  • 25
  • 35
  • Sorry for mistake I have corrected it now and re-posted again. – jan Jan 11 '13 at 16:21
  • Thanks Aurand, paymentBusinessService.checkForCreditCardReport itself is a public method. And the call inside applicationValidation method goes through checkForCreditCardReport method. I can also see my mocked value in the test class if I make a direct call to String report = paymentBusinessService.checkForCreditCardReport(this.deal.getId()) Can you give me example of injecting the mock object into my appliation class – jan Jan 11 '13 at 20:01
0

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);  
jan
  • 91
  • 3
  • 6