-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransactionTest.java
55 lines (44 loc) · 1.48 KB
/
TransactionTest.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
package com.example.demo;
import javax.persistence.EntityManager;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.transaction.AfterTransaction;
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace=Replace.NONE)
public class TransactionTest {
@Autowired PersonRepository repo;
@Autowired
private EntityManager em;
@Before
public void showCountBefore() {
System.err.println("before: " + repo.count());
}
@After
public void showCountAfter() {
System.err.println("after: " + repo.count());
}
@AfterTransaction
public void showCountAfterTransaction() {
System.err.println("after tx: " + repo.count());
}
@Test
public void testRollback() {
repo.save(new Person("Deyne", "Dirk", "dirkdeyne"));
System.err.println("saved: " + repo.count());
}
@Test
public void testRollbackWithEntityManger() {
Person entity = new Person("Deyne", "Kevin", "kevdeyne");
em.persist(entity);
em.flush();
System.err.println("em save: " + repo.count());
}
}