- 스프링에서 테스트 코드를 작성시 @Rollback(true)해당 옵션이 기본 설정임.
 해당 설정은 메소드와 클래스 레벨에서 기본 설정으로 되어 있음.
 개별 테스트가 끝난후 롤백, 전체 테스트가 끝난후 클래스 래밸에서 롤백 시킴.
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
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class SpringBootJpaTestSliceTest extends Common {
  @Autowired
  private BookRepository bookRepository;
  @Test
  @Order(1)
  void testJpaTestSlice() {
    long countBefore = bookRepository.count();
    Assertions.assertEquals(countBefore, 0L);
    bookRepository.save(new Book("my book", "123210ws", "self"));
    long countAfter = bookRepository.count();
    Assertions.assertTrue(countBefore != countAfter);
    Assertions.assertEquals(countAfter, 1L);
  }
  @Test
  @Order(2)
  void testJpaTestSpliceTransaction() {
    long count = bookRepository.count();
    Assertions.assertEquals(count, 1L);
  }
}
- 위와 같은 코드가 있을때 첫번째 테스트는 통과하지만 두번째 테스트는 통과하지 못함.
@Rollback
- 변경 사항을 되돌리는 어노테이션 : import org.springframework.test.annotation.Rollback- @Rollback(value = false) : 롤백 기능을 off
- @Rollback(value = true) : 롤백 기능을 on (기본 설정)
 
@Commit
- 변경 사항을 적용시키는 어노테이션 : import org.springframework.test.annotation.Commit- 코드를 보면 @Rollback(false)가 되어 있는걸 볼 수 있다.
 
1
2
3
4
5
6
7
8
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Rollback(false)
public @interface Commit {
}
수정 코드
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
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class SpringBootJpaTestSliceTest extends Common {
    @Autowired
    private BookRepository bookRepository;
    @Test
    @Commit
    @Order(1)
    void testJpaTestSlice() {
        long countBefore = bookRepository.count();
        Assertions.assertEquals(countBefore, 0L);
        bookRepository.save(new Book("my book", "123210ws", "self"));
        long countAfter = bookRepository.count();
        Assertions.assertTrue(countBefore != countAfter);
        Assertions.assertEquals(countAfter, 1L);
    }
    @Test
    @Order(2)
    void testJpaTestSpliceTransaction() {
        long count = bookRepository.count();
        Assertions.assertEquals(count, 1L);
    }
}