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. 4 2371
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)
@chaarmann I going to split up my classes into Students and StudentListing. I will post the code soon!
@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: -
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;
-
-
}
-
-
-
}
- 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();
-
-
-
-
}
-
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
use: - 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: - System.out.println("data="+ data);
Your insert is also simple, just use - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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)
|
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
|
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?
|
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...
| |
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>
|
by: sudeerao |
last post by:
Please let me know how do we effectively and quickly test a php code ?
|
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!
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |