Testing Main Paths with System Tests
Learn how to test the main paths of your application with the help of system tests.
We'll cover the following...
On top of the pyramid are system tests. A system test starts up the whole application and runs requests against its
Send money system test
In a system test for the “Send Money” use case, we send an
Press + to interact
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)class SendMoneySystemTest {@Autowiredprivate TestRestTemplate restTemplate;@Test@Sql("SendMoneySystemTest.sql")void sendMoney() {Money initialSourceBalance = sourceAccount().calculateBalance();Money initialTargetBalance = targetAccount().calculateBalance();ResponseEntity response = whenSendMoney(sourceAccountId(),targetAccountId(),transferredAmount());then(response.getStatusCode()).isEqualTo(HttpStatus.OK);then(sourceAccount().calculateBalance()).isEqualTo(initialSourceBalance.minus(transferredAmount()));then(targetAccount().calculateBalance()).isEqualTo(initialTargetBalance.plus(transferredAmount()));}private ResponseEntity whenSendMoney(AccountId sourceAccountId,AccountId targetAccountId,Money amount) {HttpHeaders headers = new HttpHeaders();headers.add("Content-Type", "application/json");HttpEntity<Void> request = new HttpEntity<>(null, headers);return restTemplate.exchange("/accounts/sendMoney/{sourceAccountId}/{targetAccountId}/{amount}",HttpMethod.POST,request,Object.class,sourceAccountId.getValue(),targetAccountId.getValue(),amount.getAmount());}// some helper methods omitted}
The @SpringBootTest
annotation
With @SpringBootTest
, we’re telling Spring to start up the whole network of objects that
makes the application. We’re also ...