2

Essentially I'm trying to write a test to deserialize the JSON response to the Customer object and Assert that the Firstname is "Darron"

JSON object

  {
        "Id": 615,
        "Customer": {
            "Id": 726,
            "MemberNodeId": 2257,
            "EmailAddress": "[email protected]",
            "Deleted": false,
            "LastModifiedBy": "CUSTOMER",
            "LastModifiedOn": "2021-06-02T08:54:38.243Z",
            "CreatedBy": "CUSTOMER",
            "CreatedOn": "2021-06-02T08:54:38.243Z"
        },
        "Title": {
            "Id": 1,
            "Title": "Mr"
        },
        "Firstname": "Darron",
        "Lastname": "Lips",
        "Gender": "Male",
        "DateOfBirth": "1977-11-21T00:00:00Z",
        "Height": 241.000000000,
        "Weight": 204.000000000,
        "LastModifiedBy": "CUSTOMER",
        "LastModifiedOn": "2021-06-02T08:54:38.257Z",
        "CreatedBy": "CUSTOMER",
        "CreatedOn": "2021-06-02T08:54:38.257Z"
    }
]

Customer model

   public class Customer
    {
        [JsonProperty("Id")]
        public int Id { get; set; }

        [JsonProperty("MemberNodeId")]
        public int MemberNodeId { get; set; }

        [JsonProperty("EmailAddress")]
        public string EmailAddress { get; set; }

        [JsonProperty("Deleted")]
        public bool Deleted { get; set; }

        [JsonProperty("LastModifiedBy")]
        public string LastModifiedBy { get; set; }

        [JsonProperty("LastModifiedOn")]
        public DateTime LastModifiedOn { get; set; }

        [JsonProperty("CreatedBy")]
        public string CreatedBy { get; set; }

        [JsonProperty("CreatedOn")]
        public DateTime CreatedOn { get; set; }
    }

    public class CustomerTitle
    {
        [JsonProperty("Id")]
        public int Id { get; set; }

        [JsonProperty("Title")]
        public string Title { get; set; }
    }

    public class Root
    {
        [JsonProperty("Id")]
        public int Id { get; set; }

        [JsonProperty("Customer")]
        public Customer Customer { get; set; }

        [JsonProperty("Title")]
        public string Title { get; set; }

        [JsonProperty("Firstname")]
        public string Firstname { get; set; }

        [JsonProperty("Lastname")]
        public string Lastname { get; set; }

        [JsonProperty("Gender")]
        public string Gender { get; set; }

        [JsonProperty("DateOfBirth")]
        public DateTime DateOfBirth { get; set; }

        [JsonProperty("Height")]
        public int Height { get; set; }

        [JsonProperty("Weight")]
        public int Weight { get; set; }

        [JsonProperty("LastModifiedBy")]
        public object LastModifiedBy { get; set; }

        [JsonProperty("LastModifiedOn")]
        public DateTime LastModifiedOn { get; set; }

        [JsonProperty("CreatedBy")]
        public string CreatedBy { get; set; }

        [JsonProperty("CreatedOn")]
        public DateTime CreatedOn { get; set; }
    }

}

and the Test

  [Test]
        public void CanGetCustomerDetails()
        {
            RestClient client = new RestClient(baseUrl);
            RestRequest request = new RestRequest("CustomerDetailsApi/GetAll", Method.GET);
            request.AddHeader("Authorization", $"Bearer {token}");
            IRestResponse response = client.Execute(request);

       
        }

so far I have tried

  var customer = client.Execute(request).Content;
        var responsea = JsonConvert.DeserializeObject<List<Customer>>(customer);
        Assert.AreEqual(responsea, "firstname");

and

   var customer = client.Execute(request).Content;
            var responsea = JsonConvert.DeserializeObject<Customer>(customer);

which results in:

"Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type "

5
  • Your example json is missing the opening [, is that a C+P error, or is your json really like that? Commented Jun 2, 2021 at 10:37
  • From the exception error, it seems that it is a question typo. Commented Jun 2, 2021 at 10:37
  • 2
    Your JSON doesn't match your classes. First of all, you should be deserialising into List<Root>, but you also need to modify that class to have a child class for the Title property. Also, those numbers for Height and Weight cannot be deserialised into int properties. Commented Jun 2, 2021 at 10:38
  • Did you try to Deserialize a List of Root objects instead? You will find other errors like the property Title defined as string instead of CustomerTitle and some values that cannot be assigned to integers (Width/Height) but the deserialization works and you can use that list to check for the firstname property Commented Jun 2, 2021 at 10:38
  • @DavidG it does not, well spotted thank you Commented Jun 2, 2021 at 11:04

3 Answers 3

2

Your JSON doesn't match your classes. First of all, you should be deserialising into List<Root>, but you also need to modify that class to have a child class for the Title property. Also, those numbers for Height and Weight cannot be deserialised into int properties.

So first of all, is your Root class:

public class Root
{
    public int Id { get; set; }
    public Customer Customer { get; set; }
    public CustomerTitle Title { get; set; } // Correct type used here
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Gender { get; set; }
    public DateTime DateOfBirth { get; set; }
    public double Height { get; set; } // double rather than int
    public double Weight { get; set; } // double rather than int
    public object LastModifiedBy { get; set; }
    public DateTime LastModifiedOn { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedOn { get; set; }
}

Now deserialise into correct type:

var responsea = JsonConvert.DeserializeObject<List<Root>>(customer);

Now you can assert something like this:

Assert.AreEqual(responsea[0].Firstname, "Darron");

Note that you don't need all the JsonProperty attributes if the property names already match.

Sign up to request clarification or add additional context in comments.

Comments

0

And what worked for me was simply to make my response as a "Content" as below:

var responsea = JsonConvert.DeserializeObject<List<Root>>(customer.Content);

Comments

-1

Try this:

  var customer = client.Execute(request).Content;
        var responsea = JsonConvert.DeserializeObject<List<Root>>(customer);
        Assert.AreEqual(responsea[0].Firstname , "Darron");

Essentially responsea[0].Firstname access the first element of the List take the Firstname property of it and assert with the value, which seems to be "Darron"

Your second try, essentially tries to convert an entire array to one object which is raises the exception.

Moreover, you have some declared types that do not match your JSON.

3 Comments

The problem is with the deserialisation, and yours matches what he's already tried.
The first try does not raise an exception, only the second does, though I updated the answer with the Root inside the list instead of the Customer.
thanks you all for your contribution, i have got it to work using the example above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.