1

我想手动配置我的 picocontainer 为我的黄瓜构建配置配置对象的实例。IE,@ConfigurationProperties在 Spring 中提供配置对象。

但是,目前尚不清楚如何做到这一点。该网站提供了大量关于如何操作容器的文档,但没有围绕这些示例的上下文。我正在使用 Cucumber maven 构建,使用cucumber-picocontainer依赖项。

理想情况下,PicoContainer 将能够从主 yaml/ 配置文件(如 Spring)中获取依赖项,但如果我可以手动将它们输入到正在运行的容器中,那也可以。

为了清楚起见,我知道我可以这样做:

@RunWith(Cucumber.class)
public class RunWithCucumberTest{

    public PicoContainer getContainer(){
        MutablePicoContainer pico = new DefaultPicoContainer();
        //do the config, inject onjects, etc

        return pico;
    }
}

但这并不意味着 this 返回的实例实际上是用来注入我的属性的。

总之,我正在寻找一种使用 pico 容器执行以下操作之一的方法:

  • 自动能够创建配置类,通过文件(yaml,,properties等)配置
  • 手动配置正在运行的容器,自己从配置中创建对象并将它们交给 pico 稍后注入
4

1 回答 1

2

这些功能不可用。Picocontainer 是一个非常简单的依赖注入框架,Cucumbers 对它的使用更加简单。为了有效地使用它,尽量不要像在构建应用程序图一样使用它。而是使用它来构建用于测试应用程序的 API。

通常,您最终会包装无法实例化自己的对象以及无处不在的包装器。这不是很灵活,但通常不需要这种灵活性。您的步骤定义 + 支持类应该操纵被测应用程序。

如果这对您不起作用,您也可以使用cucumber-guicecucumber-spring分别使用 Guice 和 Spring 进行依赖注入。

你也可以滚动你自己的实现PicoFactory,它的代码量很小,如果它是唯一的ObjectFactory实现,它将被自动使用。否则,您可以使用object-factory属性/选项。

public class Configuration {

    private final Properties someProperties = new Properties();

    public Configuration() throws IOException {
        someProperties.load(Properties.class.getResourceAsStream("some.properties"));
    }

    public String entryPoint() {
        return someProperties.getProperty("example.entry-point", "localhost:7070/entry");
    }

}

public class StepDefinitions {

    private final Configuration configuration;
    private Application application;

    public StepDefinitions(Configuration configuration) {
        this.configuration = configuration;
    }

    @Given("a thing is done")
    public void aThingIsDone(){
        String entryPoint = configuration.entryPoint();
        this.application = //... create application with entry point
    }

}
于 2020-02-27T23:12:54.473 回答