0

I'm relatively new to Java programming. I was wondering what could I code to test this method? Could you please post any suggestions? I would like to test my method in a client class.

 public void insertThird(Player p)
  {
      PlayerNode pn = new PlayerNode(p);
      PlayerNode current = head;

      if (current == null)
          head = pn;
      else
      {
          current = current.getNext();
          if(current!=null)
          {
              pn.setNext(current.getNext());
              current.setNext(pn);
          }
          else
          {
              head.setNext(pn);
          }
      }
      numberOfItems++;
  }

1 Answers1

0

If you don't want to create a jUnit test ( a real test taht runs when you build to make sure the method ALWAYS works) and you just want to step through then this do something like this.... you'll have to clean up the instance variables that are not in your posted code

public class PlayerTester {

  public static void main(String[] args) {
    PlayerTester pt = new PlayerTester();
    pt.insertThird(new Player());
  }

  public void insertThird(Player p) {
    PlayerNode pn = new PlayerNode(p);
    PlayerNode current = head;

    if (current == null) {
      head = pn;
    }
    else {
      current = current.getNext();
      if (current != null) {
        pn.setNext(current.getNext());
        current.setNext(pn);
      }
      else {
        head.setNext(pn);
      }
    }
    numberOfItems++;
  }
}

then just debug the file which will run the main(...) method. BTW, main methods are how to run stuff in java.

Mark Giaconia
  • 3,844
  • 5
  • 20
  • 42