I'm new to java and I'm confused about testing code with JUnit and Mockito.
I have forked a project on github where the task is to write Junit testing for
AccountService which is an interface. What I don't know is what do I have to test ? Interface or class that implement interface ?
Here is AccountService:
public interface AccountService {
public Operation deposit(String accountNumber, int amount);
public Operation withdraw(String accountNumber, int amount);
public OperationsDto history(String accountNumber);
}
Here is AccountService implementation
@Service
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountRepository accountRepository;
@Autowired
private OperationRepository operationRepository;
@Autowired
OperationConverter operationConverter;
public Operation deposit(String accountNumber, int amount) {
checkAmount (amount);
AccountEntity account = getAccount (accountNumber);
int balance = account.getBalance();
balance = balance + amount;
OperationEntity operation = new OperationEntity();
operation.setAccount(accountNumber);
operation.setAmount(amount);
operation.setType(OperationType.DEPOSIT.toString());
operation.setDate(DateUtil.getCurrentDate());
operation.setBalance(balance);
operationRepository.save(operation);
//update account balance
account.setBalance(balance);
accountRepository.save(account);
return operationConverter.convert(operation);
}
public Operation withdraw(String accountNumber, int amount) {
checkAmount (amount);
AccountEntity account = getAccount (accountNumber);
int balance = account.getBalance();
balance = balance - amount;
if(balance < 0){
throw new OperationException(Constants.ERROR_INVALID_OPERATION);
}
OperationEntity operation = new OperationEntity();
operation.setBalance(balance);
operation.setAccount(accountNumber);
operation.setAmount(amount);
operation.setType(OperationType.WITHDRAW.toString());
operation.setDate(DateUtil.getCurrentDate());
operationRepository.save(operation);
//update account balance
account.setBalance(balance);
accountRepository.save(account);
return operationConverter.convert(operation);
}
public OperationsDto history(String accountNumber) {
List<OperationEntity> operations =
operationRepository.findByAccount(accountNumber);
return operationConverter.convertList(operations);
}
private AccountEntity getAccount(String accountNumber){
AccountEntity account =
accountRepository.findByNumber(accountNumber);
if(account == null) {
throw new AccountException(Constants.ERROR_INVALID_ACCOUNT);
}
return account;
}
private void checkAmount (int amount) {
if(amount < 0){
throw new OperationException(Constants.ERROR_INVALID_OPERATION);
}
}
}