Transaction roll back is not working in test case in @Nested class of JUnit5

Transaction roll back is not working in test case in @Nested class of JUnit5



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.


@Nested




2 Answers
2



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)



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()







Would you mind creating an issue in the JUnit 5 project, so some
– Nicolai
May 26 '17 at 17:46





created issue. github.com/junit-team/junit5/issues/868
– Yeongjun Kim
May 27 '17 at 8:19






Oh, damn I forgot to edit my comment. Argh! I accidentally linked to the wrong project, this one might have been better. But we will see...
– Nicolai
May 27 '17 at 8:22





Neither JUnit 5 nor my personal spring-test-junit5 repository is the appropriate place to raise such an issue. The correct place is Spring's JIRA issue tracker. ;-)
– Sam Brannen
May 28 '17 at 12:12


spring-test-junit5






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)