title: Unit Testing custom ConfigurationElement & ConfigurationElementCollection
body: <p>I have created a custom <code>ConfigurationElement</code> and <code>ConfigurationSection</code> to make it easier to set up a host of application parameters at startup. However, I'd really like to unit test this logic. </p>
  
  <p><strong>ServiceConnection</strong></p>
  
  <pre><code>public class ServiceConnection : ConfigurationElement
  {
      [ConfigurationProperty("locationNumber", IsRequired = true)] 
      public string LocationNumber
      {
          get { return (string) base["locationNumber"]; }
          set { base["locationNumber"] = value; }
      }
  
      [ConfigurationProperty("hostName", IsRequired = true)]
      public string HostName
      {
          get { return (string) base["hostName"]; }
          set { base["hostName"] = value; }
      }
  
      [ConfigurationProperty("port", IsRequired = true)]
      public int Port
      {
          get { return (int) base["port"]; }
          set { base["port"] = value; }
      }
  
      [ConfigurationProperty("environment", IsRequired = true)]
      public string Environment
      {
          get { return (string) base["environment"]; }
          set { base["environment"] = value.ToUpper(); }
      }
  
      internal string Key
      {
          get { return string.Format("{0}|{1}", LocationNumber, Environment); }
      }
  }
  </code></pre>
  
  <p><strong>ServiceConnection Collection</strong></p>
  
  <pre><code>[ConfigurationCollection(typeof(ServiceConnection), AddItemName = "service", CollectionType = ConfigurationElementCollectionType.BasicMap)]
  public class ServiceConnectionCollection : ConfigurationElementCollection
  {
      protected override ConfigurationElement CreateNewElement()
      {
          return new ServiceConnection();
      }
  
      protected override object GetElementKey(ConfigurationElement element)
      {
          return ((ServiceConnection) element).Key;
      }
  
      public ServiceConnection Get(string locationNumber, string environment = "PRODUCTION")
      {
          return (ServiceConnection) BaseGet(string.Format("{0}|{1}", locationNumber, environment));
      }
  
      public ServiceConnection this[int index]
      {
          get { return (ServiceConnection)BaseGet(index); }
          set
          {
              if (BaseGet(index) != null)
              {
                  BaseRemoveAt(index);
              }
              BaseAdd(index, value);
          }
      }
  }
  </code></pre>
  
  <p><strong>Some Test XML</strong></p>
  
  <pre><code>&lt;MyServiceConnections&gt;
      &lt;service locationNumber="0AB0" hostName="DEVSERVER" port="1234" environment="DEVELOPMENT" /&gt;
      &lt;service locationNumber="0AB0" hostName="BETASERVER" port="1234" environment="BETA" /&gt;
      &lt;service locationNumber="0AB0" hostName="PRODSERVER" port="1234" environment="PRODUCTION" /&gt;
  &lt;/MyServiceConnections&gt;
  </code></pre>
  
  <p>In my production code, I'm using <code>ConfigurationManager</code> to retrieve the <code>ServiceConnection</code> but am not sure how to create a test that bypasses the manager altogether.</p>
  
  <p>I'm looking to retrieve a ServiceConnection object and make sure all the fields match the input that I set up in the test XML. I'd also like to test functionality when a user failed to populate one or more of the fields.</p>