代码之家  ›  专栏  ›  技术社区  ›  willcodejavaforfood

无法让@Rollback为我的Spring JPA集成测试工作

  •  6
  • willcodejavaforfood  · 技术社区  · 14 年前

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "/META-INF/catalog-spring.xml" })
    @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
    public class TermTest
    {
        @Autowired
        private CatalogService service;
        @Rollback(true)
        @Test
        public void testSimplePersist()
        {   
            Term term = new Term();
            term.setDescription("Description");
            term.setName("BirdSubject8");
            term.setIsEnabled("F");
            term.setIsSystem("F");
            term.setTermType("TERM");
            service.createTerm(term);
        }
    }
    

    我的弹簧配置

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="catalog2"></property>
    </bean>
    
    <bean id="catalogService" class="com.moo.catalog.service.CatalogService">
        <property name="termDao" ref="termDao"></property>
    </bean>
    
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
    
    <bean id="transactionManager"
            class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    
    <tx:annotation-driven />
    
    2 回复  |  直到 14 年前
        1
  •  14
  •   axtavt    14 年前

    你需要 @Transactional 除了 @TransactionConfiguration

    @RunWith(SpringJUnit4ClassRunner.class) 
    @ContextConfiguration(locations = { "/META-INF/catalog-spring.xml" }) 
    @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) 
    @Transactional
    public class TermTest { ... }
    
        2
  •  0
  •   xu cheng    9 年前

    在稍后的Spring4.0中,因为TransactionConfiguration被弃用

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "/config/spring-config.xml")
    @Transactional
    public class UserTest {
        @Rollback
        public void test(){
        }
    }