tavier
tavier

Reputation: 1794

How to mock external dependencies using JustMock Lite

I am using free version of JustMock to mock some external dependencies like GetUserSettingsResponse which is a sealed class. The problem is that the free version does not allow us to mock sealed classes. I cannot use the full version because of some reasons. Below is my code sample:

public GetUserSettingsResponse GetUserSettingsResponse(string emailAddress)
{
    var response = service.GetUserSettings(
        emailAddress,
        UserSettingName.ExternalEwsUrl,
        UserSettingName.InternalEwsUrl,
        UserSettingName.EwsSupportedSchemas);
        return response;
}

This is the method I am trying to mock as below:

[TestMethod]
public void GetExchangeService()
{
    //Arrange            
    string url = "";
    GetUserSettingsResponse response;
    ObjectOfMyClass.Arrange(x => x.GetUserSettingsResponse(Arg.IsAny<string>())).Returns(new GetUserSettingsResponse()); //this is where it throws exception saying that only non-sealed classes can be mocked with lite version of just mock

}

Edit: My application is a Web service which is basically using EWS managed APIs to get the email account details from the exchange server. So, in order to start the communication I am first doing the AutoDiscovery to get the url out of email address of the user. So the subject under test is below method which internally calls GetUserSettingsResponse method:

public class MyClass
{
    public ExchangeService GetExchangeService(
                string userName,
                string password,
                int version,
                string emailAddress,
                string userId)
    {
        AutoDiscoverService service = new AutodiscoverService();
        service.UseDefaultCredentials = false;
        service.Credentials = new NetworkCredential(userName, password);
        service.EnableScpLookup = true;

        service.ReturnClientRequestId = true;

        service.PreAuthenticate = false;

        service.RedirectionUrlValidationCallback = RedirectionUrlValidationCallback;

        var url = string.Empty;
        var response = GetUserSettingsResponse(emailAddress);
        if (response.TryGetSettingValue(UserSettingName.ExternalEwsUrl, out settingValue)
            || response.TryGetSettingValue(UserSettingName.InternalEwsUrl, out settingValue))
        {
            ewsurl = settingValue;
        }
        var exchangeService = new ExchangeService((ExchangeVersion)version)
                                      {
                                          Credentials = new WebCredentials(userName, password),
                                          KeepAlive = true,
                                          Url = new Uri(ewsurl)
                                      };
        return exchangeService;
    }
}

Upvotes: 0

Views: 198

Answers (0)

Related Questions