JUnit 测试收到 UnfinishedStubbingException
UnfinishedStubbingException received for JUnit test
您好,我正在尝试使用 Mockito 测试方法并收到 UnfinishedStubbingException。我是 Mockito 的新手,不确定我是否在做一些很棒的事情:)
这些是我的存根
@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
感谢任何帮助:)
你的问题是这一行。
when(preGeneratedAccountRepository.saveAndFlush(preGeneratedAccount));
您还没有指定调用此方法时希望 Mockito 执行的操作。
另请注意,您已在不需要时使用了两次 Answer
。您可以在每次使用 Answer
时使用 thenReturn
,例如,
when(preGeneratedAccountRepository.countByAcctNumAvailableFlag(Constants.FALSE)).thenReturn(pregeneratedAccountCount);
您好,我正在尝试使用 Mockito 测试方法并收到 UnfinishedStubbingException。我是 Mockito 的新手,不确定我是否在做一些很棒的事情:)
这些是我的存根
@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
感谢任何帮助:)
你的问题是这一行。
when(preGeneratedAccountRepository.saveAndFlush(preGeneratedAccount));
您还没有指定调用此方法时希望 Mockito 执行的操作。
另请注意,您已在不需要时使用了两次 Answer
。您可以在每次使用 Answer
时使用 thenReturn
,例如,
when(preGeneratedAccountRepository.countByAcctNumAvailableFlag(Constants.FALSE)).thenReturn(pregeneratedAccountCount);