cancel
Showing results for 
Search instead for 
Did you mean: 

Testing IdentityService. Is @Rollback possible?

ollib
Champ in-the-making
Champ in-the-making
I wrote some JUnit test methods for IdentityService. Is there a way to rollback modifications in the IdentityService tables at the end of a test with @Rollback or @Transaction?

In the IdentityTest test class on Github there is a manual cleanup at the end of each test method.

Best regards
Oliver
2 REPLIES 2

trademak
Star Contributor
Star Contributor
Hi Oliver,

Yes, if you use the Spring module of Activiti you can use Spring transaction rollback in your unit tests.

Best regards,

ollib
Champ in-the-making
Champ in-the-making
Ok, this seems to work:

<code>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:Test/spring/application-context.xml",
                                  "file:Test/spring/activiti-context.xml"} )
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
public class IdentityServiceTest {
private static Logger logger = Logger.getLogger(IdentityServiceTest.class.getName());

@Autowired
@Rule
public ActivitiRule activitiSpringRule;

@Autowired
private IdentityService identityService;


@Test
@Transactional
public void groupTest1() {

      identityService.saveGroup(identityService.newGroup("testgruppe"));
      long countGroups = identityService.createGroupQuery().count();
      assertEquals(1, countGroups);
}


@Test
@Transactional
public void groupTest2() {
      identityService.saveGroup(identityService.newGroup("testgruppe"));
      long countGroups = identityService.createGroupQuery().count();
      assertEquals(1, countGroups);
}
}
</code>

But only with this transactionsmanager definition:

<code>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="datasource"/>
</bean>
</code>

As I want to use JPA entities, I need this kind of transactionmanager:
<code>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="datasource"/>
        <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
        <property name="persistenceUnitName" value="xxx"/>
</bean>
</code>

With this persistence.xml

<code>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
  <persistence-unit name="xxx" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.show_sql" value="true"/>
    </properties>
   
    <class>org.olli.mvc.persistence.model.Vacation</class>
  </persistence-unit>
</persistence>
</code>

I have some difficulties in finding the right configuration for my transactionmanager. In general, I need a setup where I can handle activiti and my JPA entities in the same transaction.


Best regards
Oliver