|
24 | 24 | import org.springframework.beans.factory.annotation.Autowired; |
25 | 25 | import org.springframework.test.context.ContextConfiguration; |
26 | 26 | import org.springframework.transaction.PlatformTransactionManager; |
27 | | -import org.springframework.transaction.annotation.Transactional; |
28 | 27 | import org.springframework.transaction.support.TransactionTemplate; |
29 | 28 |
|
30 | 29 | import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations; |
@@ -55,64 +54,59 @@ void initData() |
55 | 54 | } |
56 | 55 |
|
57 | 56 | @Test |
58 | | - void accountTransaction_Working(final PlatformTransactionManager transactionManager) |
| 57 | + void accountTransaction_Working(@Autowired final PlatformTransactionManager transactionManager) |
59 | 58 | { |
60 | 59 | new TransactionTemplate(transactionManager).execute( |
61 | 60 | status -> |
62 | 61 | { |
63 | 62 | this.account1.setBalance(this.account1.getBalance().subtract(BigDecimal.ONE)); |
64 | 63 | this.repository.save(this.account1); |
65 | 64 |
|
66 | | - this.account2.setBalance(this.account2.getBalance().subtract(BigDecimal.ONE)); |
| 65 | + this.account2.setBalance(this.account2.getBalance().add(BigDecimal.ONE)); |
67 | 66 | this.repository.save(this.account2); |
68 | 67 | return null; |
69 | 68 | } |
70 | 69 | ); |
71 | 70 |
|
72 | 71 | Assertions.assertEquals( |
73 | | - BigDecimal.valueOf(9.0), |
| 72 | + BigDecimal.valueOf(9), |
74 | 73 | this.repository.findById(this.account1.getId()).get().getBalance()); |
75 | 74 | Assertions.assertEquals(BigDecimal.ONE, this.repository.findById(this.account2.getId()).get().getBalance()); |
76 | 75 | } |
77 | 76 |
|
78 | 77 | @Test |
79 | | - void accountTransaction_UnexpectedError(final PlatformTransactionManager transactionManager) |
| 78 | + void accountTransaction_UnexpectedError(@Autowired final PlatformTransactionManager transactionManager) |
80 | 79 | { |
81 | | - new TransactionTemplate(transactionManager).execute( |
82 | | - status -> |
83 | | - { |
84 | | - this.account1.setBalance(this.account1.getBalance().subtract(BigDecimal.ONE)); |
85 | | - this.repository.save(this.account1); |
86 | | - |
87 | | - throw new RuntimeException("Unexpected error"); |
88 | | - } |
89 | | - ); |
| 80 | + Assertions.assertThrows(RuntimeException.class, () -> |
| 81 | + new TransactionTemplate(transactionManager).execute( |
| 82 | + status -> |
| 83 | + { |
| 84 | + this.account1.setBalance(this.account1.getBalance().subtract(BigDecimal.ONE)); |
| 85 | + this.repository.save(this.account1); |
| 86 | + |
| 87 | + throw new RuntimeException("Unexpected error"); |
| 88 | + } |
| 89 | + )); |
90 | 90 |
|
91 | 91 | Assertions.assertEquals(BigDecimal.TEN, this.repository.findById(this.account1.getId()).get().getBalance()); |
92 | 92 | Assertions.assertEquals(BigDecimal.ZERO, this.repository.findById(this.account2.getId()).get().getBalance()); |
93 | 93 | } |
94 | 94 |
|
| 95 | + /** |
| 96 | + * This test should demonstrate the behavior of transactions. |
| 97 | + */ |
95 | 98 | @Test |
96 | | - void accountTransaction_UnexpectedError_Annotation() |
| 99 | + void findStoredEntityWithinTransaction(@Autowired final PlatformTransactionManager transactionManager) |
97 | 100 | { |
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"); |
| 101 | + new TransactionTemplate(transactionManager).execute( |
| 102 | + status -> |
| 103 | + { |
| 104 | + final Account account3 = new Account(3, BigDecimal.valueOf(100.0)); |
| 105 | + this.repository.save(account3); |
| 106 | + |
| 107 | + Assertions.assertFalse(this.repository.findById(account3.getId()).isPresent()); |
| 108 | + return null; |
| 109 | + } |
| 110 | + ); |
117 | 111 | } |
118 | 112 | } |
0 commit comments