11package com .baeldung .jtademo ;
22
33import com .baeldung .jtademo .dto .TransferLog ;
4- import com .baeldung .jtademo .services .BankAccountManualTxService ;
5- import com .baeldung .jtademo .services .TellerService ;
6- import com .baeldung .jtademo .services .TestHelper ;
4+ import com .baeldung .jtademo .services .*;
75import org .junit .Before ;
86import org .junit .Test ;
97import org .junit .runner .RunWith ;
@@ -25,7 +23,10 @@ public class JtaDemoUnitTest {
2523 TellerService tellerService ;
2624
2725 @ Autowired
28- BankAccountManualTxService bankAccountManualTxService ;
26+ BankAccountService accountService ;
27+
28+ @ Autowired
29+ AuditService auditService ;
2930
3031 @ Before
3132 public void beforeTest () throws Exception {
@@ -34,37 +35,53 @@ public void beforeTest() throws Exception {
3435 }
3536
3637 @ Test
37- public void whenNoException_thenAllCommitted () throws Exception {
38+ public void givenAnnotationTx_whenNoException_thenAllCommitted () throws Exception {
3839 tellerService .executeTransfer ("a0000001" , "a0000002" , BigDecimal .valueOf (500 ));
3940
40- BigDecimal result = testHelper .balanceOf ("a0000001" );
41- assertThat (testHelper .balanceOf ("a0000001" )).isEqualByComparingTo (BigDecimal .valueOf (500 ));
42- assertThat (testHelper .balanceOf ("a0000002" )).isEqualByComparingTo (BigDecimal .valueOf (2500 ));
41+ assertThat (accountService .balanceOf ("a0000001" )).isEqualByComparingTo (BigDecimal .valueOf (500 ));
42+ assertThat (accountService .balanceOf ("a0000002" )).isEqualByComparingTo (BigDecimal .valueOf (2500 ));
4343
44- TransferLog lastTransferLog = testHelper .lastTransferLog ();
44+ TransferLog lastTransferLog = auditService .lastTransferLog ();
4545 assertThat (lastTransferLog ).isNotNull ();
4646 assertThat (lastTransferLog .getFromAccountId ()).isEqualTo ("a0000001" );
4747 assertThat (lastTransferLog .getToAccountId ()).isEqualTo ("a0000002" );
4848 assertThat (lastTransferLog .getAmount ()).isEqualByComparingTo (BigDecimal .valueOf (500 ));
4949 }
5050
5151 @ Test
52- public void whenException_thenAllRolledBack () throws Exception {
52+ public void givenAnnotationTx_whenException_thenAllRolledBack () throws Exception {
5353 assertThatThrownBy (() -> {
54- tellerService .executeTransferFail ("a0000002" , "a0000001" , BigDecimal .valueOf (100 ));
55- }).hasMessage ("Something wrong, rollback!" );
54+ tellerService .executeTransfer ("a0000002" , "a0000001" , BigDecimal .valueOf (100000 ));
55+ }).hasMessage ("Insufficient fund." );
56+
57+ assertThat (accountService .balanceOf ("a0000001" )).isEqualByComparingTo (BigDecimal .valueOf (1000 ));
58+ assertThat (accountService .balanceOf ("a0000002" )).isEqualByComparingTo (BigDecimal .valueOf (2000 ));
59+ assertThat (auditService .lastTransferLog ()).isNull ();
60+ }
61+
62+ @ Test
63+ public void givenProgrammaticTx_whenCommit_thenAllCommitted () throws Exception {
64+ tellerService .executeTransferProgrammaticTx ("a0000001" , "a0000002" , BigDecimal .valueOf (500 ));
5665
57- assertThat (testHelper .balanceOf ("a0000001" )).isEqualByComparingTo (BigDecimal .valueOf (1000 ));
58- assertThat (testHelper .balanceOf ("a0000002" )).isEqualByComparingTo (BigDecimal .valueOf (2000 ));
66+ BigDecimal result = accountService .balanceOf ("a0000001" );
67+ assertThat (accountService .balanceOf ("a0000001" )).isEqualByComparingTo (BigDecimal .valueOf (500 ));
68+ assertThat (accountService .balanceOf ("a0000002" )).isEqualByComparingTo (BigDecimal .valueOf (2500 ));
5969
60- assertThat (testHelper .lastTransferLog ()).isNull ();
70+ TransferLog lastTransferLog = auditService .lastTransferLog ();
71+ assertThat (lastTransferLog ).isNotNull ();
72+ assertThat (lastTransferLog .getFromAccountId ()).isEqualTo ("a0000001" );
73+ assertThat (lastTransferLog .getToAccountId ()).isEqualTo ("a0000002" );
74+ assertThat (lastTransferLog .getAmount ()).isEqualByComparingTo (BigDecimal .valueOf (500 ));
6175 }
6276
6377 @ Test
64- public void givenBMT_whenNoException_thenAllCommitted () throws Exception {
65- bankAccountManualTxService .transfer ("a0000001" , "a0000002" , BigDecimal .valueOf (100 ));
78+ public void givenProgrammaticTx_whenRollback_thenAllRolledBack () throws Exception {
79+ assertThatThrownBy (() -> {
80+ tellerService .executeTransferProgrammaticTx ("a0000002" , "a0000001" , BigDecimal .valueOf (100000 ));
81+ }).hasMessage ("Insufficient fund." );
6682
67- assertThat (testHelper .balanceOf ("a0000001" )).isEqualByComparingTo (BigDecimal .valueOf (900 ));
68- assertThat (testHelper .balanceOf ("a0000002" )).isEqualByComparingTo (BigDecimal .valueOf (2100 ));
83+ assertThat (accountService .balanceOf ("a0000001" )).isEqualByComparingTo (BigDecimal .valueOf (1000 ));
84+ assertThat (accountService .balanceOf ("a0000002" )).isEqualByComparingTo (BigDecimal .valueOf (2000 ));
85+ assertThat (auditService .lastTransferLog ()).isNull ();
6986 }
7087}
0 commit comments