将 Spring-wired 库与 robotsframework 结合使用
我有一个用于集成测试的精心设计的 Spring bean 设置。现在,我正在考虑编写一个机器人库,以向机器人测试公开我的测试数据创建/行为执行/断言方法。
但是,我从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
}
因此,我将上面的 @Autowired
字段更改为 protected
,并创建一个子类来静态初始化 Spring 上下文并定义一个机器人友好的构造函数:
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");
}
}
这应该可行,但感觉有点笨拙。我应该考虑更好的方法吗?更好的是,是否有一个机器人扩展已经可以为我做到这一点?
I have an elaborate Spring bean setup for integration tests. Now I'm looking into writing a Robot library to expose my test data creation / behavior execution / assertion methods to Robot tests.
However what I understand from the Robot Framework user guide is that Robot can only instantiate library classes by calling a constructor. This is a bummer because I'd rather have my instances managed by Spring.
Ideally, I'd want to be able to give Robot the path to the application context and the bean name for the library. Failing that, I'd want Robot to be able to invoke a static factory method rather than a constructor, so I'm not forced to create a new instance.
One workaround I thought of is to create the Spring context in a static initializer and wire my dependencies by fetching beans from that context.
My original class looks like:
public class MyAwesomeTests {
@Autowired
private ThisHelper thisHelper;
@Autowired
private ThatHelper thatHelper;
// implementations of test steps and such
}
So I would change the above @Autowired
fields to be protected
, and create a subclass that statically initializes the Spring context and defines a Robot-friendly constructor:
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");
}
}
This should work, but it feels somewhat clunky. Is there a better way I should consider? Better yet, is there a Robot extension that already does this for me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过使用Spring
@Configurable
,那么即使是由普通new
创建的实例也将成为Spring管理的bean。@参见Spring参考章节7.8.1 使用AspectJ通过Spring依赖注入域对象
Have you thought about using Spring
@Configurable
, then even instances created by a normalnew
will become spring managed beans.@See Spring Reference Chapter 7.8.1 Using AspectJ to dependency inject domain objects with Spring
有一个 Robot Framework 扩展支持使用 Spring 连接测试库,请查看: http://code.google.com/p/robotframework-javalibcore/wiki/SpringLibrary
我不完全确定它是否支持你的情况,因为我对 Spring 一点也不熟悉。
There's a Robot Framework extension that supports using Spring to wire test libraries, take a look at: http://code.google.com/p/robotframework-javalibcore/wiki/SpringLibrary
I am not entirely sure whether it supports your case since I am not familiar at all with Spring.