cukes icon indicating copy to clipboard operation
cukes copied to clipboard

Difficulty getting working in a spring-based project

Open kyounger opened this issue 7 years ago • 6 comments

Do you guys have a suggestions for including this in a project that already uses cucumber-spring? It seems like cucumber-guice and cucumber-spring don't play well together.

I had a go at trying to use the spring-guice project to see if I could include the guice modules in the spring container, but I don't know guice that well and couldn't figure out how to get it to work.

I'd also be ok with suggestions on how to approach modifying the code to use spring instead of guice. It doesn't seem that you use a ton of guice, so perhaps that would be an easier approach.

kyounger avatar Aug 03 '17 20:08 kyounger

Could you please give a bit more details about your use case (i.e. why you would like to use Spring in your Cucumber tests)?

The most simple way is to separate classpaths. E.g. either move your Cucumber/Cukes tests into separate module or even to a separate project. This way you can avoid dependency conflicts.

But Cukes should work fine with Spring-based applications. You can have a look at one of the samples. Here we start a Spring Boot application before running Cukes tests. So in this case we have both Spring DI and Guice in classpath at the same time.

Hope this helps.

sergeytrasko avatar Aug 04 '17 06:08 sergeytrasko

I'll review that specific sample. Thanks.

We do use spring quite a bit already in our cucumber steps, for things like backgrounds (data setup). As you probably know, with a large application the testing code can become an application itself and since we already know spring, we use it there to great effect.

Perhaps a better question is, when you need to write custom steps, where should that code live? Is that what the custom plugin piece of the architecture is for?

kyounger avatar Aug 09 '17 13:08 kyounger

In one of the projects I did something similar. We had a Spring-based application and during Cucumber scenario setup we had to make some data preparation (i.e. persisting data into database).

Our solution was the following:

  1. Implement own lv.ctco.cukes.core.extension.CukesPlugin. Don't forget to include it into cukes.properties file under cukes.plugins property
  2. beforeAllTests() method is a good place to start your Spring context. You should get reference to ApplicationContext out of this process. Don't forget to close the context in afterAllTests()
  3. Store reference to this ApplicationContext somewhere e.g. in some singleton or a place which you can access from any context (not only spring-managed beans)
  4. In custom step just get reference to ApplicationContext (that you stored on a previous step)
  5. Lookup desired Spring bean (e.g. repository) from ApplicationContext
  6. Run required data preparation using Spring bean taken from context

So in other words we are separating Cukes runtime from Spring context.

Hope that helps.

sergeytrasko avatar Aug 10 '17 14:08 sergeytrasko

You can check out https://github.com/ctco/cukes/tree/master/cukes-samples/cukes-plugin-sample for a showcase how to create plugins

sergeytrasko avatar Aug 10 '17 14:08 sergeytrasko

Solution with before\after works, but you are loosing spring power like mocking beans\3d-party services. As a workaround I combined SpringFactory from cucumber-spring with SingletonObjectFactory from cukes. Sample is here https://gist.github.com/slamdev/c56e97f80cd4d82c1b02e013a589adea

it would be super cool to have some official support from cukes.

slamdev avatar Feb 12 '18 15:02 slamdev

Hi,

Is this still the state of affairs? Sergey's suggestion is too much overhead to justify in our case

hughesadam87 avatar Jul 24 '18 16:07 hughesadam87