Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application.Current is null when calling from a unittest

Tags:

I have a method that I'm trying to call from a unit test. This method will in real life be run from a background thread. It uses some code to kick off in the invoke updates to the UI thread (using Application.Current.Dispatcher.BeginInvoke .... ).

However Application.Current is null when being called from the unit tests.

I don't really what to put an if (Application.Current !=null) around everything to fix.

Is there any other way around this?

_statusUpdates is an ObservableCollection

Below is the part of the code in the method I'm looking to test (it is more of an integration test than a unit test to be fair).

Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (EventHandler)delegate {     _statusUpdates.Add(new StatusUpdate     {         DateTime = DateTime.Now,         Message = "Checking For Messages"     }); }, null, null); 
like image 514
DermFrench Avatar asked Feb 12 '13 17:02

DermFrench


People also ask

Can current dispatcher be null?

Dispatcher? Application. Current is null when the test is executed, which leads to an exception. If you're using async/await in your ViewModel, you might have less need to use the Dispatcher at all, as async/await marshalls back to the Thread on which you've created the ViewModel, which is usually the UI-Thread.

What is the use of Unittest?

The main objective of unit testing is to isolate written code to test and determine if it works as intended. Unit testing is an important step in the development process, because if done correctly, it can help detect early flaws in code which may be more difficult to find in later testing stages.


2 Answers

The following code snippet works for me:

if (System.Windows.Application.Current == null)    { new System.Windows.Application { ShutdownMode = ShutdownMode.OnExplicitShutdown }; } 

IIRC, I had a problem where Application was null using a WPF control embedded in a WinForms application and that code snippet was suggested as a solution to the problem in another question on StackOverflow (sorry, can not find the source). It solves the same problem in unit tests (and I don't believe the ShutdownMode property needs to be explicitly set in that case).

like image 76
Shea Avatar answered Nov 09 '22 11:11

Shea


As already stated, you simply won't have an Application class during unit tests.

That said, there's an issue here I think needs addressing - by having code that relies on a defined static property, in your case Application.Current.Dispatch, you are now very tightly coupled to the specific implementation of that class, namely the WPF Application class, where you do not need to be.

Even if you simply wrap the idea of "the current root dispatcher" in a Singleton-style class wrapper, now you have a way of decoupling yourself from the vagaries of the Application class and dealing directly with what you care about, a Dispatcher:

Note, there are MANY MANY ways to write this, I'm just putting up the simplest possible implementation; hence, I will not be doing any multithreaded safety checks, etc.

public class RootDispatcherFetcher {      private static Dispatcher _rootDispatcher = null;       public static Dispatcher RootDispatcher      {          get           {              _rootDispatcher = _rootDispatcher ??                  Application.Current != null                       ? Application.Current.Dispatcher                      : new Dispatcher(...);              return _rootDispatcher;          }          // unit tests can get access to this via InternalsVisibleTo          internal set          {              _rootDispatcher = value;          }      } } 

Ok, now this implementation is only slightly better than before, but at least you now have finer control over access to the type and are no longer strictly dependent on the existence of an Application instance.

like image 36
JerKimball Avatar answered Nov 09 '22 10:11

JerKimball