1

My Spring.Net context file for an NUnit class is not being found. My question is where do I put the Spring.Net context file so it can be found by the NUnit test? I'm using Spring.Net 1.3.2 (that is the version defaulted to by NuGet) and the Spring.Net integration with NUnit. The unit test is not finding the context file specified via AbstractDependencyInjectionSpringContextTests.

This is my first .Net project project so I'm probably missing something basic. In my solution my main C# project would have an assembly/project name of MyAssembly. Then I created a test project in the same solution with a name MyAssemply.Test. Initially I created my unit test class and its context file in the root directory of the project. The context file is flagged as an Embedded Resource and Do Not Copy. The Resource URI is "assembly://MyAssembly.Test/MyNamespace/EmailManagerTestContext.xml". I keep getting a null pointer.

So I tried using the AssemblyResource class without extending the Spring.Net AbstractDependencyInjectionSpringContextTests class. That also came up null and did not tell me where it was expecting the file to be. I also used Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) and found that the project was being built in the temp directory on another drive. I tried moving the class and context XML file into the folder with the same name as the namespace, but that did not work. I've seen some examples where the directory structure is not the same as the namespace as it is with Java packages, so I am a little confused. Thank you for any help.

  • Hard to tell what is your actual problem. One thing: the temp location probably results from the NUnit setting to "shadow copy" the test assemblies. Also: I'm not sure about the embedded resource thing. Maybe this helps: http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file – Dio F Jan 10 '14 at 13:31
  • Can you add your config file? – Najera Jan 14 '14 at 01:23

1 Answers1

0

To setup Nunit with Spring.Net you will need:

  1. Setup your development environment: Install Nunit test adapter.

  2. Setup your project: Add a nunit reference, and Spring.Net Nunit Reference.

  3. Your test must derives from AbstractDependencyInjectionSpringContextTests class.

  4. Implement ConfigLocations property get with your Embedded Resource path. Make sure this path, is a most frequent error factor to unsucess to configure that.

`

string resourcePath = string.Format( "assembly://{0}/{1}/{2}.xml"
, currentType.Assembly.GetName().Name
, currentType.Namespace
, currentType.Name);

`

  1. On constructor class you may opt for autowire seting the AutowireMode property.

this.AutowireMode = Spring.Objects.Factory.Config.AutoWiringMode.ByName; and this.DependencyCheck = true;

`

namespace Spring.Objects.Factory.Config
{
    // Summary:
    //     The various autowiring modes.
    [Serializable]
    public enum AutoWiringMode
    {
        // Summary:
        //     Do not autowire.
        No = 0,
        //
        // Summary:
        //     Autowire by name.
        ByName = 1,
        //
        // Summary:
        //     Autowire by System.Type.
        ByType = 2,
        //
        // Summary:
        //     Autowiring by constructor.
        Constructor = 3,
        //
        // Summary:
        //     The autowiring strategy is to be determined by introspection of the object's
        //     System.Type.
        AutoDetect = 4,
    }
}

`

  1. I'm using ByName. It represents which the name of defined object on Spring.Net XML must match exactly my property name. To make the configuration more easier, use alias to reusing your native configurations on your tests.

Best Regards