Skip to content

Commit 5a5b5d4

Browse files
committed
correct test transactional rollback
1 parent 36508cb commit 5a5b5d4

File tree

11 files changed

+64
-64
lines changed

11 files changed

+64
-64
lines changed

transaction-declarative/.idea/compiler.xml

Lines changed: 10 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transaction-declarative/.idea/encodings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transaction-declarative/readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Example use declarative transaction
22

3-
For service layer - for class
4-
5-
`/src/main/java/hello/service/transaction/impl/EmployeeServiceImpl.java`
3+
Create two service from one interface `EmployeeService`
4+
- `EmployeeServiceWithTransactional` with annotation @Transactional
5+
- `EmployeeServiceWithoutTransactional` without annotation @Transactional
66

77
## Configuration
88

transaction-declarative/src/main/java/hello/service/transaction/EmployeeService.java renamed to transaction-declarative/src/main/java/hello/service/EmployeeService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package hello.service.transaction;
1+
package hello.service;
22

33
import hello.model.Employee;
44

55
import java.util.List;
66

77
public interface EmployeeService {
8+
89
void insert(Employee emp);
910

1011
int insertWithReturnInsertedId(Employee emp);

transaction-declarative/src/main/java/hello/service/transaction/impl/EmployeeServiceImpl.java renamed to transaction-declarative/src/main/java/hello/service/transaction/impl/EmployeeServiceWithTransactional.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package hello.service.transaction.impl;
22

3-
import java.util.List;
4-
53
import hello.dao.EmployeeDao;
64
import hello.model.Employee;
7-
import hello.service.transaction.EmployeeService;
5+
import hello.service.EmployeeService;
86
import org.springframework.beans.factory.annotation.Autowired;
97
import org.springframework.dao.EmptyResultDataAccessException;
108
import org.springframework.stereotype.Service;
119
import org.springframework.transaction.annotation.Transactional;
1210

13-
@Service("EmployeeServiceWithTransaction")
11+
import java.util.List;
12+
13+
@Service
1414
@Transactional
15-
public class EmployeeServiceImpl implements EmployeeService {
15+
public class EmployeeServiceWithTransactional implements EmployeeService {
1616

1717
@Autowired
1818
EmployeeDao employeeDao;

transaction-declarative/src/main/java/hello/service/without_transaction/EmployeeService.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

transaction-declarative/src/main/java/hello/service/without_transaction/impl/EmployeeServiceImpl.java renamed to transaction-declarative/src/main/java/hello/service/without_transaction/impl/EmployeeServiceWithoutTransactional.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
22

33
import hello.dao.EmployeeDao;
44
import hello.model.Employee;
5-
import hello.service.without_transaction.EmployeeService;
5+
import hello.service.EmployeeService;
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.dao.EmptyResultDataAccessException;
88
import org.springframework.stereotype.Service;
9-
import org.springframework.transaction.annotation.Transactional;
109

1110
import java.util.List;
1211

13-
@Service("EmployeeServiceWithoutTransaction")
14-
public class EmployeeServiceImpl implements EmployeeService {
12+
@Service
13+
public class EmployeeServiceWithoutTransactional implements EmployeeService {
1514

1615
@Autowired
1716
EmployeeDao employeeDao;

transaction-declarative/src/test/java/hello/service/transaction/impl/EmployeeServiceImplTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package hello.service.transaction.impl;
22

33
import hello.model.Employee;
4-
import hello.service.transaction.EmployeeService;
54
import org.junit.Test;
65
import org.junit.runner.RunWith;
76
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.beans.factory.annotation.Qualifier;
97
import org.springframework.dao.DuplicateKeyException;
108
import org.springframework.test.context.ContextConfiguration;
119
import org.springframework.test.context.jdbc.Sql;
@@ -17,6 +15,7 @@
1715

1816
import static org.hamcrest.CoreMatchers.equalTo;
1917
import static org.hamcrest.Matchers.containsInAnyOrder;
18+
import static org.hamcrest.Matchers.is;
2019
import static org.junit.Assert.assertThat;
2120

2221
@ContextConfiguration(classes = EmployeeServiceImplTestConfig.class)
@@ -25,9 +24,9 @@
2524
@Sql("/db.sql"),
2625
})
2726
public class EmployeeServiceImplTest {
27+
2828
@Autowired
29-
@Qualifier("EmployeeServiceWithTransaction")
30-
private EmployeeService service;
29+
private EmployeeServiceWithTransactional service;
3130

3231
@Test
3332
public void insertList() {
@@ -46,22 +45,26 @@ public void insertList() {
4645
}
4746

4847
@Test
49-
public void insertList_WhenDuplicateByEmail() {
48+
public void insertList_WhenDuplicateByEmail_ShouldNothingSavedAllRollback() {
5049
String sameEmail = "[email protected]";
5150
Employee employee1 = new Employee(1, "dd", sameEmail);
5251
Employee employee2 = new Employee(2, "dd2", sameEmail);
5352

54-
List<Employee> expectedList = new ArrayList<>();
55-
expectedList.add(employee1);
56-
expectedList.add(employee2);
53+
List<Employee> employees = new ArrayList<>();
54+
employees.add(employee1);
55+
employees.add(employee2);
5756

5857
// will rollback
58+
boolean isDuplicateKeyException = false;
5959
try {
60-
service.insertList(expectedList);
60+
service.insertList(employees);
6161
} catch (DuplicateKeyException e) {
62+
isDuplicateKeyException = true;
6263
e.printStackTrace();
6364
}
6465

66+
assertThat(isDuplicateKeyException, is(true));
67+
6568
List<Employee> actualList = service.getAll();
6669

6770
assertThat(actualList, equalTo(new ArrayList<Employee>()));

transaction-declarative/src/test/java/hello/service/without_transaction/impl/EmployeeServiceImplTest.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package hello.service.without_transaction.impl;
22

33
import hello.model.Employee;
4-
import hello.service.without_transaction.EmployeeService;
54
import org.junit.Test;
65
import org.junit.runner.RunWith;
76
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.beans.factory.annotation.Qualifier;
97
import org.springframework.dao.DuplicateKeyException;
108
import org.springframework.test.context.ContextConfiguration;
119
import org.springframework.test.context.jdbc.Sql;
@@ -16,6 +14,7 @@
1614
import java.util.List;
1715

1816
import static org.hamcrest.Matchers.containsInAnyOrder;
17+
import static org.hamcrest.Matchers.is;
1918
import static org.junit.Assert.assertThat;
2019

2120
@ContextConfiguration(classes = EmployeeServiceImplTestConfig.class)
@@ -24,9 +23,9 @@
2423
@Sql("/db.sql"),
2524
})
2625
public class EmployeeServiceImplTest {
26+
2727
@Autowired
28-
@Qualifier("EmployeeServiceWithoutTransaction")
29-
private EmployeeService service;
28+
private EmployeeServiceWithoutTransactional service;
3029

3130
@Test
3231
public void insertList() {
@@ -45,23 +44,31 @@ public void insertList() {
4544
}
4645

4746
@Test
48-
public void insertList_WhenDuplicateByEmail() {
47+
public void insertList_WhenDuplicateByEmail_ShouldInsertOnlyOne() {
4948
String sameEmail = "[email protected]";
5049
Employee employee1 = new Employee(1, "dd", sameEmail);
5150
Employee employee2 = new Employee(2, "dd2", sameEmail);
5251

53-
List<Employee> expectedList = new ArrayList<>();
54-
expectedList.add(employee1);
52+
List<Employee> employees = new ArrayList<>();
53+
employees.add(employee1);
54+
employees.add(employee2);
5555

5656
// will not rollback
57+
boolean isDuplicateKeyException = false;
5758
try {
58-
service.insertList(expectedList);
59+
service.insertList(employees);
5960
} catch (DuplicateKeyException e) {
61+
isDuplicateKeyException = true;
6062
e.printStackTrace();
6163
}
6264

65+
assertThat(isDuplicateKeyException, is(true));
66+
6367
List<Employee> actualList = service.getAll();
6468

65-
assertThat(actualList, containsInAnyOrder(expectedList.toArray()));
69+
List<Employee> expected = new ArrayList<>();
70+
expected.add(employee1);
71+
72+
assertThat(actualList, containsInAnyOrder(expected.toArray()));
6673
}
6774
}

transaction-programmatic/.idea/compiler.xml

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transaction-programmatic/.idea/encodings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)