2010-11-04 5 views
6

Das hier ist eine kleine Testklasse, die ich habe. Problem ist, dass die Transaktion nach jedem Testlauf nicht zurückgesetzt wird. Was habe ich falsch gemacht? :)@Rollback kann nicht für meinen Spring JPA-Integrationstest verwendet werden

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

und meine Feder Config

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

Antwort

14

Sie benötigen @Transactional neben @TransactionConfiguration:

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

I l Ove du! (Du hast aber zu schnell geantwortet, brauche noch 4 Minuten, bevor ich eine Antwort annehmen kann) – willcodejavaforfood

+1

genau die Antwort, die ich gegeben hätte, wenn ich rechtzeitig auftauchte (+1) –

+0

@seanizer - Ich werde deinen Kommentar zumindest auffrischen :) – willcodejavaforfood

0

im Frühjahr 4.0 später, weil TransactionConfiguration veraltet

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