1
こんにちは私はMockitoでメソッドをテストしようとしていて、UnfinishedStubbingExceptionを受け取りました。私は素晴らしい何かをやっている場合、私は:)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());
}
Mockitoに新しく確認されていませんしています
私のサービスでこの行に誤りがあります
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
:)
を
thenReturn
するたびに使用することができます。 – user2004685しかし、私はそれをデバッグするときにこの行を破るpreGeneratedAccountRepository.countByAcctNumAvailableFlag(Constants.FALSE);私のサービスで。参考までに、私はあなたが帰りに言った方法を試しましたが、同じエラーが発生しました。だから私はAnswerに変更して問題が解決するかどうか確認します。運がない場合@DavidWallace – arjun
(preGeneratedAccountRepository.saveAndFlush(preGeneratedAccount));これはデバッグ中に私のテストメソッド中に壊れているが、そうではありません。それはそこのステップを通過し、私のコントロールはサービスクラスメソッドに行き、そこで失敗しました – arjun