Blog post-accompanying project
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Cucumber-JVM Dependency Injection with Spring

This repo accompanies this blog post and is an example maven project demonstrating how to use Spring's dependency injection with Cucumber-JVM in a test automation framework.


The purpose of this repo is to demonstrate how a cucumber scenario can be written in such a way as to be implementation agnostic, achieving this using dependency injection. Depending on one system property the tests will either run the mobile implementation of the application under test, or the desktop implementation.

The Scenario

It's pretty simple: webdriver loads Bing, searches for "cucumber", and verifies that the first hit in the search results contains the word "Cucumber".

The dependency injection bit

The project is set up such that Page Objects are implementation specific, i.e. there's a MobileBingHomePage and a DesktopBingHomePage, but both implement a single BingHomePage interface. The decision between which implementation is used is determined at runtime through the use of spring Profiles. If the command line argument is set to desktop then the step definition class will be injected with DesktopBingHomePage, or, if it's set to mobile then MobileBingHomePage will be injected.

The same happens with the Page Objects themselves. They all need a WebDriver instance so one is injected in. Depending, again, on which spring profile is set, the Page Object will be injected with a WebDriver instance that is either set up with "desktop" dimensions, or with "mobile" dimensions and appropriate user-agent.

That's about the sum of it.

Note, this is just one way to arrange things. This repo is to demonstrate how all the pieces work together, not their optimal arrangement - that'll be determined by your own unique requirements.


To run the feature with a "desktop browser":

mvn clean verify

To run the feature with a "mobile browser":

mvn clean verify

Tested with...

On 14th Jan 2019, with the following setup, It Worked On My Machine:

  • Macbook Pro 13" 2018
  • macOS 10.14.2 (18C54)
  • Maven 3.6.0
  • Java openjdk 11.0.1 2018-10-16
  • Chrome for Mac 71.0.3578.98 (Official Build) (64-bit)
  • ChromeDriver 2.45.615355


  • No proxy required
  • No corporate SSL MiTM in place
  • Microsoft haven't tinkered with