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

  • Home
  • SEARCH
  • 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 8539297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:19:52+00:00 2026-06-11T11:19:52+00:00

When writing unit tests I need some objects with sample data. For example suppose

  • 0

When writing unit tests I need some objects with sample data. For example suppose I have an Order object. One needs to write code like this –

Order o = new Order();
o.setId(3);
o.setAmount(2830.9);

List<Item> items = new ArrayList<Item>();
Item i = new Item();
i.setId(3);
i.setCost(34);
items.add(i);

o.setItems(items);

It is a lot more frustrating and redundant than it looks here because a real object is likely to have lot more attributes and nested objects.

And if one needs multiple orders …

What is the best way to create mock data objects for testing?

Off the top of my head I’m thinking about deserializing my objects from Json. Is there a standard, efficient way to do this?

  • 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-06-11T11:19:53+00:00Added an answer on June 11, 2026 at 11:19 am

    Generally DTO contain only fields and no logic which needs to be mocked out.

    I would use a DTO as a mock of itself. If the DTO has logic in it you might like to mock out, I would move the logic out of the DTO.

    To create DTO, I would do this from text, either in the test itself, or from an external file. You could use JSon, but if you don’t use that already I would use XMLEncoder/XMLDecoder. Its not pretty XML but it is built in so you don’t need an extra library.

    If you can, you might be able to create DTOs from the logs of the application, so you can recreate a realistic scenario.

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

Sidebar

Related Questions

I am writing some unit tests and I need to be able to access
I'm writing some unit tests and I need to be able to Assert whether
I am currently writing the unit tests for a php data access object. Obviously
I am writing some unit tests in which I need a fake xml file.
I have been writing unit tests using NUnit and Moq with my Silverlight code
I’ve been writing unit tests in strongly typed languages and I have a fair
I'm writing unit tests for a module in a small Javascript application. In order
I'm writing unit tests using RhinoMocks for mocking, and now I'm in need of
I'm writing some unit tests for my View Model class. The constructor of this
I am writing some unit tests in VS 2010 using Selenium and C#. 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.