Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Mock Call httpWebRequest using XrmFakedContext D365

(1) ShareShare
ReportReport
Posted on by 5

Hello,

I wanted to mock the call to the HttpWebRequest service web in the unit tests using the XrmFakedContext; in this example I show you the code of the test function and the function that calls the web service Test Class:

[TestMethod, TestCategory("Unit example")]
    public void test()
    { 
    
    //Initialisation
    fakeContext.Initialize(new List()
            {  
                customer,
                 
            });

        
        var inputs = new Dictionary { { "CustomerId", customer.ToEntityReference() }};

        var result = fakeContext.ExecuteCodeActivity(inputs, null);


        Assert.AreEqual(result["Exist"], true);
    }


the function that calls the web service in the customWorkFlow :

private ReponseClass CallServiceWeb(string someParams)
    {
        try
        {

            var request = (HttpWebRequest)WebRequest.Create(someParams);

            request.Method = "GET";
            request.KeepAlive = true;
            request.Accept = @"*/*";

            var credential = new NetworkCredential(someParams, someParams, someParams);
            var credentialCache = new CredentialCache();
            credentialCache.Add(new Uri(url), "NTLM", credential);
            request.Credentials = credentialCache;

            string response = null;

            using (var responseDetail = (HttpWebResponse)request.GetResponse())
            {
                using (var sr = new StreamReader(responseDetail.GetResponseStream()))
                {
                    response = sr.ReadToEnd();
                }
            }

            return this.Deserialize(response);
        }
        catch (Exception e)
        {
            return new ReponseClass () { erreurAppel = e };
        }
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Product updates

Dynamics 365 release plans