我正在使用带有h2数据库和testNg的spring boot集成测试进行集成测试。我的spring boot应用程序使用spring data 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
。
这是我的想法:
您可以显示更多代码吗?测试代码,控制器,服务和数据层。当然,您的配置类对帮助您很有用
您必须使用注释,@Commit
而不是@Rollback
@ActiveProfiles("test")
@SpringBootTest(classes = APP.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class IntegrationTest extends AbstractTestNGSpringContextTests {
@Test
@Commit
private void m1() {
}
}
或者,您可以使用TransactionTemplate
更细粒度的控件。看到这个:如何在Spring Boot测试中强制事务提交?。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句