title: Testing a method which requires use of other methods of same class in Java
body: <p>Here is the part of the data structure:</p>
  <pre><code>public class SLList {
  
    public class IntNode {
        public int item;
        public IntNode next;
  
        public IntNode(int item, IntNode next){
            this.item = item;
            this.next = next;
        }
    }
  
    private IntNode first;
  
    public SLList(int x){
        this.first = new IntNode(x, null);
    }
  
    public void addFirst(int n){
        first = new IntNode(n, first);
    }
  
    public int getFirst(){
        return first.item;
    }
  }
  </code></pre>
  <p>Now, I need to test the two methods, addFirst() and getFirst(). I wrote the code as:</p>
  <pre><code>public class SLListTest {
  
  @Test
  public void addFirst() {
      SLList s = new SLList(10);
      s.addFirst(5);
      Assert.assertEquals(5, s.getFirst());
  }
  
  @Test
  public void getFirst() {
      SLList s = new SLList(10);
      s.addFirst(5);
      Assert.assertEquals(5, s.getFirst());
  }
  }
  </code></pre>
  <p>Now, I see a problem here, the call to other functions in the test. For example, test to addFirst() depends on getting getFirst() right and similar thing with getFirst(). How do I write the test to solve this problem? For example what if both of my methods are wrong but due to the dependency, test cases pass?</p>