我有两种方法在我的服务,这是findaccount和信用。credit方法在内部调用findaccount。我想管理的是验证在控制器测试中调用findaccount的次数
这是我的课
我的服务是:
@Service
public class AccountServiceImpl implements AccountService {
private static final String CLASS_NAME = AccountServiceImpl.class.getSimpleName();
Logger log = LoggerFactory.getLogger(this.getClass());
private final AccountRepository accountRepository;
@Autowired
public AccountServiceImpl(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
@Override
public TransactionStatus credit(String accountNumber, Transaction trx) {
log.info("{} -> credit -> accountNumber: {}, trx: {}",CLASS_NAME, accountNumber, trx);
Account account = findAccount(accountNumber);
trx.setAccount(account);
TransactionStatus trxStatus = new TransactionStatus();
try {
account.post(trx);
trxStatus.setApprovalCode(trx.getApprovalCode().toString());
} catch (InsufficientBalanceException e) {
log.error("InsufficientBalanceException is occured: {}", e.getMessage());
trxStatus.setStatus(BankingConstants.NOT_OK);
}
return trxStatus;
}
@Override
public TransactionStatus debit(String accountNumber, Transaction trx) throws InsufficientBalanceException {
log.info("{} -> debit -> accountNumber: {}, trx: {}",CLASS_NAME, accountNumber, trx);
Account account = findAccount(accountNumber);
trx.setAccount(account);
TransactionStatus trxStatus = new TransactionStatus();
account.post(trx);
return trxStatus;
}
@Override
public Account findAccount(String accountNumber) {
log.info("{} -> findAccount -> accountNumber: {}",CLASS_NAME, accountNumber);
return accountRepository.findByAccountNumber(accountNumber).orElseThrow(EntityNotFoundException::new);
}
}
控制器类:
@RestController
@RequestMapping("/account/v1")
public class AccountController {
private final AccountService accountService;
@Autowired
public AccountController(AccountService accountService) {
this.accountService = accountService;
}
@PostMapping(path = "/credit/{accountNumber}")
public ResponseEntity<TransactionStatus> credit(@PathVariable String accountNumber, @RequestBody DepositTransaction depositTransaction) throws InsufficientBalanceException {
TransactionStatus response = accountService.credit(accountNumber, depositTransaction);
return new ResponseEntity<>(response,HttpStatus.OK);
}
@PostMapping(path = "/debit/{accountNumber}")
public ResponseEntity<TransactionStatus> debit(@PathVariable String accountNumber, @RequestBody WithdrawalTransaction withdrawalTransaction) throws InsufficientBalanceException {
return new ResponseEntity<>(accountService.debit(accountNumber, withdrawalTransaction),HttpStatus.OK);
}
@GetMapping(path = "/{accountNumber}")
public ResponseEntity<Account> getAccount(@PathVariable String accountNumber) {
Account result = accountService.findAccount(accountNumber);
return new ResponseEntity<>(result,HttpStatus.OK);
}
}
下面是我的控制器测试类:
@SpringBootTest
@ContextConfiguration
@AutoConfigureMockMvc
class ControllerTests {
@Spy
@InjectMocks
private AccountController controller;
@Mock
private AccountService service;
@Test
public void givenId_Credit_thenReturnJson() throws Exception {
Account account = new Account("Kerem Karaca", "17892");
doReturn(account).when(service).findAccount("17892");
ResponseEntity<TransactionStatus> result = controller.credit("17892", new DepositTransaction(1000.0));
verify(service, times(1)).findAccount("17892");
assertEquals("OK", result.getBody().getStatus());
assertEquals(1000.0, account.getBalance(), 0.001);
}
}
1条答案
按热度按时间8yoxcaq71#
您在模拟服务,因此当控制器调用credit时,您没有执行该例程的实现。
在我看来,您想要的是为服务编写一个测试,模拟存储库,然后验证在调用服务的credit时,是否调用了repository findbyaccountnumber例程一次。