Gautam Kumar
Gautam Kumar

Reputation: 83

Nunit unit test for WPF application gives NullReference exception

While i was writing unit test(NUnit) for wpf application. Got an error 'object reference not set to an instance'

at the bottom level i can see the line which raise exception

 Application.Current.Dispatcher.Invoke(DispatcherPriority.Send,
          new Action(() => loc.ResolveLocalizedValue(out ui)));

this code is used in helper class library. Thanks in advance. Guys please suggest how can i resolve this.

Upvotes: 0

Views: 587

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81243

Application.Current will be null in your case. It returns application object for the current AppDomain which gets set only when application is run.

Test cases are to test business logic i.e. to test ViewModel layer. Code which you want to test should not contain any reference to UI component.


More details can be found here - Application.Current is null in unit test cases. Workaround is provided in the link but I strongly discourage the use of Application.Current in your testcases or usage of it in ViewModel layer which needs to be unit tested.

Upvotes: 2

Related Questions