Open mpkorstanje opened 2 months ago
I currently don't have much time to implement this. So if anyone wants to help out, that would be much appreciated.
To implement this the following needs to be done.
PicoBackend
class that implements the Backend
interface and scans the glue path for classes that implement Provider
. This can be quite similar to the SpringBackend
. But swapping out .filter(SpringFactory::hasCucumberContextConfiguration)
for the check that a class implements Provider
.PicoFactory
separate the Provider
classes from the regular glue classes register them with the Pico Container at startup.On second thought. While the Provider
interface is quite nice, there are some problems.
So it might be nicer to declare a proper factory object:
@Factory
public class WebDriverFactory {
private final Project project;
public WebDriverFactory(Project project) {
this.project = project;
}
@Bean
public WebDriver webDriver() {
return // create WebDriver
}
@Bean
public SecuredPage provide(WebDriver driver) {
return new SecuredPage(driver, project, "example", "top secret");
}
}
But this isn't great either. Because now we have 2 annotations, to mark the factory class and methods involved. I'm also not sure if PicoContainer supports something like this. Or what circular dependencies might be created this way.
This could be simplified a bit by requiring the factory methods to be static.
public class WebDriverFactory {
@Bean
public static WebDriver webDriver() {
return // create WebDriver
}
@Bean
public static SecuredPage provide(WebDriver driver, Project project) {
return new SecuredPage(driver, project, "example", "top secret");
}
}
But at point we may as well recommend that people use a fully featured DI such as Spring or Guice. Perhaps what we need instead are some good examples.
🤔 What's the problem you're trying to solve?
Currently when using Cucumber with
cucumber-picocontainer
it is not possible to inject dependencies that that ultimately do not have a zero-argument constructor or require some setup. For example:For example
And it is also not possible to inject dependencies that partially depend on other inject dependencies and partially on some configuration.
For example we may want to inject
PageObjects
that partially depend on other dependencies and partially on some configuration:✨ What's your proposed solution?
Support Pico Containers
Providers
so that it becomes possible to write.⛏ Have you considered any alternatives or workarounds?
The current work around is to create a container object. But that quickly becomes ugly once multiple dependencies are involved. The other alternative is to use
cucumber-spring
orcucumber-guice
but both are quite complicated indeed.📚 Any additional context?