9

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.

4 Answers 4

11

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)

3

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() {
        }   
    }
}
3
  • 1
    Would you mind creating an issue in the JUnit 5 project, so some Commented May 26, 2017 at 17:46
  • 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... Commented May 27, 2017 at 8:22
  • 1
    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. ;-) Commented May 28, 2017 at 12:12
0

I solved it in the following way

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

// JUnit5
@SpringBootTest
public class TestClass {

    @Resource
    private TestMapper testMapper;

    @Test
    @Rollback
    @Transactional
    public void createByTimerId() {
        Assertions.assertEquals(1, testMapper.insert());
    }
}

0

Using the annotation NestedTestConfiguration from Spring on the enclosing class did the trick for me. It seems that without it, the nested class won't inherit the config.

@NestedTestConfiguration(NestedTestConfiguration.EnclosingConfiguration.INHERIT)

So maybe on your TestClass you would have your current annotations and then:

@NestedTestConfiguration(NestedTestConfiguration.EnclosingConfiguration.INHERIT)
public class TestClass {
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.