spring - 如何在 AbstractTransactionalJUnit4SpringContextTests 事务中执行 junit 规则?

标签 spring testing junit

我想在测试方法事务中做一些数据库操作。我想为此使用 junit TestRules。但是规则是在事务之外执行的。有什么办法可以让我的规则在交易中执行吗?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:/META-INF/spring/applicationContext.xml"})
public class ProductServiceTest extends AbstractTransactionalJUnit4SpringContextTests {

public @Rule @Autowired MySimpleSpringRule mySimpleSpringRule;

@Before
public void before() {
    logger.debug("before");
}

@After
public void after() {
    logger.debug("after");
}

@Test
public void testFindProducts() {
...

@Component
public class MySimpleSpringRule implements TestRule {

private class Banaani extends ExternalResource {
    @Override
    protected void before() throws Throwable {
        logger.debug("before");
    };

    @Override
    protected void after() {
        logger.debug("after");
    };
}

private final Banaani banaani = new Banaani();

@Override
public Statement apply(Statement st, Description desc) {
    return banaani.apply(st, desc);
}

结果

2013-01-18 10:33:24,845 DEBUG [main]: MySimpleSpringRule         - before
2013-01-18 10:33:24,869 INFO  [main]: ionalTestExecutionListener - Began transaction (1): transaction manager [org.springframework.orm.jpa.JpaTransactionManager@a713a5]; rollback [true]
2013-01-18 10:33:24,869 DEBUG [main]: ProductServiceTest         - before
2013-01-18 10:33:24,869 DEBUG [main]: ProductServiceTest         - running test...
2013-01-18 10:33:24,984 DEBUG [main]: ProductServiceTest         - after
2013-01-18 10:33:24,986 INFO  [main]: ionalTestExecutionListener - Rolled back transaction after test execution for test context [[TestContext@67946a testClass = ProductServiceTest, testInstance = fi.liikennevirasto.service.ProductServiceTest@40f0b2, testMethod = testFindRootProducts@ProductServiceTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@17a6b96 testClass = ProductServiceTest, locations = '{classpath:/META-INF/spring/test-bean-configuration.xml, classpath:/META-INF/spring/applicationContext.xml, classpath:/META-INF/spring/infrastructure.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]]
2013-01-18 10:33:24,987 DEBUG [main]: MySimpleSpringRule         - after

直接在测试类中的@Before和@After方法是在事务内部执行的,而MySimpleSpringRule中的方法是在事务外部执行的。

最佳答案

这可能无法使用@Rules....?

如果不是,可以使用 http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/test/context/TestExecutionListener.html 完成同样的事情

关于spring - 如何在 AbstractTransactionalJUnit4SpringContextTests 事务中执行 junit 规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14395189/

相关文章:

java - 使用 Spring MVC 保存文件

java - Eclipse Java IDE JUnit5 : junit. jupiter.api.Assertions 无法访问

Java Spring - 使用 API 持久化对象可以工作,但我无法在测试中使用服务持久化对象,空指针

java - Spring - 在 Spring 项目中包含本身使用 Spring 的 jar 的方法

java - Spring JDBCTemplate 执行不抛出异常

java - 如何在 hibernate 中使用带有位置参数的@Loader?

testing - 用于在浏览器中交换(实时和测试)URL 的书签

java - 如何从 Play WSClient 的字符串创建 WSResponse 对象

java - 通过 Annotation Transformer : TestNG 禁用特定测试

java - Vertx ( vertx-Junit5 模块 ) 没有找到测试