1

안녕하세요, Mockito를 사용하여 메서드를 테스트하고 UnfinishedStubbingException을 받았습니다. 내가 대단한 일을하고 있는지 확실 Mockito에 새로운 아니다 :JUnit 테스트에 대해 UnfinishedStubbingException이 수신되었습니다.

이 내 스텁

@InjectMocks 
Constants constants; 

@Mock 
PreGeneratedAccountRepository preGeneratedAccountRepository; 

@InjectMocks 
PopulateUnqiueAccountNumber populateUnqiueAccountNumber; 

@Before 
public void setUp() throws Exception { 
    MockitoAnnotations.initMocks(this); 
} 

내 시험 방법

@Test 
public void testCheckAvailableAccountNumbersForSuccess() throws Exception { 

    populateUnqiueAccountNumber.setMinCount(2); 
    populateUnqiueAccountNumber.setInsertRecordCount(2); 

    long preGeneratedRowCount = 1; 
    long preGeneratedAccountCount = 0; 
    long accountNum = 8609024563l; 

    PreGeneratedAccount preGeneratedAccount = new PreGeneratedAccount(); 

    when(preGeneratedAccountRepository.countByAcctNumAvailableFlag(Constants.FALSE)).thenAnswer(new Answer<Long>() { 
     @Override 
     public Long answer(InvocationOnMock invocation) throws Throwable {    
      return preGeneratedRowCount; 
     } 
    }); 
    when(preGeneratedAccountRepository.countByAccountNum(accountNum)).thenAnswer(new Answer<Long>() { 
     @Override 
     public Long answer(InvocationOnMock invocation) throws Throwable {    
      return preGeneratedAccountCount; 
     } 
    }); 
    when(preGeneratedAccountRepository.saveAndFlush(preGeneratedAccount)); 

    // call testing method 
    populateUnqiueAccountNumber.checkAvailableAccountNumbers(); 

    // Verify the mock calls 
    verify(preGeneratedAccountRepository, times(1)).countByAcctNumAvailableFlag(Constants.FALSE); 
    verify(preGeneratedAccountRepository, times(1)).countByAccountNum(accountNum); 
    verify(preGeneratedAccountRepository, times(1)).saveAndFlush(preGeneratedAccount); 

} 

내 서비스 방법입니다

@Scheduled(cron = "${app.config.cron.rate}") 
public void checkAvailableAccountNumbers() { 
    LOGGER.debug("Method execution started :: " + new Date()); 

    try { 
     long rowCount = preGeneratedAccountRepository.countByAcctNumAvailableFlag(Constants.FALSE); 
     LOGGER.debug("Available account numbers in Database" + rowCount); 

     int totalRecordInserted = 0; 

     if (rowCount < minCount) { 
      do { 
       int count = insertRecordCount - totalRecordInserted; 
       LOGGER.debug("Number of Record need to insert::" + count); 
       int recordInserted = insertAccountNumbers(count); 
       totalRecordInserted = totalRecordInserted + recordInserted; 
       LOGGER.debug("totalRecordInserted::" + totalRecordInserted); 
      } while (totalRecordInserted < insertRecordCount); 
     } 
    } 

    catch (DataAccessException e) { 
     LOGGER.error("An exception was encountered when inserting account numbers", e); 
     throw e; 
    } 
    LOGGER.debug("Method execution ended :: " + new Date()); 
} 

내 서비스에서이 줄에 오류가 발생했습니다.

preGeneratedAccountRepository.countByAcctNumAvailableFlag(Constants.FALSE); 

예외

Unfinished stubbing detected here: 
-> at com.cardinalhealth.chh.batch.PopulateUnqiueAccountNumberTest.testCheckAvailableAccountNumbersForSuccess(PopulateUnqiueAccountNumberTest.java:80) 

E.g. thenReturn() may be missing. 
Examples of correct stubbing: 
    when(mock.isOk()).thenReturn(true); 
    when(mock.isOk()).thenThrow(exception); 
    doThrow(exception).when(mock).someVoidMethod(); 
Hints: 
1. missing thenReturn() 
2. you are trying to stub a final method, you naughty developer! 
3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed 

은 어떤 도움에 감사드립니다 :)

답변

2

귀하의 문제가이 라인입니다.

when(preGeneratedAccountRepository.saveAndFlush(preGeneratedAccount)); 

이 메서드를 호출 할 때 Mockito에서 수행 할 작업을 지정하지 않았습니다.

필요하지 않은 경우 Answer을 두 번 사용했음을 유의하십시오. 당신은 또한, 나는 여기에`Constants`에`@ InjectMocks`의 필요성을 이해하지 않는, 예를 들어, 당신이 Answer을 사용했습니다

when(preGeneratedAccountRepository.countByAcctNumAvailableFlag(Constants.FALSE)).thenReturn(pregeneratedAccountCount); 
+0

thenReturn 때마다 사용할 수 있습니다. – user2004685

+0

하지만이 줄에서 디버깅 할 때 preGeneratedAccountRepository.countByAcctNumAvailableFlag (Constants.FALSE); 내 서비스에서. 참고로, 처음에 나는 당신이 반환을 위해 언급 한 방식을 시도했지만 같은 오류가 발생했습니다. 그래서 문제가 해결 되었는 지 확인하기 위해 Answer로 바 꾸었습니다. @DavidWallace – arjun

+0

(preGeneratedAccountRepository.saveAndFlush (preGeneratedAccount)); 이 다음 디버깅하는 동안 내 테스트 메서드를 오전 동안 휴식해야하지만 그것은 않습니다. 거기 계단을 지나고 제 통제가 서비스 클래스 방법으로 넘어 가서 거기에서 실패했습니다 – arjun