2010-11-04 38 views
6

工作,這這裏是一個小測試類,我有。問題在於,每次測試運行後都不會回滾事務。我做錯了什麼? :)不能得到@Rollback爲我的春天JPA集成測試

@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); 
    } 
} 

和我的Spring配置

<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 /> 

回答

14

您除了需要@Transactional@TransactionConfiguration

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "/META-INF/catalog-spring.xml" }) 
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) 
@Transactional 
public class TermTest { ... } 
+0

我升奧夫你! (你回答得太快,需要4分鐘,因爲我可以接受答案) – willcodejavaforfood 2010-11-04 15:58:20

+1

正是我給出的答案,如果我已經及時出現了(+1) – 2010-11-04 16:01:07

+0

@seanizer - 我會滿意你的評論至少:) – willcodejavaforfood 2010-11-04 16:07:24

0

春季4.0後來因爲TransactionConfiguration已被棄用

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