Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 903963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:58:27+00:00 2026-05-15T15:58:27+00:00

I need to create a unit test for a method that returns an xmldocument.

  • 0

I need to create a unit test for a method that returns an xmldocument. What should I be validating in such a case? The method checks a database and creates an xmldocument and sends back the data to the client.

Should I look if the xmldocument returned has all expected xml tags? or should I have a Expected.xml file and match the xmldocument returned with this xml file. If I am going by the second approach, then whatever I am searching for is not present in database then this test would always fail. I want to write a test that is not dependent on any particular data but should check whether the data returned by the method is proper or not, so I am leaning towards the approach of checking only the tags and assuming that if tags are there and the values in these tags are also correct.

Lets assume i writing this test for a library application that gives a list of all books issued to a particular member. Members are identified by a id and let that be string:

<Member id="">
 <Book>
   <Name>Book_name</Name>
   <Author>author</Author>
   <Due_date>due date </Due_Data>
 </Book>
</Member>

So i need to verify whether my method would return an xmlfile like above. And i need to verify that the tags are correct and it contains value (and not null values).

What do you think should be better approach? If someone has dealt with such situations before, it would be great if you can share your experience.

Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:58:27+00:00Added an answer on May 15, 2026 at 3:58 pm

    The problem is not so much the XmlDocument – it’s easy to check if this matches the expected output.

    Your problem is more to do with the test input data. In this sort of case you either need to run the code against a specially set up test database so that you can rely on it always returning the same data for a given query, or you need to mock the database (call the database via your own class so that you can replace it with some code that simply pretends to be the database and returns the unit test inputs that you wish to try)

    Once you can provide well defined test inputs to the method, you can then verify that each test input produces the required test output – a unit test.

    If you wish to be less rigorous, then you can check that the database returns an XML document that is in the correct form (contains a key set of elements and attributes), but that still requires you have some valid input data to test with (so you may still need a test or mocked DB) and won’t be testing the method as thoroughly.

    edit

    To answer your edit, you can test if specific bits of an XmlDocument are “valid” pretty easily. e.g. Is the root element “Member”:

    Assert(doc.DocumentElement.Name == "Member");
    

    Does the root element have a child element “Name” that is non-blank, and has no children?

    Assert(doc.DocumentElement["Name"] != null);
    Assert(!string.IsNullOrEmpty(doc.DocumentElement["Name"].InnerText));
    Assert(doc.DocumentElement["Name"].ChildNodes.Count == 0);
    

    etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm beginner in testing.I need to create unit test for method with NetworkStream like
I need to create a method to generate a unit vector in three dimensions
I am writing an unit test for a mvc web application that checks if
i want to test a service method that parses an excel file so i
I need your help in solving one issue. I have such method in my
I have a method which uses StreamReader which i would like to unit test.
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get
I need create a document word with Java. And I ask, how can I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.