0

Can I make use of Spring.NET's base classes (a la AbstractDependencyInjectionSpringContextTests) for loading an application context and performing dependency injection using MSTest?

I'm trying to develop transactional scenarios that will ultimately hit a live integration database and rollback. I have a set of base classes that extend from Spring's that perform a bit of initialization necessary on our application platform.

I suppose I can abstract this and use the Before and After hooks, but I'd like to keep it as similar as possible to what we already have and continue to make use of autowired dependency injection.

pcw216
  • 126
  • 2

2 Answers2

0

I would suggest that you take a look that the ContextInjection-feature of SpecFlow. It's a simple but rather powerful feature that allows you to inject an object right into your step definition class.

For example you could configure your transactional component the way you want and then just inject the configured object right into your steps. Create a "use-only-in-test"-version of it so to speak.

For transactions I've used the Before/AfterScenario hooks before but that is a bit hidden as you say. It works fine though.

Hope this helps

Marcus Hammarberg
  • 4,762
  • 1
  • 31
  • 37
  • ContextInjection requires classes with empty constructors, and I don't think it's well suited for dependency injection. Before/After I think is probably the best bet, but I specifically wanted to re-use the Spring.NET base classes for tests. – pcw216 May 03 '13 at 15:01
0

I was able to extend the Spring test base class and hook up test initialize/cleanup with Specflows Before/After scenario. My step class, at least, will be auto-wired with the necessary dependencies.

https://github.com/techtalk/SpecFlow/wiki/Hooks

[Binding]
public class MySteps : AbstractDependencyInjectionSpringContextTests
{

    protected override string[] ConfigLocations
    {
        get
        {
            return new string[] { 
              "assembly://My.Assembly/Path.To.Config/My.config" };
        }
    }

    public IMyService MyService { get; set; }

    [Before]
    public void BeforeTest()
    {

        this.TestInitialize();
    }

    [After]
    public void AfterTest()
    {
        this.TestCleanup();
    }
}
pcw216
  • 126
  • 2