Skip to content

Commit f355bbb

Browse files
BAEL-9305 (#18668)
* init * test updated * example code * apply suggestions and sync with draft code
1 parent 43d2870 commit f355bbb

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.transactional;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
6+
7+
@SpringBootApplication
8+
@EnableJpaRepositories
9+
public class Application {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(Application.class, args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.transactional;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.stereotype.Service;
5+
import org.springframework.transaction.annotation.Transactional;
6+
7+
@Service
8+
public class OrderService {
9+
10+
@Autowired
11+
private TestOrderRepository repository;
12+
13+
@Transactional
14+
public void createOrder(TestOrder order) {
15+
repository.save(order);
16+
}
17+
18+
@Transactional
19+
public void createOrderPublic(TestOrder order) {
20+
repository.save(order);
21+
throw new RuntimeException("Rollback createOrderPublic");
22+
}
23+
24+
@Transactional
25+
void createOrderPackagePrivate(TestOrder order) {
26+
repository.save(order);
27+
throw new RuntimeException("Rollback createOrderPackagePrivate");
28+
}
29+
30+
@Transactional
31+
protected void createOrderProtected(TestOrder order) {
32+
repository.save(order);
33+
throw new RuntimeException("Rollback createOrderProtected");
34+
}
35+
36+
@Transactional
37+
private void createOrderPrivate(TestOrder order) {
38+
repository.save(order);
39+
throw new RuntimeException("Rollback createOrderPrivate");
40+
}
41+
42+
public void callPrivate(TestOrder order) {
43+
createOrderPrivate(order);
44+
}
45+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.transactional;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.GeneratedValue;
5+
import jakarta.persistence.GenerationType;
6+
import jakarta.persistence.Id;
7+
import jakarta.persistence.Table;
8+
9+
@Entity
10+
@Table(name = "test_order")
11+
public class TestOrder {
12+
13+
@Id
14+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
15+
private Long id;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.transactional;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public interface TestOrderRepository extends JpaRepository<TestOrder, Long> {
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.transactional;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
6+
import org.junit.jupiter.api.AfterEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
11+
@SpringBootTest(classes = { Application.class, OrderService.class, TestOrderRepository.class })
12+
class TestOrderServiceTest {
13+
14+
@Autowired
15+
private OrderService underTest;
16+
17+
@Autowired
18+
private TestOrderRepository repository;
19+
20+
@AfterEach
21+
void afterEach() {
22+
repository.deleteAll();
23+
}
24+
25+
@Test
26+
void givenPublicTransactionalMethod_whenCallingIt_thenShouldRollbackOnException() {
27+
assertThat(repository.findAll()).isEmpty();
28+
29+
assertThatThrownBy(() -> underTest.createOrderPublic(new TestOrder())).isNotNull();
30+
31+
assertThat(repository.findAll()).isEmpty();
32+
}
33+
34+
@Test
35+
void givenPackagePrivateTransactionalMethod_whenCallingIt_thenShouldRollbackOnException() {
36+
assertThat(repository.findAll()).isEmpty();
37+
38+
assertThatThrownBy(() -> underTest.createOrderPackagePrivate(new TestOrder())).isNotNull();
39+
40+
assertThat(repository.findAll()).isEmpty();
41+
}
42+
43+
@Test
44+
void givenProtectedTransactionalMethod_whenCallingIt_thenShouldRollbackOnException() {
45+
assertThat(repository.findAll()).isEmpty();
46+
47+
assertThatThrownBy(() -> underTest.createOrderProtected(new TestOrder())).isNotNull();
48+
49+
assertThat(repository.findAll()).isEmpty();
50+
}
51+
52+
@Test
53+
void givenPrivateTransactionalMethod_whenCallingIt_thenShouldNotRollbackOnException() {
54+
assertThat(repository.findAll()).isEmpty();
55+
56+
assertThatThrownBy(() -> underTest.callPrivate(new TestOrder())).isNotNull();
57+
58+
assertThat(repository.findAll()).hasSize(1);
59+
}
60+
}

0 commit comments

Comments
 (0)