Spring boot 如何防止TestNg+中每个测试的回滚操作;弹簧&x2B;H2数据库?

Spring boot 如何防止TestNg+中每个测试的回滚操作;弹簧&x2B;H2数据库?,spring-boot,testng,Spring Boot,Testng,我使用SpringBoot集成测试和h2数据库以及testNg进行集成测试。 我的spring引导应用程序使用spring数据jpa。 对于每个测试,我无法看到以前测试保存的数据。 下面是我的集成测试类:- import org.springframework.test.annotation.Rollback; import org.testng.annotations.Test; import org.springframework.test.context.ActiveProfiles; i

我使用SpringBoot集成测试和h2数据库以及testNg进行集成测试。 我的spring引导应用程序使用spring数据jpa。 对于每个测试,我无法看到以前测试保存的数据。 下面是我的集成测试类:-

import org.springframework.test.annotation.Rollback;
import org.testng.annotations.Test;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;

@ActiveProfiles("test")
@SpringBootTest(classes = APP.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class IntegrationTest extends AbstractTestNGSpringContextTests {

    @Test(priority = 1)
    @Rollback(false)
    private void m1() {
        // save data.        
    }

    @Test(priority = 1)
    @Rollback(false)
    private void m2() {        

        // retrieve saved data. (Failed)
    }

 }

我正在使用TestRestTemplate来测试测试PUT和POST-Rest端点。

您正在使用
@SpringBootTest
注释,这意味着您将启动整个spring-boot应用程序<代码>@Commit和
@Rollback
用于
@DataJpaTest
或类似的测试,当您的测试是
@Transactional
时,以获得良好的控制

以下是我的想法:

  • 要么TestNG和Spring存在问题,在这种情况下,我无能为力
  • 或者你的代码有错。您是否尝试过使用一些断点来查看代码是否通过了所有层
你能展示更多你的代码吗?测试代码、控制器、服务和数据层。当然,对于您的配置类,这将非常有帮助


您必须使用注释
@Commit
,而不是
@Rollback

@ActiveProfiles("test")
@SpringBootTest(classes = APP.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class IntegrationTest extends AbstractTestNGSpringContextTests {

    @Test
    @Commit
    private void m1() {
    }
 }

或者,您可以使用
TransactionTemplate
实现更细粒度的控件。请参见:

//如何保存数据。它调用PostRESTAPI。我尝试了两种方法。但这对我还是不起作用。