Wanted to Unit Test a method in the following Class
public class DeviceAuthorisationService : IDeviceAuthorisationService
{
private DeviceDetailsDTO deviceDetailsDTO = null;
private IDeviceAuthorisationRepositiory deviceAuthorisationRepositiory;
public DeviceAuthorisationService(IDeviceAuthorisationRepositioryService paramDeviceAuthorisationRepository)
{
deviceAuthorisationRepositiory = paramDeviceAuthorisationRepository;
}
public void AuthoriseDeviceProfile(long paramUserID, string paramClientMakeModel)
{
if (deviceDetailsDTO == null)
GetCellPhoneDetails(userID);
if (deviceDetailsDTO.IsDeviceSelected == false)
throw new SomeCustomExceptionA();
if (deviceDetailsDTO.CellPhoneMakeModel.ToLower() != paramClientMakeModel.ToLower())
throw new SomeCustomExceptionB;
}
public void UpdateDeviceStatusToActive(long userID)
{
if (deviceDetailsDTO == null)
throw new InvalidOperationException("UnAuthorised Device Profile Found Exception");
if (deviceDetailsDTO.PhoneStatus != (short)Status.Active.GetHashCode())
deviceAuthorisationRepositiory.UpdatePhoneStatusToActive(deviceDetailsDTO.DeviceID);
}
private void GetCellPhoneDetails(long userID)
{
deviceDetailsDTO = deviceAuthorisationRepositiory.GetSelectedPhoneDetails(userID);
if (deviceDetailsDTO == null)
throw new SomeCustomException()
}
}
Note:
How will we unit test this method?
Any design suggestions to make this testable is most welcome Thanks in advance.
You can do it by using ref keyword. First Create a void method with a ref parameter in your Class Library Solution. Out goal is to test this void method in unit test. Calling the methodRef in Test Class.
Your modified program can ask for user input, call the function, then print the output. A testing framework can supply the input from a list of inputs to test, and check the outputs against the expected values. To test as-is, you'd have to intercept the input and output streams.
In this example, we have learned that how we can JUnit Test Void Method. We have also learned that how to catch the exception if it is thrown by a void method. Actually testing mechanism is same for all methods, but void methods are special as we don't have any returning value to be matched for testing.
Since your method returns void, it probably has some side-effect that you can test/assert on.
In your case, an option would be to provide a mock instance of IDeviceAuthorisationRepositioryService
. You can then check if a call to UpdatePhoneStatusToActive
has happened. Here is a solution using Moq:
var mock = new Mock<IDeviceAuthorisationRepositioryService>();
var service = new DeviceAuthorisationService(mock.Object);
service.UpdateDeviceStatusToActive(....);
mock.Verify(x => service.UpdatePhoneStatusToActive(), Times.Never());
If a method is void, then it should have some observable side-effect - otherwise it's pointless. So instead of testing the return value, you test the side-effects. In this case, it looks like those a probably around which exceptions are thrown in which situations.
(Here, "throws an exception" is deemed a side-effect; you could also think of it as an implicit kind of return value of course...)
Inject a mocked repository. Test if certain methods on the repository are called.
You can set exception expectancies on your unit tests. In nUnit it looks like this:
[Test]
[ExpectedException(typeof(InvalidOperationException))]
public void TestAuthoriseFail()
{
// do something that should make the tested method throw the exception
}
Even if your method returns void, it must be doing something that is useful for you (otherwise it would be a pointless method to have).
From your code, I'm guessing there are essentially 2 flavours of 'useful' things that the AuthoriseDeviceProfile
method is doing:
GetSelectedPhoneDetails
method on the IDeviceAuthorisationRepositiory
Therefore to unit test the method, you should do two things that correspond to this:
IDeviceAuthorisationRepositiory
and have it record and/or assert whether GetSelectedPhoneDetails
is calledIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With