maui icon indicating copy to clipboard operation
maui copied to clipboard

Setting MainPage in a unit test causes a null reference exception

Open TheresasAccount opened this issue 10 months ago • 3 comments

Description

Before upgrading to 8.0.20 all my unit tests were passing, now this simple line causes a null reference exception: new Microsoft.Maui.Controls.Application() { MainPage = new Page() }

Steps to Reproduce

Create a new Maui Project Create an nunit test project and add a reference to the project Add .net8.0 to the target frameworks and this condition on OutputType: Condition="'$(TargetFramework)' != 'net8.0'" In the setup of the nunit stub add this line: var app = new Application() { MainPage = new Page() };

Expected: Makes a new application with a new MainPage Actual: Message:  System.NullReferenceException : Object reference not set to an instance of an object.

Stack Trace:  <.ctor>b__7_0() Lazy1.ViaFactory(LazyThreadSafetyMode mode) Lazy1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor) Lazy`1.CreateValue() Application.get_SystemResources() ResourcesExtensions.GetMergedResources(IElementDefinition element) Application.set_MainPage(Page value) Tests.Setup() line 8 RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Link to public reproduction project repository

https://github.com/TheresasAccount/UnitTestNullReference

Version with bug

8.0.20 SR4

Is this a regression from previous behavior?

Yes, this used to work in .NET MAUI

Last version that worked well

8.0.14 SR3.1

Affected platforms

Other (Tizen, Linux, etc. not supported by Microsoft directly)

Affected platform versions

Unit testing

Did you find any workaround?

No

Relevant log output

Message: 
System.NullReferenceException : Object reference not set to an instance of an object.

  Stack Trace: 
<.ctor>b__7_0()
Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
Lazy`1.CreateValue()
Application.get_SystemResources()
ResourcesExtensions.GetMergedResources(IElementDefinition element)
Application.set_MainPage(Page value)
Tests.Setup() line 8
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

TheresasAccount avatar Apr 10 '24 19:04 TheresasAccount

Verified this issue with Visual Studio 17.10.0 Preview 3(8.0.20). Can repro this issue with sample project. image

Zhanglirong-Winnie avatar Apr 11 '24 05:04 Zhanglirong-Winnie

Can you test with the latest nightly build? https://github.com/dotnet/maui/wiki/Nightly-Builds

PureWeen avatar Apr 24 '24 21:04 PureWeen

@PureWeen Tried with 8.0.40-nightly.10539 still the same issue with the same stack trace

TheresasAccount avatar Apr 25 '24 15:04 TheresasAccount

I don't know why this suddenly fail, the your null ref is coming from a missing DependencyService of type ISystemResourcesProvider. Look at what we do in our own unittests with MockPlatformServices.cs

StephaneDelcroix avatar May 14 '24 10:05 StephaneDelcroix