How do I test all of my methods given the code I have?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dseals22
    New Member
    • Jan 2017
    • 74

    How do I test all of my methods given the code I have?

    I have created a Java program that is designed to keep track of student's names, identification numbers, and GPA's, but I want to know how I can test each one of my methods given the code I have? Can someone help me? Thanks! Here is my code of what I have so far. Also, how will my instance variables come into play at when use my methods. Some of the methods that I have are the insert method, fetch method, delete, and update method.

    Code:
    public class StudentListings{
    
         private String name;   // key field 
    
         private int ID; 
    
         private double GPA; 
    
         private int next; 
    
         private int size;
    
         private StudentListing[] data; 
    
    
       public StudentListings(){
    
           this.name= name; 
           this.id= ID; 
           this.gpa=GPA; 
       } 
    
       public StudentListings(){ 
    
        next=0; 
        data= new StudentListings[Size]; 
        size= Size; 
    
       } // end of constructor 
    
       public boolean insert(StudentListings newStudentListing) { 
    
         if(next>=size) // the structure is full 
          return false; 
    
        // store a deep copy of the client's node
    
         data[next]= new StudentListing.deepCopy(); 
    
         if(data[next]== null) 
          return false; 
          next= next + 1; // prepare for the next insert 
           return true; 
    
        } // end of insert method 
    
       public StudentListings fetch(String targetKey){ 
    
        StudentListings studentListings; 
        StudentListings temp; 
    
        // access the node using a sequential search 
         int i=0; 
    
        while(i < next &&!(data[i].compareTo(targetKey)==0)
        {
            i++; 
        } 
    
        if(i== next) // node not found
          return null; 
    
         // deep copy the node's information into the client's node 
    
         studentListings= data[i].deepCopy(); 
    
         if(i!= 0) // bubble-up accessed node
         { 
            temp= data[i-1]; 
            data[i-1]=data[i];
            data[i]= temp;
         } 
            return studentListings; 
    
       } // end of fetch method 
    
    
       public boolean delete(String targetKey){ 
    
        int i=0; 
        while(i < next && !(data[i].compareTo(targetKey)==0))
         { 
            i++; 
         } 
         if(i==next) // node not found
    
          // move the last node into the deleted node's position
           return false; 
           data[i]= data[next-1]; 
           data[next-1]=null; 
           next= next-1; 
           return true; // node found and deleted
    
       }  // end of delete method 
    
       public boolean update(String targetKey, StudentListings newStudentListing){
    
        if(delete(targetKey)== false) // node not in the structure
          return false; 
        else if(insert(newStudentListing)==false) // insufficient memory 
          return false; 
        else 
           return true; // node found and updated 
    
       } // end of update method 
    
    
       public void showAll(){
    
        for(int i=0; i< next; i++)
           System.out.println(data[i].toString()); 
    
      } // end of showAll method 
    
    
      public static void main(String[] args){ 
    
        StudentListings obj1= new StudentListings(); 
    
       // how do I test each of this methods to see if they actually work?
    
        obj1.insert(); 
        obj1.fetch(); 
        obj1.delete(); 
        obj1.update(); 
        obj1.showAll(); 
    
     } 
    
    } // end of StudentListings class
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    You need to write JUnit-Tests for each public method of your class. Using Mockito will greatly help you.
    Install EclEmma (or any other code coverage tool) as plugin and when you run your JUnit-Tests it will tell you which code lines you have tested. So the answer to "how can I test all of my methods" is: keep writing JUnit-Tests until all is green (all is covered).

    By the way, your constructor is missing its 3 arguments, else the code inside does not make any sense.

    You only have public methods. Testing private methods would be a different problem (like using powerMock vs. rewriting code)

    Comment

    • dseals22
      New Member
      • Jan 2017
      • 74

      #3
      @chaarmann I going to split up my classes into Students and StudentListing. I will post the code soon!

      Comment

      • dseals22
        New Member
        • Jan 2017
        • 74

        #4
        @chaarmann I split up my code, but I confused about what I need to do in each of the four operations to make the StudentListings class work? Here are the classes below:

        Code:
        public class Student{
                   
             private String name;   // key field 
        
             private int ID; 
        
             private double GPA; 
        
        
           public Student(String name, int ID, double GPA){
             
               this.name= name; 
               this.ID= ID; 
               this.GPA=GPA; 
           } 
        
            public void setName(String name){ 
            
             this.name= name; 
        
           } 
        
           public String toString() { 
         
              return " Student Name: " + name + " Student ID: " + ID + "Student GPA: " + GPA; 
            }
          
          public String getName() { 
        
              return name; 
        
            }
        
           public void setID(int ID) { 
        
             this.ID= ID; 
         
            } 
        
             public int getID(){ 
        
              return ID; 
        
            } 
        
            public void setGPA(double GPA){ 
        
               this.GPA= GPA; 
        
            }
        
           public double getGPA(){ 
         
              return GPA; 
        
           }
        
          
        }
        Code:
        public class StudentListings{
        
        
        private Student[] data; // an array of Student objects
        private int next;
        private int size;
        
        
         StudentListings(){ 
        
          data= new Student[size];
          next=0; 
          size= s; 
        
        } 
        
        public boolean insert(Student newStudentListing) { 
        
        
        
        
        } 
        
         public StudentListings fetch(String targetKey){ 
        
        
        
        }
        
        public boolean delete(String targetKey){ 
        
        
        
        
        }
        
         public boolean update(String targetKey, Student newStudentListing){ 
        
        
        
        } 
        
        
           public void showAll(){ 
        
           for(int i=0; i< next; i++){ 
        
           System.out.println(data[i].toString());
          
          } 
        
        
        public static void main(String[] args){ 
        
              StudentListings obj1= new StudentListings();
              
              Student l1 = new Student("Terrence", 1, 3.45);
              Student l2 = new Student("Roberta", 2, 2.15);
              Student l3 = new Student("George", 3, 1.50);
              
               obj1.insert(l1);
               obj1.insert(l2);
               obj1.insert(l3);
        
               obj1.ShowAll();
        
               l3= obj1.fetch("Terrence");
        
               System.out.println(l3.toString());
        
               obj1.delete("Roberta");
        
               obj1.ShowAll();
        
               obj1.update();
        
               obj1.ShowAll(); 
              
        
        
        }

        Comment

        • chaarmann
          Recognized Expert Contributor
          • Nov 2007
          • 785

          #5
          If you use an array, you have to program much more than using an arrayList. You have to implement the search, the copying when increasing, the printing etc. all of your own, instead of just using a single command from the ArrayList.

          So instead
          Code:
          private Student[] data
          use:
          Code:
          private ArrayList data = new ArrayList();
          Then delete variables "next" and "size", you don't need them anymore.
          Also the printing in "showAll" does not need a loop anymore, you can print the whole list a single line:
          Code:
          System.out.println("data="+ data);
          Your insert is also simple, just use
          Code:
          data.add(newStudentListing)
          The others methods of your class: I leave it for you to look up the corresponding commands for deleting or finding a record inside the list. Just look up ArrayList-API the methods remove, indexOf and get.

          Comment

          Working...