Resource Reference key not found exception while unit testing

by shubham sharma   Last Updated June 29, 2018 16:05 PM

I have written a unit test for a converter.

Converter -

    public class KvpOverrideLevelToColorConverter : IValueConverter
        private const string redBrush = "redBrush";
        private const string blueBrush = "blueBrush";

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            if (value != null)
                KvpOverrideLevel level = (KvpOverrideLevel)value;
                switch (level)
                    case KvpOverrideLevel.ReportTemplate:
                        return Application.Current.FindResource(redBrush);
            return Application.Current.FindResource(blueBrush);

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            throw new NotImplementedException();

Unit test -

    public class MockTests
        private static Application application = new Application() { ShutdownMode = ShutdownMode.OnExplicitShutdown };

    public class KvpOverrideLevelToColorConverterTest : MockTests
        private KvpOverrideLevelToColorConverter _underTest;

        public void KvpOverrideLevelToColorConvertTest()

            _underTest = new KvpOverrideLevelToColorConverter();
            var result = Application.Current.FindResource("redBrush");

            var convertedvalue = _underTest.Convert(KvpOverrideLevel.ReportTemplate, typeof(int), null, CultureInfo.InvariantCulture);
            Assert.AreEqual(result, convertedvalue);

            convertedvalue = _underTest.Convert(null, typeof(int), null, CultureInfo.InvariantCulture);
            Assert.AreEqual(Application.Current.FindResource("blueBrush"), convertedvalue);

But everytime, I debug this test, it throws an exception : bluebrush and redBrush resource not found. I checked the references and all references are in place. Here KvpOverrideLevel is an enum where KvpOverrideLevel.ReportTemplate = 3. Is there any other way to tackle this?

Answers 1

There are two issues:

First, Resources are searched for in a specific order in a WPF application, starting with the Resources dictionary of the Application. Because you call the function from a test setup instead of the normal application, some "magic" code automatically embedded during compile into the Application gets not executed, and the dictionary is not filled.

You could try to do that in your MockTests class, start with

ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Add("key", "value"); // add your resources here
Application.Current.Resources = dictionary;

Second, the test is useless. Its way of determining the expected outcome uses the same lines of code as does the implementation under test.

Bernhard Hiller
Bernhard Hiller
May 30, 2018 14:21 PM

Related Questions


Updated February 18, 2017 10:05 AM

What does stubbing mean in programming?

Updated March 03, 2017 13:05 PM

Integration tests, but how much?

Updated February 28, 2017 08:05 AM

i want a postman's counterpart for mobile developers

Updated December 05, 2017 07:05 AM

How to manage non-unit tests in a project?

Updated April 26, 2017 14:05 PM