[SPRING] No transactions created using cucumber-spring

1,119 views
Skip to first unread message

Per Segersten

unread,
Apr 2, 2014, 4:19:37 AM4/2/14
to cu...@googlegroups.com
Hi,

I'm currently upgrading from Cucumber v1.0.2 to v1.1.6 and can't get the JPA transactions to work. After upgrading the beans are still injected to the step object but the tests fail with:
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
at com.sun.proxy.$Proxy72.flush(Unknown Source)

Example:
@RunWith(Cucumber.class)
@CucumberOptions(features = {"src/test/java/tests/cucumber/test.feature"},
        glue
= { "tests.cucumber",
                 
"cucumber.runtime.java.spring.hooks"})
public class MyCucumberTest {
}

I'm usung the same spring configuration as before:
@ContextConfiguration("classpath:cucumber.xml")
public class MyCucumberSteps {

   
@Inject
   
MyService myService;

   
@Given
   
...
   
@When
   
...
   
@Then
   
...
}

What is needed to create transactions in v1,1.6? 

Regards
Per

Björn Rasmusson

unread,
Apr 2, 2014, 1:05:39 PM4/2/14
to cu...@googlegroups.com
Per Segersten wrote:
Hi,

I'm currently upgrading from Cucumber v1.0.2 to v1.1.6 and can't get the JPA transactions to work. After upgrading the beans are still injected to the step object but the tests fail with:
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
at com.sun.proxy.$Proxy72.flush(Unknown Source)

Example:
@RunWith(Cucumber.class)
@CucumberOptions(features = {"src/test/java/tests/cucumber/test.feature"},
        glue
= { "tests.cucumber",
                 
"cucumber.runtime.java.spring.hooks"})
public class MyCucumberTest {
}

Hi,

The SpringTransactionHook class has been moved to the package cucumber.api.spring so the glue parameter need to be changed to:
        glue = { "tests.cucumber",
                 
"cucumber.api.spring"})

However there are unresolved issues with Spring in the newest versions Cucumber-JVM; v1.1.4?, v1.1.5 and v1.1.6. So it may be hard to get transactions with Spring to work on those versions (AFAIK, I'm not using Spring myself), see #569, #637, #644 and #649.

Best Regards
Björn

 

aslak hellesoy

unread,
Apr 2, 2014, 1:35:28 PM4/2/14
to Cucumber Users, dmytro.c...@ffbit.com, Pedro Viegas
On Wed, Apr 2, 2014 at 6:05 PM, Björn Rasmusson <bj.ras...@gmail.com> wrote:
Per Segersten wrote:
Hi,

I'm currently upgrading from Cucumber v1.0.2 to v1.1.6 and can't get the JPA transactions to work. After upgrading the beans are still injected to the step object but the tests fail with:
javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.checkTransactionNeeded(AbstractEntityManagerImpl.java:1171)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:1332)
at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:342)
at com.sun.proxy.$Proxy72.flush(Unknown Source)

Example:
@RunWith(Cucumber.class)
@CucumberOptions(features = {"src/test/java/tests/cucumber/test.feature"},
        glue
= { "tests.cucumber",
                 
"cucumber.runtime.java.spring.hooks"})
public class MyCucumberTest {
}

Hi,

The SpringTransactionHook class has been moved to the package cucumber.api.spring so the glue parameter need to be changed to:
        glue = { "tests.cucumber",
                 
"cucumber.api.spring"})

However there are unresolved issues with Spring in the newest versions Cucumber-JVM; v1.1.4?, v1.1.5 and v1.1.6. So it may be hard to get transactions with Spring to work on those versions (AFAIK, I'm not using Spring myself), see #569, #637, #644 and #649.


I would love to see these issues closed. I'm not using Spring either, and my Spring-fu isn't strong enough to fix them without spending a considerable amount of investigation.

We'd be very grateful for a PR that fixes those issues.

Dmytro, Pedro - are you still around? Is this something you could look into?
Anyone else with strong Spring skills here?

Cheers,
Aslak
 
Best Regards
Björn

 
I'm usung the same spring configuration as before:
@ContextConfiguration("classpath:cucumber.xml")
public class MyCucumberSteps {

   
@Inject
   
MyService myService;

   
@Given
   
...
   
@When
   
...
   
@Then
   
...
}

What is needed to create transactions in v1,1.6? 

Regards
Per

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pedro Viegas

unread,
Apr 4, 2014, 9:21:28 AM4/4/14
to aslak hellesoy, Cucumber Users, dmytro.c...@ffbit.com
Hey dude! 

I am really sorry, I just opened my own company and I am kinda busy. I am gonna take a look on it on sunday and give you some feedback.

I am not working with spring right now here, otherwise I would have already fixed it =P


Regards
Pedro Viegas

Björn Rasmusson

unread,
Apr 21, 2014, 10:09:38 AM4/21/14
to cu...@googlegroups.com
Hi,

As far as I can tell (not being a spring user myself), in Cucumber-JVM v1.1.4, v1.1.5 and v1.1.6 there will be two separate spring contexts. One context based on the xml file cucumber/runtime/java/spring/cucumber-glue.xml of the spring module, and one context based on the xml file defined by the @ContextConfiguration annotation. The first context will be applied to stuff loaded outside the @ContextConfiguration annotation, notably the SpringTransactionHooks class, and this stuff will not be able to access stuff loaded within the @ContextConfiguration, for instance the the PlatformTransactionManger needed by the Spring TransactionHooks class. The second context will be applied to stuff loaded within the @ContextConfiguration annotation (stepdefs and application under test), and they will not be able to access stuff in the first context, for instance the @Scope('cucumber-glue').

The workaround I know of are:
To use the @Scope('cucumber-glue'), import the cucumber/runtime/java/spring/cucumber-glue.xml into the context defined by the @ContextConfiguration, see #600
  <import resource="classpath:/cucumber/runtime/java/spring/cucumber-glue.xml"/>

To use transactions, see this issue comment and code changes in #644
  • Do not add SpringTransactionHooks to the glue path (that is "cucumber.api.spring")
  • Define SpringTransactionHooks as a bean in the context defined by the @ContextConfiguration:
    <bean class="cucumber.api.spring.SpringTransactionHooks"/>
  • Add an SpringTransactionHooks as an autowired field to a step definition class and define a @Before and an @After hook that delegates to the SpringTransactionHooks instance:
        @Autowired
       
    private SpringTransactionHooks springTransactionHooks;

       
    @Before(value = {"@txn"}, order = 100)
       
    public void startTransaction() {
            springTransactionHooks
    .startTransaction();
       
    }

       
    @After(value = {"@txn"}, order = 100)
       
    public void rollBackTransaction() {
            springTransactionHooks
    .rollBackTransaction();
       
    }
Best Regards
Björn
Reply all
Reply to author
Forward
0 new messages