技术开发 频道

在Spring中使用JTA事务管理



    5. 在Spring中运行测试

    代码清单 3 TestBbtForumJta
package com.baobaotao.service; import org.springframework.test.AbstractDependencyInjectionSpringContextTests; public class TestBbtForumJta extends AbstractDependencyInjectionSpringContextTests{ private BbtForum bbtForum; private final Logger logger = Logger.getLogger(getClass()); public void setBbtForum(BbtForum bbtForum) { this.bbtForum = bbtForum; } protected String[] getConfigLocations() { return new String[]{"classpath:applicationContext-jta.xml"}; } public void testAddPost() throws Exception{ logger.info("begin........"); Topic topic = new Topic(); topic.setTopicTitle("Title -pfb"); Post post = new Post(); post.setPostText("post content -pfb"); topic.setPost(post); bbtForum.addTopic(topic); ①使用了JTA事务的业务方法 logger.info("end........"); } }
    通过Spring测试类AbstractDependencyInjectionSpringContextTests的支持,很容易编写一个测试类,对启用了JTA事务的BbtForum#addTopic()方法进行测试。建议你将Log4J设置为DEBUG,这样就可以通过丰富的输出日志观测到JTA事务的执行情况。运行这个测试类后,你将可以看到JTA事务被正确实施。
0
相关文章