Pergunta

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?

Foi útil?

Solução

Have you thought about using Spring @Configurable, then even instances created by a normal new will become spring managed beans.

@See Spring Reference Chapter 7.8.1 Using AspectJ to dependency inject domain objects with Spring

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top