에서 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);
}
을 통과한다 제거 됐어? 모의 메서드를 두 번 호출한다는 주석을 받았지만 테스트 클래스에서 아래 코드 줄을 제거했지만 여전히 BusinessServiceImpl에서 Null이며 테스트가 여전히 실패했습니다. String report = paymentBusinessService.checkForCreditCardReport (this.deal.getId()); – Abhi
당신은이 질문을하기 전에, 그리고 그 시간을 명시했습니다. 당신은 외부 클래스에서 조롱 된 객체를 사용하지 않습니다. 따라서'report'는 null이됩니다. 그러나 테스트의'report'가'null'이라면 전혀 알 수 없습니다. – atomman
빠른 답장을 보내 주셔서 감사합니다. "Report"는 Test Class에서 null이 아니며 BusinessServiceImpl 클래스에서 null입니다. 테스트를 위해 외부 클래스를 수정해야한다면 (Mockito의 사용법은 무엇입니까?) 외부 호출을 조롱하는 대신 다음과 같이 직접 새로운 보고서 문자열을 만들고 단위 테스트를 완료 할 수 있습니다. . 귀하의 답을 오해 한 경우 알려주십시오. 또한 당신이 외부 클래스라고했을 때 BusinessServiceImpl이 아닌 PaymentBusinessService를 의미한다고 생각했습니다. – Abhi