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 8174521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:28:52+00:00 2026-06-06T22:28:52+00:00

I’m still not that confident when it comes to write unit tests for more

  • 0

I’m still not that confident when it comes to write unit tests for more complex methods/classes. My understanding of tdd and unit tests in general is, that you should be able to refactor the implementation of a class / method and have some confidence that you haven’t changed it’s behaviour

Let me give an example to make things more clear.

public class OrderService implements IOrderService
{
    private IItemService itemService;

    public Order create(OrderCreationDto dto)
    {
        Order order = new Order();
        order.addItems(createOrderLines(dto.products));
        return order;
    }

    private Set<OrderLine> createOrderItems(List<ProductDto> products)
    {
        Set<OrderLine> orderLines = new HashSet<>();
        for (ProductDto product : products) {
            Item item = itemService.create(product.id, product.price);
            OrderLine orderLine = new OrderLine();
            orderLine.setItemId(item.id);
            orderLines.add(orderLine);
        }
        return orderLines;
    }
}

Basically I’m just creating a new Order from a OrderCreationDto. For each order line I have to use the item service to create a new item first. One of my tests would make sure, that a call to itemService.create(...) is made for each order line with certain arguments.

Since each call to itemService.create(...) is quite expensive I’d like to refactor the class to create all items in one request .. let’s do the changes

public class OrderService implements IOrderService
{
    private Set<OrderLine> createOrderItems(List<ProductDto> products)
    {
        Set<OrderLine> orderLines = new HashSet<>();
        BatchItemCreationResponse response = itemService.createAll(products);
        for (ProductDto product : products) {
            Item item = response.get(product.id, product.price);
            ...
        }
        return orderLines;
    }
}

Now I’ve made the changes and my previously written test fails, even though the behaviour of the class has not changed at all.

Am I missing something? Is it something I have to accept the way it is and simply adjust the written test(s)?

Any thoughts are appreciated

  • 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-06T22:28:53+00:00Added an answer on June 6, 2026 at 10:28 pm

    Am I missing something? Is it something I have to accept the way it is and simply adjust the written test(s)?

    No. Yes.

    Unit tests do change. You’ve set your test basing on some class to utilize certain contract (ItemService). Changing this contract requires you to change the test, as simple as that.

    Think about it differently: from tested class client’s point of view, nothing changed and behavior is the same (that’s good). However, from ItemService client point of view, you’ve intoduced breaking change (contract change). Who’s the ItemService client? In this case, OrderService and its tests.

    Also, you should consider whether state verification (asserting state of OrderService products) won’t be better fit than behavior verification (verifying mock was called). Former one usually makes your tests less fragile to such changes, but neither will make the need of introducing changes to go away.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to create an if statement in PHP that prevents a single post

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.