473,591 Members | 2,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

74 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. public class StudentListings{
  2.  
  3.      private String name;   // key field 
  4.  
  5.      private int ID; 
  6.  
  7.      private double GPA; 
  8.  
  9.      private int next; 
  10.  
  11.      private int size;
  12.  
  13.      private StudentListing[] data; 
  14.  
  15.  
  16.    public StudentListings(){
  17.  
  18.        this.name= name; 
  19.        this.id= ID; 
  20.        this.gpa=GPA; 
  21.    } 
  22.  
  23.    public StudentListings(){ 
  24.  
  25.     next=0; 
  26.     data= new StudentListings[Size]; 
  27.     size= Size; 
  28.  
  29.    } // end of constructor 
  30.  
  31.    public boolean insert(StudentListings newStudentListing) { 
  32.  
  33.      if(next>=size) // the structure is full 
  34.       return false; 
  35.  
  36.     // store a deep copy of the client's node
  37.  
  38.      data[next]= new StudentListing.deepCopy(); 
  39.  
  40.      if(data[next]== null) 
  41.       return false; 
  42.       next= next + 1; // prepare for the next insert 
  43.        return true; 
  44.  
  45.     } // end of insert method 
  46.  
  47.    public StudentListings fetch(String targetKey){ 
  48.  
  49.     StudentListings studentListings; 
  50.     StudentListings temp; 
  51.  
  52.     // access the node using a sequential search 
  53.      int i=0; 
  54.  
  55.     while(i < next &&!(data[i].compareTo(targetKey)==0)
  56.     {
  57.         i++; 
  58.     } 
  59.  
  60.     if(i== next) // node not found
  61.       return null; 
  62.  
  63.      // deep copy the node's information into the client's node 
  64.  
  65.      studentListings= data[i].deepCopy(); 
  66.  
  67.      if(i!= 0) // bubble-up accessed node
  68.      { 
  69.         temp= data[i-1]; 
  70.         data[i-1]=data[i];
  71.         data[i]= temp;
  72.      } 
  73.         return studentListings; 
  74.  
  75.    } // end of fetch method 
  76.  
  77.  
  78.    public boolean delete(String targetKey){ 
  79.  
  80.     int i=0; 
  81.     while(i < next && !(data[i].compareTo(targetKey)==0))
  82.      { 
  83.         i++; 
  84.      } 
  85.      if(i==next) // node not found
  86.  
  87.       // move the last node into the deleted node's position
  88.        return false; 
  89.        data[i]= data[next-1]; 
  90.        data[next-1]=null; 
  91.        next= next-1; 
  92.        return true; // node found and deleted
  93.  
  94.    }  // end of delete method 
  95.  
  96.    public boolean update(String targetKey, StudentListings newStudentListing){
  97.  
  98.     if(delete(targetKey)== false) // node not in the structure
  99.       return false; 
  100.     else if(insert(newStudentListing)==false) // insufficient memory 
  101.       return false; 
  102.     else 
  103.        return true; // node found and updated 
  104.  
  105.    } // end of update method 
  106.  
  107.  
  108.    public void showAll(){
  109.  
  110.     for(int i=0; i< next; i++)
  111.        System.out.println(data[i].toString()); 
  112.  
  113.   } // end of showAll method 
  114.  
  115.  
  116.   public static void main(String[] args){ 
  117.  
  118.     StudentListings obj1= new StudentListings(); 
  119.  
  120.    // how do I test each of this methods to see if they actually work?
  121.  
  122.     obj1.insert(); 
  123.     obj1.fetch(); 
  124.     obj1.delete(); 
  125.     obj1.update(); 
  126.     obj1.showAll(); 
  127.  
  128.  } 
  129.  
  130. } // end of StudentListings class 
Sep 22 '17 #1
4 2371
chaarmann
785 Recognized Expert Contributor
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)
Sep 22 '17 #2
dseals22
74 New Member
@chaarmann I going to split up my classes into Students and StudentListing. I will post the code soon!
Sep 22 '17 #3
dseals22
74 New Member
@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:

Expand|Select|Wrap|Line Numbers
  1. public class Student{
  2.  
  3.      private String name;   // key field 
  4.  
  5.      private int ID; 
  6.  
  7.      private double GPA; 
  8.  
  9.  
  10.    public Student(String name, int ID, double GPA){
  11.  
  12.        this.name= name; 
  13.        this.ID= ID; 
  14.        this.GPA=GPA; 
  15.    } 
  16.  
  17.     public void setName(String name){ 
  18.  
  19.      this.name= name; 
  20.  
  21.    } 
  22.  
  23.    public String toString() { 
  24.  
  25.       return " Student Name: " + name + " Student ID: " + ID + "Student GPA: " + GPA; 
  26.     }
  27.  
  28.   public String getName() { 
  29.  
  30.       return name; 
  31.  
  32.     }
  33.  
  34.    public void setID(int ID) { 
  35.  
  36.      this.ID= ID; 
  37.  
  38.     } 
  39.  
  40.      public int getID(){ 
  41.  
  42.       return ID; 
  43.  
  44.     } 
  45.  
  46.     public void setGPA(double GPA){ 
  47.  
  48.        this.GPA= GPA; 
  49.  
  50.     }
  51.  
  52.    public double getGPA(){ 
  53.  
  54.       return GPA; 
  55.  
  56.    }
  57.  
  58.  
  59. }
Expand|Select|Wrap|Line Numbers
  1. public class StudentListings{
  2.  
  3.  
  4. private Student[] data; // an array of Student objects
  5. private int next;
  6. private int size;
  7.  
  8.  
  9.  StudentListings(){ 
  10.  
  11.   data= new Student[size];
  12.   next=0; 
  13.   size= s; 
  14.  
  15.  
  16. public boolean insert(Student newStudentListing) { 
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  public StudentListings fetch(String targetKey){ 
  23.  
  24.  
  25.  
  26. }
  27.  
  28. public boolean delete(String targetKey){ 
  29.  
  30.  
  31.  
  32.  
  33. }
  34.  
  35.  public boolean update(String targetKey, Student newStudentListing){ 
  36.  
  37.  
  38.  
  39.  
  40.  
  41.    public void showAll(){ 
  42.  
  43.    for(int i=0; i< next; i++){ 
  44.  
  45.    System.out.println(data[i].toString());
  46.  
  47.   } 
  48.  
  49.  
  50. public static void main(String[] args){ 
  51.  
  52.       StudentListings obj1= new StudentListings();
  53.  
  54.       Student l1 = new Student("Terrence", 1, 3.45);
  55.       Student l2 = new Student("Roberta", 2, 2.15);
  56.       Student l3 = new Student("George", 3, 1.50);
  57.  
  58.        obj1.insert(l1);
  59.        obj1.insert(l2);
  60.        obj1.insert(l3);
  61.  
  62.        obj1.ShowAll();
  63.  
  64.        l3= obj1.fetch("Terrence");
  65.  
  66.        System.out.println(l3.toString());
  67.  
  68.        obj1.delete("Roberta");
  69.  
  70.        obj1.ShowAll();
  71.  
  72.        obj1.update();
  73.  
  74.        obj1.ShowAll(); 
  75.  
  76.  
  77.  
  78. }
  79.  
Sep 26 '17 #4
chaarmann
785 Recognized Expert Contributor
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
Expand|Select|Wrap|Line Numbers
  1. private Student[] data
use:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. System.out.println("data="+ data);
Your insert is also simple, just use
Expand|Select|Wrap|Line Numbers
  1. 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.
Sep 27 '17 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
1974
by: Jan Decaluwe | last post by:
I'm working on a unit test for a finite state machine (FSM). The FSM behavior is specified in a dictionary called transitionTable. It has a key per state with a tuple of possible transitions as the corresponding value. A transition is defined as a number of input values, a next state, and a documentation string. I want to test each possible transition in a separate test method. For a particular transition, a test method could look as...
1
1352
by: TEN TOD PSA | last post by:
you can trick ASP.NET into letting you declare methods with code blocks (how anybody can survive without this i have no idea - in classic asp i haven't written any html outside of a sub/method in 8 years - ASP.NET seems to be going backwards in some respects) (seems to me using Classic ASP but accessing the juicy .NET framework features via COM Interop is a better choice than actually using ASP.NET) in your aspx page do this: (start)
1
2180
by: Robert V. Hanson | last post by:
Please give me some ideas on how to setup the ability to test System.Web.Mail code to send emails using just my development computer, Win2000Pro OS and IIS5.0? Thanks, Bob Hanson
3
1458
by: mtczx232 | last post by:
in previous versions we can put Override header method into code by choosing Override from upper left dropdown list, and choosing the method from right one. but in VS2005 Override Option disappear?
14
1561
by: ToddLMorgan | last post by:
Summary: How should multiple (related) projects be arranged (structured) and configured so that the following is possible: o Sharing common code (one of the projects would be a "common" project referenced by all others and likely the others would share at least the common project and possibly more as times goes on) o Clear separation of "production" code and "test" code (ie to readily ship source and test as separate components. Usually...
22
1912
by: santosh | last post by:
I've written the following function to return a string of arbitrary length from stdin. So far, it seems to work as it should. Are there any unportable assumptions and/or logical errors in the code? Would it be better to return the length of the string? Or an integer value indicating success or failure, (the type of failure too)? Thanks. #include <stdio.h> #include <stdlib.h>
5
2139
by: sudeerao | last post by:
Please let me know how do we effectively and quickly test a php code ?
5
6509
by: shuisheng | last post by:
Dear All, I was told that unit test is a powerful tool for progamming. If I am writing a GUI code, is it possible to still using unit test? I have a little experience in using unittest++. But I can not work out a way to use it to test GUI code. Thanks a lot!
0
7934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8362
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5732
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1465
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1199
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.