|
19 | 19 | import java.util.List; |
20 | 20 |
|
21 | 21 | import org.junit.jupiter.api.Assertions; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
22 | 23 | import org.junit.jupiter.api.Test; |
23 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
24 | 25 | import org.springframework.test.context.ContextConfiguration; |
25 | 26 | import org.springframework.transaction.PlatformTransactionManager; |
| 27 | +import org.springframework.transaction.annotation.Transactional; |
26 | 28 | import org.springframework.transaction.support.TransactionTemplate; |
27 | 29 |
|
28 | 30 | import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations; |
|
33 | 35 | class TransactionsTest |
34 | 36 | { |
35 | 37 | private final TransactionsTestConfiguration configuration; |
36 | | - // single TransactionTemplate shared amongst all methods in this instance |
37 | | - private final TransactionTemplate transactionTemplate; |
| 38 | + private final AccountRepository repository; |
| 39 | + private Account account1; |
| 40 | + private Account account2; |
38 | 41 |
|
39 | 42 | @Autowired |
40 | | - public TransactionsTest( |
41 | | - final TransactionsTestConfiguration configuration, |
42 | | - final PlatformTransactionManager transactionManager) |
| 43 | + public TransactionsTest(final TransactionsTestConfiguration configuration, final AccountRepository repository) |
43 | 44 | { |
44 | 45 | this.configuration = configuration; |
45 | | - this.transactionTemplate = new TransactionTemplate(transactionManager); |
| 46 | + this.repository = repository; |
| 47 | + } |
| 48 | + |
| 49 | + @BeforeEach |
| 50 | + void initData() |
| 51 | + { |
| 52 | + this.account1 = new Account(1, BigDecimal.TEN); |
| 53 | + this.account2 = new Account(2, BigDecimal.ZERO); |
| 54 | + this.repository.saveAll(List.of(this.account1, this.account2)); |
46 | 55 | } |
47 | 56 |
|
48 | 57 | @Test |
49 | | - void accountTransaction_Working(final AccountRepository repository) |
| 58 | + void accountTransaction_Working(final PlatformTransactionManager transactionManager) |
50 | 59 | { |
51 | | - final Account account1 = new Account(BigDecimal.TEN); |
52 | | - final Account account2 = new Account(BigDecimal.ZERO); |
53 | | - repository.saveAll(List.of(account1, account2)); |
54 | | - |
55 | | - this.transactionTemplate.execute( |
| 60 | + new TransactionTemplate(transactionManager).execute( |
56 | 61 | status -> |
57 | 62 | { |
58 | | - account1.setBalance(account1.getBalance().subtract(BigDecimal.ONE)); |
59 | | - repository.save(account1); |
| 63 | + this.account1.setBalance(this.account1.getBalance().subtract(BigDecimal.ONE)); |
| 64 | + this.repository.save(this.account1); |
60 | 65 |
|
61 | | - account2.setBalance(account2.getBalance().subtract(BigDecimal.ONE)); |
62 | | - repository.save(account2); |
| 66 | + this.account2.setBalance(this.account2.getBalance().subtract(BigDecimal.ONE)); |
| 67 | + this.repository.save(this.account2); |
63 | 68 | return null; |
64 | 69 | } |
65 | 70 | ); |
66 | 71 |
|
67 | | - Assertions.assertEquals(BigDecimal.valueOf(9.0), repository.findById(account1.getId()).get().getBalance()); |
68 | | - Assertions.assertEquals(BigDecimal.ONE, repository.findById(account2.getId()).get().getBalance()); |
| 72 | + Assertions.assertEquals( |
| 73 | + BigDecimal.valueOf(9.0), |
| 74 | + this.repository.findById(this.account1.getId()).get().getBalance()); |
| 75 | + Assertions.assertEquals(BigDecimal.ONE, this.repository.findById(this.account2.getId()).get().getBalance()); |
69 | 76 | } |
70 | 77 |
|
71 | 78 | @Test |
72 | | - void accountTransaction_UnexpectedError(final AccountRepository repository) |
| 79 | + void accountTransaction_UnexpectedError(final PlatformTransactionManager transactionManager) |
73 | 80 | { |
74 | | - final Account account1 = new Account(BigDecimal.TEN); |
75 | | - final Account account2 = new Account(BigDecimal.ZERO); |
76 | | - repository.saveAll(List.of(account1, account2)); |
77 | | - |
78 | | - this.transactionTemplate.execute( |
| 81 | + new TransactionTemplate(transactionManager).execute( |
79 | 82 | status -> |
80 | 83 | { |
81 | | - account1.setBalance(account1.getBalance().subtract(BigDecimal.ONE)); |
82 | | - repository.save(account1); |
| 84 | + this.account1.setBalance(this.account1.getBalance().subtract(BigDecimal.ONE)); |
| 85 | + this.repository.save(this.account1); |
83 | 86 |
|
84 | 87 | throw new RuntimeException("Unexpected error"); |
85 | 88 | } |
86 | 89 | ); |
87 | 90 |
|
88 | | - Assertions.assertEquals(BigDecimal.TEN, repository.findById(account1.getId()).get().getBalance()); |
89 | | - Assertions.assertEquals(BigDecimal.ZERO, repository.findById(account2.getId()).get().getBalance()); |
| 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()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void accountTransaction_UnexpectedError_Annotation() |
| 97 | + { |
| 98 | + try |
| 99 | + { |
| 100 | + this.makeSingleChangeAndThenThrowException(); |
| 101 | + Assertions.fail("Somehow no exception was raised!"); |
| 102 | + } |
| 103 | + catch(final RuntimeException e) |
| 104 | + { |
| 105 | + } |
| 106 | + Assertions.assertEquals(BigDecimal.TEN, this.repository.findById(this.account1.getId()).get().getBalance()); |
| 107 | + Assertions.assertEquals(BigDecimal.ZERO, this.repository.findById(this.account2.getId()).get().getBalance()); |
| 108 | + } |
| 109 | + |
| 110 | + @Transactional |
| 111 | + void makeSingleChangeAndThenThrowException() |
| 112 | + { |
| 113 | + this.account1.setBalance(this.account1.getBalance().subtract(BigDecimal.ONE)); |
| 114 | + this.repository.save(this.account1); |
| 115 | + |
| 116 | + throw new RuntimeException("Unexpected error"); |
90 | 117 | } |
91 | 118 | } |
0 commit comments