92 lines
3.8 KiB
Java
Raw Normal View History

2018-08-26 13:37:06 +07:00
package com.baeldung.jtademo;
import com.baeldung.jtademo.dto.TransferLog;
2018-08-29 14:51:21 +07:00
import com.baeldung.jtademo.services.AuditService;
import com.baeldung.jtademo.services.BankAccountService;
import com.baeldung.jtademo.services.TellerService;
import com.baeldung.jtademo.services.TestHelper;
2018-08-26 13:37:06 +07:00
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.math.BigDecimal;
2018-08-29 14:51:21 +07:00
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2018-08-26 13:37:06 +07:00
@RunWith(SpringRunner.class)
@SpringBootTest(classes = JtaDemoApplication.class)
public class JtaDemoUnitTest {
@Autowired
TestHelper testHelper;
@Autowired
TellerService tellerService;
@Autowired
BankAccountService accountService;
@Autowired
AuditService auditService;
2018-08-26 13:37:06 +07:00
@Before
public void beforeTest() throws Exception {
testHelper.runAuditDbInit();
testHelper.runAccountDbInit();
}
@Test
public void givenAnnotationTx_whenNoException_thenAllCommitted() throws Exception {
2018-08-26 13:37:06 +07:00
tellerService.executeTransfer("a0000001", "a0000002", BigDecimal.valueOf(500));
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500));
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500));
2018-08-26 13:37:06 +07:00
TransferLog lastTransferLog = auditService.lastTransferLog();
2018-08-26 13:37:06 +07:00
assertThat(lastTransferLog).isNotNull();
assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001");
assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002");
assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500));
}
@Test
public void givenAnnotationTx_whenException_thenAllRolledBack() throws Exception {
2018-08-26 13:37:06 +07:00
assertThatThrownBy(() -> {
tellerService.executeTransfer("a0000002", "a0000001", BigDecimal.valueOf(100000));
}).hasMessage("Insufficient fund.");
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000));
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000));
assertThat(auditService.lastTransferLog()).isNull();
}
@Test
public void givenProgrammaticTx_whenCommit_thenAllCommitted() throws Exception {
tellerService.executeTransferProgrammaticTx("a0000001", "a0000002", BigDecimal.valueOf(500));
2018-08-26 13:37:06 +07:00
BigDecimal result = accountService.balanceOf("a0000001");
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(500));
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2500));
2018-08-26 13:37:06 +07:00
TransferLog lastTransferLog = auditService.lastTransferLog();
assertThat(lastTransferLog).isNotNull();
assertThat(lastTransferLog.getFromAccountId()).isEqualTo("a0000001");
assertThat(lastTransferLog.getToAccountId()).isEqualTo("a0000002");
assertThat(lastTransferLog.getAmount()).isEqualByComparingTo(BigDecimal.valueOf(500));
2018-08-26 13:37:06 +07:00
}
@Test
public void givenProgrammaticTx_whenRollback_thenAllRolledBack() throws Exception {
assertThatThrownBy(() -> {
tellerService.executeTransferProgrammaticTx("a0000002", "a0000001", BigDecimal.valueOf(100000));
}).hasMessage("Insufficient fund.");
2018-08-26 13:37:06 +07:00
assertThat(accountService.balanceOf("a0000001")).isEqualByComparingTo(BigDecimal.valueOf(1000));
assertThat(accountService.balanceOf("a0000002")).isEqualByComparingTo(BigDecimal.valueOf(2000));
assertThat(auditService.lastTransferLog()).isNull();
2018-08-26 13:37:06 +07:00
}
}