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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:54:43+00:00 2026-05-16T15:54:43+00:00

Let’s assume we are designing a Stack class test-first (TDD): public class Stack<T> {

  • 0

Let’s assume we are designing a Stack class test-first (TDD):

public class Stack<T> {
    private T[] elements = new T[16];
    private int size = 0;
    ...
}

This Stack makes use of a size 16 internal array to store its elements. It will work fine until you need to add a 17th element. As I might need a 17th element, I decided to add that functionality to my Stack, so I started thinking about what name I could give to the test that’d make have to add that functionality. That will be the subject of my first question.

I first chose something of the form of:

Should_Be_Able_To_Correctly_Increase_Its_Inner_Array_Size()

and then

Should_Handle_More_Items_Than_The_Default_Internal_Array_Size()

but after thinking for a bit I came to the conclusion that maybe something like the following would be more apropriate:

Should_Double_Its_Size_Every_Time_Its_Full()

My reasoning would have to do that in the first case, I am saying only what it does, but not when.
In the second, I am saying when to add more items, but I am also stating how I’m thinking of achieving it (internally), which may not be correct. In my view (I’m not sure I am correct), my tests should the possible interactions of my SUT with the exterior, and not on how it is implemented internally. Am I right?

It looks to me that the 3rd options is the best, as it clearly states what it does(grow in size — in fact, double its size), when it does(when it’s full) and doesn’t ties me to any specific implementation (I could maybe later want to change it to an internal ArrayList!).

Which leads me to my second question: Assuming I did all the Unit-Tests for my Stack class that uses internally an Array and it works fine and as expected, should my tests remain intact if I later want to refactor and change the Array to an ArrayList or any other kind of data-structure? Or should the tests in any way reflect that? I’d guess no, but I’m not sure.

  • 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-16T15:54:43+00:00Added an answer on May 16, 2026 at 3:54 pm

    In my view (I’m not sure I am
    correct), my tests should the possible
    interactions of my SUT with the
    exterior, and not on how it is
    implemented internally. Am I right?

    You are correct. If you change the internal implementation of your class, the unit tests should remain the same. If you expose anything new externally, you should create new unit tests to account for these changes.

    Remember that when you’re designing your class you don’t want to expose anything that indicates how it was implemented. The class’ public members indicate how to interact with it, now how it works behind the scenes.

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

Sidebar

Related Questions

Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)
Let's say I have the following classes : public class MyProductCode { private String
Let's assume we have the following class : public class ImageButton extends MovieClip {
Let's imagine I have a Java class of the type: public class MyClass {
Let's say we have the following: abstract class A; class B : public A;
Let me explain best with an example. Say you have node class that can
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's assume that we are building a high traffic site that will be used
Let's say I have an facebook application running using the JS SDK. First user
Let's say, if I need to assign an id to element with first name,

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.