2013-01-14 2 views
1

에서 NULL을 반환, 내가 보고서의 조롱 값을보고 있지 않다 대신이 Null이고 내 테스트는 실패합니다. 테스트 클래스에서는 (보고서의) 조롱 된 값을 볼 수 있지만 BusinessServiceImpl 클래스에서는 볼 수 없으며 Application (Method Return)은 예상대로 수정되지 않습니다.외부 조롱 방법은 실제 클래스

내 기대 : Impl 클래스에서 External 호출을 조롱하면 조롱 된 값을 사용할 수 있어야하며 실제 테스트가 완료되면 단위 테스트를 완료하는 것처럼 나머지 작업을 수행해야합니다.

구현 코드 :

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); //report is Null and hence not reaching here 
     } 
     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()); 
    // Mocked value of report available here 
    //Calling Impl Class whose one external call is mocked 
    //Application is not modified as expected since report is Null in Impl class 
    Application sc = BusinessService.applicationValidation(this.deal); 
} 
+0

을 통과한다 제거 됐어? 모의 메서드를 두 번 호출한다는 주석을 받았지만 테스트 클래스에서 아래 코드 줄을 제거했지만 여전히 BusinessServiceImpl에서 Null이며 테스트가 여전히 실패했습니다. String report = paymentBusinessService.checkForCreditCardReport (this.deal.getId()); – Abhi

+0

당신은이 질문을하기 전에, 그리고 그 시간을 명시했습니다. 당신은 외부 클래스에서 조롱 된 객체를 사용하지 않습니다. 따라서'report'는 null이됩니다. 그러나 테스트의'report'가'null'이라면 전혀 알 수 없습니다. – atomman

+0

빠른 답장을 보내 주셔서 감사합니다. "Report"는 Test Class에서 null이 아니며 BusinessServiceImpl 클래스에서 null입니다. 테스트를 위해 외부 클래스를 수정해야한다면 (Mockito의 사용법은 무엇입니까?) 외부 호출을 조롱하는 대신 다음과 같이 직접 새로운 보고서 문자열을 만들고 단위 테스트를 완료 할 수 있습니다. . 귀하의 답을 오해 한 경우 알려주십시오. 또한 당신이 외부 클래스라고했을 때 BusinessServiceImpl이 아닌 PaymentBusinessService를 의미한다고 생각했습니다. – Abhi

답변

1

Mockito의 주요 목적은 검사를 분리하는 것이다. 마찬가지로, BusinessServiceImpl을 테스트 할 때 모든 종속성을 조롱해야합니다.

위의 예를 사용하여 수행하려는 작업입니다. 이제 을 조롱하여을 작동 시키려면 테스트하려는 클래스에 조롱 된 객체 (이 경우 BusinessServiceImpl)를 주입해야합니다. 이 일을

한 가지 방법은 클래스, dependency injection의 생성자에 의해 실행 종속을 전달하는 것입니다. 아니면 당신이 그것을 할 수있는 방법을 볼 수 Spring and ReflectionTestUtils.

0

나는 그것을 끝내고 난 성공적으로 BusinessServiceImpl 클래스를 만지지 않고 조롱 된 값을 얻을 수있어. 내가 수행 한 단계는 다음과 같습니다. 1. @Mock PaymentBusinessService paymentBusinessService = mock (PaymentBusinessService.class); 2. @InjectMocks private PaymentBusinessService paymentBusinessService = PluginSystem.INSTANCE.getPluginInjector(). getInstance (PaymentBusinessService.class); 다음

그리고

단순히 위의 테스트를 실행하고 나는 BusinessServiceImpl에서 "단지 나에 의해 쇠퇴"로 보고서의 값을 볼 수 있었다 나의 테스트 케이스가 내가 이전에받은 응답을 볼 수 없습니다입니다