3434class TransactionsTest
3535{
3636 private final TransactionsTestConfiguration configuration ;
37- private final AccountRepository repository ;
37+ private final AccountRepository accountRepository ;
38+ private final CounterRepository counterRepository ;
3839 private Account account1 ;
3940 private Account account2 ;
41+ private Counter counter1 ;
42+ private Counter counter2 ;
4043
4144 @ Autowired
42- public TransactionsTest (final TransactionsTestConfiguration configuration , final AccountRepository repository )
45+ public TransactionsTest (
46+ final TransactionsTestConfiguration configuration ,
47+ final AccountRepository accountRepository ,
48+ final CounterRepository counterRepository )
4349 {
4450 this .configuration = configuration ;
45- this .repository = repository ;
51+ this .accountRepository = accountRepository ;
52+ this .counterRepository = counterRepository ;
4653 }
4754
4855 @ BeforeEach
4956 void initData ()
5057 {
5158 this .account1 = new Account (1 , BigDecimal .TEN );
5259 this .account2 = new Account (2 , BigDecimal .ZERO );
53- this .repository .saveAll (List .of (this .account1 , this .account2 ));
60+ this .accountRepository .saveAll (List .of (this .account1 , this .account2 ));
61+
62+ this .counter1 = new Counter (1 , 10 );
63+ this .counter2 = new Counter (2 , 0 );
64+ this .counterRepository .saveAll (List .of (this .counter1 , this .counter2 ));
5465 }
5566
5667 @ Test
@@ -60,18 +71,116 @@ void accountTransaction_Working(@Autowired final PlatformTransactionManager tran
6071 status ->
6172 {
6273 this .account1 .setBalance (this .account1 .getBalance ().subtract (BigDecimal .ONE ));
63- this .repository .save (this .account1 );
74+ this .accountRepository .save (this .account1 );
6475
6576 this .account2 .setBalance (this .account2 .getBalance ().add (BigDecimal .ONE ));
66- this .repository .save (this .account2 );
77+ this .accountRepository .save (this .account2 );
78+ return null ;
79+ }
80+ );
81+
82+ Assertions .assertEquals (
83+ BigDecimal .valueOf (9 ),
84+ this .accountRepository .findById (this .account1 .getId ()).get ().getBalance ());
85+ Assertions .assertEquals (
86+ BigDecimal .ONE ,
87+ this .accountRepository .findById (this .account2 .getId ()).get ().getBalance ());
88+ }
89+
90+ @ Test
91+ void accountTransaction_ChangeAfterSave (@ Autowired final PlatformTransactionManager transactionManager )
92+ {
93+ new TransactionTemplate (transactionManager ).execute (
94+ status ->
95+ {
96+ this .account1 .setBalance (this .account1 .getBalance ().subtract (BigDecimal .ONE ));
97+ this .accountRepository .save (this .account1 );
98+ this .account1 .setBalance (this .account1 .getBalance ().subtract (BigDecimal .ONE ));
6799 return null ;
68100 }
69101 );
70102
71103 Assertions .assertEquals (
72104 BigDecimal .valueOf (9 ),
73- this .repository .findById (this .account1 .getId ()).get ().getBalance ());
74- Assertions .assertEquals (BigDecimal .ONE , this .repository .findById (this .account2 .getId ()).get ().getBalance ());
105+ this .accountRepository .findById (this .account1 .getId ()).get ().getBalance ());
106+ }
107+
108+ @ Test
109+ void accountAndCounterTransaction_Sequential (@ Autowired final PlatformTransactionManager transactionManager )
110+ {
111+ new TransactionTemplate (transactionManager ).execute (
112+ status ->
113+ {
114+ this .account1 .setBalance (this .account1 .getBalance ().subtract (BigDecimal .ONE ));
115+ this .accountRepository .save (this .account1 );
116+
117+ this .account2 .setBalance (this .account2 .getBalance ().add (BigDecimal .ONE ));
118+ this .accountRepository .save (this .account2 );
119+ return null ;
120+ }
121+ );
122+
123+ Assertions .assertEquals (
124+ BigDecimal .valueOf (9 ),
125+ this .accountRepository .findById (this .account1 .getId ()).get ().getBalance ());
126+ Assertions .assertEquals (
127+ BigDecimal .ONE ,
128+ this .accountRepository .findById (this .account2 .getId ()).get ().getBalance ());
129+
130+ new TransactionTemplate (transactionManager ).execute (
131+ status ->
132+ {
133+ this .counter1 .setCount (this .counter1 .getCount () - 1 );
134+ this .counterRepository .save (this .counter1 );
135+
136+ this .counter2 .setCount (this .counter2 .getCount () + 1 );
137+ this .counterRepository .save (this .counter2 );
138+ return null ;
139+ }
140+ );
141+
142+ Assertions .assertEquals (
143+ 9 ,
144+ this .counterRepository .findById (this .counter1 .getId ()).get ().getCount ());
145+ Assertions .assertEquals (
146+ 1 ,
147+ this .counterRepository .findById (this .counter2 .getId ()).get ().getCount ());
148+ }
149+
150+ @ Test
151+ void accountAndCounterTransaction_SameTransaction (@ Autowired final PlatformTransactionManager transactionManager )
152+ {
153+ new TransactionTemplate (transactionManager ).execute (
154+ status ->
155+ {
156+ this .account1 .setBalance (this .account1 .getBalance ().subtract (BigDecimal .ONE ));
157+ this .accountRepository .save (this .account1 );
158+
159+ this .account2 .setBalance (this .account2 .getBalance ().add (BigDecimal .ONE ));
160+ this .accountRepository .save (this .account2 );
161+
162+ this .counter1 .setCount (this .counter1 .getCount () - 1 );
163+ this .counterRepository .save (this .counter1 );
164+
165+ this .counter2 .setCount (this .counter2 .getCount () + 1 );
166+ this .counterRepository .save (this .counter2 );
167+
168+ return null ;
169+ }
170+ );
171+
172+ Assertions .assertEquals (
173+ BigDecimal .valueOf (9 ),
174+ this .accountRepository .findById (this .account1 .getId ()).get ().getBalance ());
175+ Assertions .assertEquals (
176+ BigDecimal .ONE ,
177+ this .accountRepository .findById (this .account2 .getId ()).get ().getBalance ());
178+ Assertions .assertEquals (
179+ 9 ,
180+ this .counterRepository .findById (this .counter1 .getId ()).get ().getCount ());
181+ Assertions .assertEquals (
182+ 1 ,
183+ this .counterRepository .findById (this .counter2 .getId ()).get ().getCount ());
75184 }
76185
77186 @ Test
@@ -82,14 +191,18 @@ void accountTransaction_UnexpectedError(@Autowired final PlatformTransactionMana
82191 status ->
83192 {
84193 this .account1 .setBalance (this .account1 .getBalance ().subtract (BigDecimal .ONE ));
85- this .repository .save (this .account1 );
194+ this .accountRepository .save (this .account1 );
86195
87196 throw new RuntimeException ("Unexpected error" );
88197 }
89198 ));
90199
91- Assertions .assertEquals (BigDecimal .TEN , this .repository .findById (this .account1 .getId ()).get ().getBalance ());
92- Assertions .assertEquals (BigDecimal .ZERO , this .repository .findById (this .account2 .getId ()).get ().getBalance ());
200+ Assertions .assertEquals (
201+ BigDecimal .TEN ,
202+ this .accountRepository .findById (this .account1 .getId ()).get ().getBalance ());
203+ Assertions .assertEquals (
204+ BigDecimal .ZERO ,
205+ this .accountRepository .findById (this .account2 .getId ()).get ().getBalance ());
93206 }
94207
95208 /**
@@ -102,9 +215,9 @@ void findStoredEntityWithinTransaction(@Autowired final PlatformTransactionManag
102215 status ->
103216 {
104217 final Account account3 = new Account (3 , BigDecimal .valueOf (100.0 ));
105- this .repository .save (account3 );
218+ this .accountRepository .save (account3 );
106219
107- Assertions .assertFalse (this .repository .findById (account3 .getId ()).isPresent ());
220+ Assertions .assertFalse (this .accountRepository .findById (account3 .getId ()).isPresent ());
108221 return null ;
109222 }
110223 );
0 commit comments