Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring DataJpaTest transactional behavior for Nested JUnit 5 tests [duplicate]

I use spring-boot, JUnit5, Mybatis.

@SpringJUnitJupiterConfig(classes = {RepositoryTestConfig.class})
@MapperScan
@Rollback
@Transactional
public class TestClass {
    @Autowired
    private TestMapper testMapper;

    @BeforeEach
    void init() {
        User user = new User();
        testMapper.insert(user);    
    }

    @Test
    public void test1() {
        // (1) success rollback
    }

    @Nested
    class WhenExistData {
        @Test
        public void test2() {
            // (2) rollback not working
        }   
    }
}

(1) is working rollback. And the following log is output.

2017-05-26 22:21:29 [INFO ](TransactionContext.java:136) Rolled back transaction for test context ...

But, (2) is not working. I want to be able to roll back into @Nested.

like image 731
Yeongjun Kim Avatar asked Nov 18 '22 04:11

Yeongjun Kim


2 Answers

This is to be expected: the Spring TestContext Framework has never supported "inheritance" for nested test classes.

Thus your "work around" is actually the correct way to achieve your goal at this point in time.

Note, however, that I may add support for "pseudo-inheritance" for nested test classes in conjunction with SPR-15366.

Regards,

Sam (author of the Spring TestContext Framework)

like image 145
Sam Brannen Avatar answered Nov 20 '22 00:11

Sam Brannen


I solved it in the following way..

@SpringJUnitJupiterConfig(classes = {RepositoryTestConfig.class})
@MapperScan
@Rollback
@Transactional
public class TestClass {
    @Autowired
    private TestMapper testMapper;

    @BeforeEach
    void init() {
        User user = new User();
        testMapper.insert(user);    
    }

    @Nested
    @SpringJUnitJupiterConfig(classes = {RepositoryTestConfig.class})
    @MapperScan
    @Rollback
    @Transactional
    class WhenExistData {
        @Test
        public void test2() {
        }   
    }
}
like image 31
Yeongjun Kim Avatar answered Nov 19 '22 23:11

Yeongjun Kim