-
Notifications
You must be signed in to change notification settings - Fork 30
/
AccountEntityTransactionalTest.java
85 lines (60 loc) · 2.41 KB
/
AccountEntityTransactionalTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.jayway.repository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.ConstraintViolationException;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = EmbeddedDbJavaConfig.class)
// @ContextConfiguration("/embedded-db-application-context.xml")
@Transactional
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
public class AccountEntityTransactionalTest {
@PersistenceContext
EntityManager entityManager;
@Autowired
AccountRepository accountRepository;
@Test
public void canFindAccount() {
AccountEntity account = accountRepository.findOne(1);
assertThat(account.getAccountNumber(), is(1));
assertThat(account.getBalance(), is(100));
}
@Test
public void shouldDeposit() {
AccountEntity account = accountRepository.findOne(1);
account.deposit(50);
entityManager.flush();
AccountEntity updated = accountRepository.findOne(1);
assertThat(updated.getBalance(), is(150));
}
@Test
public void verifyBalance() {
AccountEntity account = accountRepository.findOne(1);
assertThat(account.getAccountNumber(), is(1));
assertThat(account.getBalance(), is(100));
}
@Test
public void shouldWithdraw() {
AccountEntity account = accountRepository.findOne(1);
account.withdraw(20);
entityManager.flush();
AccountEntity updated = accountRepository.findOne(1);
assertThat(updated.getBalance(), is(80));
}
@Test(expected = ConstraintViolationException.class)
public void shouldNotAcceptNegativeBalance() {
AccountEntity account = accountRepository.findOne(1);
account.withdraw(200);
entityManager.flush();
AccountEntity updated = accountRepository.findOne(1);
assertThat(updated.getBalance(), is(-100));
}
}