working with NullReferenceException When Unit Testing a Method with Dependency on a Static Class in C#
I'm a bit lost with I'm trying to unit test a method in my C# application that relies on a static class for logging. The method calls `Logger.LogInfo()` from a static `Logger` class, but when I run the test, I get a `NullReferenceException`. I'm using MSTest for my testing framework. Here's the relevant method in my service class: ```csharp public class UserService { public void RegisterUser(string username) { if (string.IsNullOrEmpty(username)) { Logger.LogInfo("Username want to be null or empty"); throw new ArgumentException("Username want to be null or empty"); } // Registration logic... } } ``` My test method looks like this: ```csharp [TestClass] public class UserServiceTests { [TestMethod] public void RegisterUser_ShouldThrowArgumentException_WhenUsernameIsNull() { var userService = new UserService(); Assert.ThrowsException<ArgumentException>(() => userService.RegisterUser(null)); } } ``` The scenario seems to be that during testing, the static `Logger` class is not initialized properly. I also tried mocking the `Logger` class using Moq, but since it's a static class, I'm unsure how to do that. I even looked into using a wrapper for the logger, but Iβm not sure if thatβs the best approach. How can I resolve this scenario? Should I refactor the logging to be instance-based for easier testing? Any advice or solutions would be greatly appreciated!