0

need to mock the HttpResponseMessage in my Unit Test. using .net core 3.1/ getting the following error:

Newtonsoft.Json.JsonSerializationException : Could not create an instance of type System.Net.Http.HttpContent. Type is an interface or abstract class and cannot be instantiated. Path 'httpResponseMessage.Content.Headers', line 206, position 16.

Deserialize Code:

public static T GetObjectFromFile<T>(string fileName) where T : class
    {

        var directory = AppDomain.CurrentDomain.BaseDirectory;
        var path = string.Format("{0}\\Stubs\\{1}", directory, fileName);  
        string stubContent = System.IO.File.ReadAllText(path);

        T res = JsonConvert.DeserializeObject<T>(stubContent, new JsonSerializerSettings
        {
            NullValueHandling = NullValueHandling.Ignore,
            ContractResolver = new PrivateResolver(),
            ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor

        });

        return res;
    }

T is (for the example):

public class ResponseWithHttpResponseMessage
{
    
    public HttpResponseMessage HttpResponseMessage { get; set; }
}

JSON example:

{ "HttpResponseMessage": { "Version": "1.1", "Content": { "Headers": [ { "Key": "Content-Type", "Value": [ "application/json" ] } ] }, "StatusCode": 200, "ReasonPhrase": "OK", "Headers": [ { "Key": "X-Backside-Transport", "Value": [ "OK OK,FAIL FAIL,OK OK" ] }, { "Key": "Connection", "Value": [ "Keep-Alive" ] }, { "Key": "Transfer-Encoding", "Value": [ "chunked" ] }, { "Key": "Date", "Value": [ "Wed, 23 Dec 2020 14:04:56 GMT" ] }, { "Key": "X-Global-Transaction-ID", "Value": [ "1fd23d895fe34e870c0158c2" ] }, { "Key": "User-Agent", "Value": [ "IBM-APIConnect/5.0" ] }, { "Key": "Access-Control-Expose-Headers", "Value": [ "APIm-Debug-Trans-Id, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-Global-Transaction-ID" ] }, { "Key": "Access-Control-Allow-Origin", "Value": [ "*" ] }, { "Key": "Access-Control-Allow-Methods", "Value": [ "GET" ] }, { "Key": "X-RateLimit-Limit", "Value": [ "name=rate-limit,92;" ] }, { "Key": "X-RateLimit-Remaining", "Value": [ "name=rate-limit,81;" ] } ], "TrailingHeaders": [], "RequestMessage": { "Version": "1.1", "Content": null, "Method": { "Method": "GET" }, "RequestUri": "https://EXAMLE?F=I,C,", "Headers": [ { "Key": "M-ID", "Value": [ "1" ] } ], "Properties": {} }, "IsSuccessStatusCode": true } }

seems that the headers class is sealed and therefore it is not possible to mock it. but is there any chance that a workaround is exist?

udi
  • 21
  • 1
  • 3
  • There may be different solution for this but with the details you have provided it is not possible to guess what you are trying to do and how you are doing it. It will be helpful if you share the actual code which you are unit testing and also to unit test code in the question. – Chetan Dec 27 '20 at 07:40
  • *headers class is sealed and therefore it is not possible to mock it* - have you read e.g. https://stackoverflow.com/questions/6484/how-do-you-mock-a-sealed-class – Caius Jard Dec 27 '20 at 09:59

0 Answers0