将Spring-wired库与机器人框架结合使用

我有一个精心设计的Spring bean设置用于集成测试。 现在,我正在研究编写一个Robot库,以便将我的测试数据创建/行为执行/断言方法暴露给Robot测试。

但是,我从Robot Framework用户指南中了解到,Robot只能通过调用构造函数来实例化库类。 这是一个无赖,因为我宁愿让我的实例由Spring管理。

理想情况下,我希望能够为Robot提供应用程序上下文的路径和库的bean名称。 如果做不到这一点,我希望Robot能够调用静态工厂方法而不是构造函数,所以我不会被迫创建一个新实例。

我想到的一个解决方法是在静态初始化程序中创建Spring上下文,并通过从该上下文中提取bean来连接我的依赖项。

我原来的课看起来像:

public class MyAwesomeTests { @Autowired private ThisHelper thisHelper; @Autowired private ThatHelper thatHelper; // implementations of test steps and such } 

所以我会改变上面protected @Autowired字段,并创建一个静态初始化Spring上下文并定义一个Robot友好构造函数的子类:

 public class RobotFriendlyTests extends MyAwesomeTests { private static final ApplicationContext CONTEXT = new ClassPathXmlApplicationContext(...); public RobotFriendlyTests() { this.thisHelper = (ThisHelper) CONTEXT.getBean("thisHelper"); this.thatHelper = (ThatHelper) CONTEXT.getBean("thatHelper"); } } 

这应该有效,但感觉有些笨重。 有没有更好的方法我应该考虑? 更好的是,是否有一个Robot扩展已经为我做了这个?

您是否考虑过使用Spring @Configurable ,那么即使是普通new创建的实例也会成为Spring托管bean。

@See Spring Reference Chapter 7.8.1使用AspectJ依赖于使用Spring注入域对象

有一个Robot Framework扩展,支持使用Spring连接测试库,请查看: http : //code.google.com/p/robotframework-javalibcore/wiki/SpringLibrary

我不完全确定它是否支持你的情况,因为我对Spring一点都不熟悉。