Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While this kind of exercise is worthwhile from the perspective of learning the ins and outs of Java, <strong>I would advise caution before applying this to other problems</strong>. </p> <p>It's encouraged to use JUnit to exercise the public interface of your class under test. In the case of <code>ArrayStack</code>, let's assume it has a few methods:</p> <blockquote> <ul> <li><code>void push(Object o)</code></li> <li><code>Object pop()</code></li> <li><code>void clear()</code></li> </ul> </blockquote> <p>@dkaztzel suggested one solution - to implement a package private method to defensively copy the contents of your internal stack. Let's say it looks like this:</p> <pre><code>Object[] getStack() { return stack.clone(); } </code></pre> <p>Assuming you've implemented a few unit tests to verify that <code>push</code> and <code>pop</code> are working as intended (including the edge cases of empty and full stacks), then you would create a unit test as follows:</p> <pre><code>@Test public void probablyNotAGoodTest() { ArrayStack arrayStack = new ArrayStack(); arrayStack.push("Luke"); arrayStack.clear(); Object[] stackCopy = arrayStack.getStack(); assertNull(stackCopy[0]); } </code></pre> <p>This achieves your intent of verifying that in fact you've set the value at the index to null once an element has been popped. I'm always a little <em>suspicious</em> when I have to write production code whose sole purpose is to help me <strong>test my contract</strong>. Not to say that this approach is wrong, but that it requires careful consideration. Here the requirement is to ensure that popped objects are actually removed from the internal stack, so maybe this is okay. </p> <p>Another approach would be to enrich the <code>ArrayStack</code> interface, and provide a method to retrieve a particular object at a given index. This method could throw an exception if a null object is encountered, and then you could test for that. Here's what the method and a test could look like (please do some additional verification before you use this):</p> <pre><code>public Object getObjectAtIndex(int i) { if (i &gt;= maxSize) { throw new IllegalArgumentException(); } if (stack[i] == null) { throw new NullPointerException(); } return stack[i]; } </code></pre> <p>And then the test:</p> <pre><code>@Test(expected=NullPointerException.class) public void tryToGetAClearedObject() { ArrayStack arrayStack = new ArrayStack(); arrayStack.push("Luke"); arrayStack.clear(); arrayStack.getObjectAtIndex(0); } </code></pre> <p>Good luck!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload