您好,我是使用Mockito进行Groovy单元测试的新手。我试图弄清楚如何编写daoImpl calss的测试用例,而无需真正更新或插入数据库中。
下面是我的代码。
@Component
public class TransactionDAOImpl implements TransactionDAO {
@Autowired
StringUtilities stringUtilities;
@Autowired
private TransactionRepository transactionRespository;
@Override
@Transactional
public String create(List<DepositoryTransaction> depositoryTransaction) {
List<DepositoryTransaction> dep = transactionRespository.saveAll(depositoryTransaction);
LOGGER.debug("Recieved atm transaction : {} = {}", dep);
if (dep != null && !dep.isEmpty())
return stringUtilities.SUCCESS;
else
return stringUtilities.FAILURE;
}
}
答案 0 :(得分:0)
@RunWith(MockitoJUnitRunner.class) // org.mockito.runners.MockitoJUnitRunner is deprecated so use org.mockito.junit.MockitoJUnitRunner instead
public class TransactionDAOImplTest{
@InjectMocks
private TransactionDAOImpl transactionDAOImpl;
@Mock
StringUtilities stringUtilities;
@Mock
private TransactionRepository transactionRespository;
List<DepositoryTransaction> depositoryTransaction=new ArrayList<>();
@Test
public void testCreateSaveAllNullReturn(){
when(transactionRespository.saveAll(depositoryTransaction)).thenReturn(null);
assertThat(transactionDAOImpl.create(depositoryTransaction)).isEqualTo(stringUtilities.SUCCESS); // i do not khnow if this is an enum ?
}
@Test
public void testCreateSaveAllEmptyReturn(){
when(transactionRespository.saveAll(depositoryTransaction)).thenReturn(new ArrayList<>());
assertThat(transactionDAOImpl.create(depositoryTransaction)).isEqualTo(stringUtilities.SUCCESS); // i do not khnow if this is an enum ?
}
@Test
public void testCreateSaveAllNotEmptyAndNotNullReturn(){
DepositoryTransaction obj=new DepositoryTransaction();
depositoryTransaction.add(obj);
when(transactionRespository.saveAll(depositoryTransaction)).thenReturn(depositoryTransaction);
assertThat(transactionDAOImpl.create(depositoryTransaction)).isEqualTo(stringUtilities.FAILURE); // i do not khnow if this is an enum ?
}
}