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.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Yeongjun Kim
  • 739
  • 7
  • 19

4 Answers4

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)

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
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() {
        }   
    }
}
Yeongjun Kim
  • 739
  • 7
  • 19
  • 1
    Would you mind [creating an issue in the JUnit 5 project](https://github.com/junit-team/junit5/issues/new), so some – Nicolai Parlog May 26 '17 at 17:46
  • Oh, damn I forgot to edit my comment. Argh! I accidentally linked to the wrong project, [this one](https://github.com/sbrannen/spring-test-junit5) might have been better. But we will see... – Nicolai Parlog May 27 '17 at 08: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. ;-) – Sam Brannen May 28 '17 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());
    }
}

Huang Jinlong
  • 774
  • 8
  • 6
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 {
...
paulo.bing
  • 82
  • 10