A lightweight library for using dependency injection in cucumber steps for Micronaut based project.
-
Cucumber steps dependency injection using the Micronaut dependency injection mechanism. Only constructor dependency injection is supported
-
Bean discovery by type
-
Bean discovery by name. See micronaut @Named annotation usage.
-
Bean discovery by parameterized type
- Clone the repo
[email protected]:david-romero/cucumber-micronaut.git
- Build the library
mvn clean package
- Clone the repo
[email protected]:david-romero/shared-payments.git
- Install the library
mvn clean install
Add the maven dependency
<dependency>
<groupId>com.davromalc</groupId>
<artifactId>cucumber-micronaut</artifactId>
<version>0.1.0</version>
</dependency>
The Micronaut Object Factory needs to be configured as cucumber object factory.
import com.davromalc.cucumber.micronaut.MicronautObjectFactory;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import io.cucumber.junit.CucumberOptions.SnippetType;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class) //Cucumber does not support JUnit 5 yet
@CucumberOptions(
plugin = {"pretty", "html:target/features"},
glue = {"com.davromalc.cucumber.micronaut.examples.acceptance"},
objectFactory = MicronautObjectFactory.class,
features = "classpath:features", snippets = SnippetType.CAMELCASE)
public class CucumberRunnerITCase {
}
- Beans definitions
import javax.inject.Singleton;
@Singleton
public class World {
}
@Singleton
final class AddFriend implements UseCase<AddFriendParams, User> {
private final FriendRepository friendRepository;
public AddFriend(FriendRepository friendRepository) {
this.friendRepository = friendRepository;
}
@Override
public Either<Validation, User> execute(AddFriendParams params) {
//Implementation...
}
}
- Step implementation
package com.davromalc.cucumber.micronaut.examples.acceptance.steps;
public class AddFriendStep {
private final UseCase<AddFriendParams, User> addFriend;
private final FriendRepository friendRepository;
private final World world;
public AddFriendStep(@Named("addFriend") UseCase<AddFriendParams, User> addFriend, FriendRepository friendRepository,
World world) {
this.addFriend = addFriend;
this.friendRepository = friendRepository;
this.world = world;
}
@Given("a new friend with name {string}")
public void aNewFriendWithName(String friendName) {
world.setFriendParams(new AddFriendParams(friendName));
}
@When("the friend is added")
public void theFriendIsAdded() {
addFriend.execute(world.getFriendParams());
}
@Then("the new friend {string} has been saved")
public void theNewFriendHasBeenSaved(String newFriendName) {
final Optional<String> newFriendNameFromDatabase = friendRepository.findByName(newFriendName)
.map(User::getName)
.filter(newFriendName::equals)
.findAny();
assertThat(newFriendNameFromDatabase)
.matches(Optional::isPresent)
.get()
.asString()
.isEqualTo(newFriendName);
}
}
For more examples, please refer to the Examples Repository
See the open issues for a list of proposed features (and known issues).
- Releasing with GitHun Actions
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the Apache License. See LICENSE
for more information.
David Romero - @davromalc
Project Link: https://github.com/david-romero/cucumber-micronaut