SlideShare a Scribd company logo
1 of 9
Download to read offline
Goal: The goal of this assignment is to help students understand the use of JUnit to test Java
code.
Description: In this assignment you will create a set of unit tests, to test the behavior of the code
written for Assignment 1. To keep things consistent, please use the solution to assignment 1
provided by the instructor. You can find the solution on TRACS, under Resources >
Assignment-related material > Assignment
Solution
s.
A unit test is an automated piece of code that invokes a unit of work in the system and then
checks a single assumption about the behavior of that unit of work. A unit of work is a single
logical functional use case in the system that can be invoked by some public interface (in most
cases). A unit of work can span a single method, a whole class or multiple classes working
together to achieve one single logical purpose that can be verified.
Think of unit testing as a way to test the behavior of the code (or parts of the code) written,
without actually having to run the program. For example, in the case of assignments 1, assume
that the front-end (console user interface) part of the program and the back-end part of the
program (dealership database management) are written by two different developers. How would
the developer of the back-end be able to ensure that the code he/she has written does what it is
supposed to do without having access to the front-end?
A good unit test is:
• Able to be fully automated
• Has full control over all the pieces running.
• Can be run in any order, if part of many other tests
• Runs in memory (no DB or File access, for example)
• Consistently returns the same result (You always run the same test, so no random numbers, for
example.)
• Runs fast
• Tests a single logical concept in the system
• Readable
• Maintainable
• Trustworthy (when you see its result, you don’t need to debug the code just to be sure)
In this assignment, you are asked to create JUnit tests to test the classes and methods written for
assignment 1. First you should consider testing the behavior of theses classes/methods under
normal operation scenarios. For example, to test the method findCar(String vin) of the class
Dealership, you may need to create a test method, which creates a mock Car object and adds it to
the carList before the method findCar can be called to search for it. To ensure that everything
worked as planned, you can then search for the car using its VIN and see if the correct car is
found. Mock objects can be created either in the same test method or before any test methods are
run, using the @BeforeClass annotation.
Subsequently, you can consider creating test cases for unusual scenarios, e.g. when a certain
input or behavior is expected to cause an exception to be thrown, or when a user input is not as
expected.
At the end create a TestRunner class, which has a main method that runs the unit tests that you
have created, and prints out the test results.
Tasks:
1. Implement the JUnit tests to test only the class Dealership.java, including all its methods. Try
to be creative by coming up with test cases that can test as many different situations as possible.
You don’t need to test the classes Car.java and MainClass.java.
2. Use a standard Java coding style to improve your program’s visual appearance and make it
more readable. I suggest the BlueJ coding style: http://www.bluej.org/objects-
first/styleguide.html
3. Use Javadoc to document your code.
Dealership Program
import java.io.IOException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
/**
* This class is used to represent a database interface for a list of
* Car's. It using a plain-text file "cars.txt" to store and write
* car objects in readable text form. It contains an ArrayList
* called carList to store the database in a runtime friendly data
* structure. The carList is written to "cars.txt" at the end of
* the Dealership object's life by calling flush().
* This class also provides methods for adding, remove, and searching for cars
* from the list.
*
* @author Vangelis Metsis
*/
public class Dealership {
private ArrayList carList;
/**
* This constructor is hard-coded to open "cars.txt" and
* initialize the carList with its contents. If no such file
* exists, then one is created. The contents of the file are "loaded" into
* the carList ArrayList in no particular order. The file is then closed
* during the duration of the program until flush() is called.
* @throws IOException
*/
public Dealership() throws IOException {
carList = new ArrayList<>();
Scanner carDatabase;
File dataFile = new File("cars.txt");
// If data file does not exist, create it.
if (!dataFile.exists()) {
System.err.println("Cars.txt does not exist, creating one now . . .");
//if the file doesn't exists, create it
PrintWriter pw = new PrintWriter("cars.txt");
//close newly created file so we can reopen it
pw.close();
}
carDatabase = new Scanner(new FileReader(dataFile));
//Initialize the Array List with cars from cars.txt
while (carDatabase.hasNextLine()) {
// split values using the space character as separator
String[] temp = carDatabase.nextLine().split(" ");
carList.add(new Car(temp[0], temp[1], temp[2], Integer.parseInt(temp[3]),
Integer.parseInt(temp[4]), Float.parseFloat(temp[5])));
}
//Cars list is now in the Array List Completely so we can close the file
carDatabase.close();
}
/**
* Method showCars displays the current list of cars in the Arraylist in no
* particular order.
*
*/
public void showCars() {
showCars(carList);
}
/**
* Private method used as an auxiliary method to display a given ArrayList
* of cars in a formatted manner.
*
* @param cars the car list to be displayed.
*/
private void showCars(ArrayList cars) {
System.out.println("
_____________________________________________________________________________
_____");
System.out.println("| Entry | VIN | Make | Model | Year | Mileage | Price |");
System.out.println("|----------------------------------------------------------------------------------|");
for (int i = 0; i < cars.size(); i++) {
System.out.println(String.format("| %-6s| %-7s| %-15s| %-15s| %-5s| %-8s| $%-12s|",
Integer.toString(i + 1), cars.get(i).getVin(), cars.get(i).getMake(), cars.get(i).getModel(),
Integer.toString(cars.get(i).getYear()), Integer.toString(cars.get(i).getMileage()),
String.format("%.2f", cars.get(i).getPrice())));
}
System.out.println("|____________________________________________________________
______________________| ");
}
/**
* This method displays cars that have a price within the range of
* low to high.
*
* @param low a float that is the lower bound price.
* @param high a float that is the upper bound price.
*/
public void showCarsRange(float low, float high) {
ArrayList cars = new ArrayList<>();
for (Car car : carList) {
if ((low <= car.getPrice()) && (car.getPrice() <= high)) {
cars.add(car);
}
}
showCars(cars);
}
/**
* This method can be used to search for a car in the Arraylist of cars.
*
* @param vin a String that represents the vehicle
* identification number of the car that to be searched for
* @return the int index of the car in the Arraylist of cars,
* or -1 if the search failed.
*/
public int findCar(String vin) {
int index = -1;
for (int i = 0; i < carList.size(); i++) {
String temp = carList.get(i).getVin();
if (vin.equalsIgnoreCase(temp)) {
index = i;
break;
}
}
return index;
}
/**
* This method is used to add a car to the carList ArrayList. In order for a
* car to be added to the ArrayList it must comply with the following:
*
* 1. The car is not already in the ArrayList according to the VIN
* as the unique key.
*
* 2. The VIN string matches the following regular expression:
* "[A-Za-z0-9]{5}" or in other words: it
* is 5 avinhanumeric characters.
*
* 3. The make of the car is only alphanumeric characters.
*
* 4. The model of the car is only alphanumeric or contains a dash "-".
*
* 5. The year model must be exactly four digits.
*
* 6. The price must be non-negative.
*
* @param toAdd the Car object to add to the
* carList
*/
public void addCar(String vin, String make, String model, String year, String mileage, String
price) {
if (this.findCar(vin) != -1) {
System.err.println("Car already exists in database.  ");
return;
}
if (!vin.matches("[A-Za-z0-9]{5}")) {
System.err.println("Invalid VIN: not proper format."
+ "VIN must be at least 5 alphanumeric characters.");
return;
}
if (!make.matches("[A-Za-z0-9]+")) {
System.err.println("Invalid make type: "
+ "The car's make must be an alphanumeric string.");
return;
}
if (!model.matches("[A-Z0-9a-z-]+")) {
System.err.println("Invalid model type: "
+ "The car's model must be a non-numeric alphabet string.");
return;
}
if (!year.matches("[0-9]{4}") ) {
System.err.println("Invalid year: "
+ "The car's year of manufacture must be a 4 digit number. ");
return;
}
if (!mileage.matches("[0-9]{1,6}")) {
System.err.println("Invalid milage: "
+ "The car's mileage has to be an integer number between 0 and 999999. ");
return;
}
if (Float.parseFloat(price) < 0) {
System.err.println("The entered price cannot be negative.");
return;
}
//If passed all the checks, add the car to the list
carList.add(new Car(vin, make, model, Integer.parseInt(year),
Integer.parseInt(mileage), Float.parseFloat(price)));
System.out.println("Car has been added. ");
}
/**
* This method will remove a car from the carList ArrayList. It
* will remove the first instance of a car that matches the car that was
* passed to this method in the carList using the
* equals() override in the class Car. If no such
* car exists, it will produce an error message.
*
* @param toDelete the car object to be removed.
*/
public void removeCar(Car toDelete) {
if (carList.remove(toDelete)) {
System.out.println("Car was removed. ");
} else {
System.err.println("Car does not exist in database.");
}
}
/**
* This method is used to retrieve the Car object from the
* carList at a given index.
*
* @param i the index of the desired car object.
* @return the car object at the index or null if the index is
* invalid.
*/
public Car getCar(int i) {
if (i < carList.size() && i >= 0) {
return carList.get(i);
} else {
System.err.println("Invalid Index. Please enter another command or 'h' to list the
commands.");
return null;
}
}
/**
* This should be the last method to be called from this class. It opens
* "cars.txt" and overwrites it with a text representation of
* all the cars in the carList.
* @throws IOException
*/
public void flush() throws IOException {
PrintWriter pw = new PrintWriter("cars.txt");
for (Car c : carList) {
pw.print(c.toString());
}
pw.close();
}
}

More Related Content

Similar to Goal The goal of this assignment is to help students understand the.pdf

#include iostream#include string#include iomanip#inclu.docx
#include iostream#include string#include iomanip#inclu.docx#include iostream#include string#include iomanip#inclu.docx
#include iostream#include string#include iomanip#inclu.docxmayank272369
 
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docxCOIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docxclarebernice
 
Code igniter unittest-part1
Code igniter unittest-part1Code igniter unittest-part1
Code igniter unittest-part1Albert Rosa
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosFlutter Agency
 
Public class race track {public static void main(string[] args
Public class race track {public static void main(string[] argsPublic class race track {public static void main(string[] args
Public class race track {public static void main(string[] argsYASHU40
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming ConceptsBhushan Nagaraj
 
Selenium web driver | java
Selenium web driver | javaSelenium web driver | java
Selenium web driver | javaRajesh Kumar
 
Java Sorting CodeImplementing and testing all three sort algorithm.pdf
Java Sorting CodeImplementing and testing all three sort algorithm.pdfJava Sorting CodeImplementing and testing all three sort algorithm.pdf
Java Sorting CodeImplementing and testing all three sort algorithm.pdfforecastfashions
 
Testing Options in Java
Testing Options in JavaTesting Options in Java
Testing Options in JavaMichael Fons
 
Appium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation ExecutionAppium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation ExecutionpCloudy
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Vu Tran Lam
 
Javascript coding-and-design-patterns
Javascript coding-and-design-patternsJavascript coding-and-design-patterns
Javascript coding-and-design-patternsHernan Mammana
 
#include iostream #include string #include fstream std.docx
#include iostream #include string #include fstream  std.docx#include iostream #include string #include fstream  std.docx
#include iostream #include string #include fstream std.docxajoy21
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingVisual Engineering
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java AppletsTareq Hasan
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)David McCarter
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate JavaPhilip Johnson
 

Similar to Goal The goal of this assignment is to help students understand the.pdf (20)

#include iostream#include string#include iomanip#inclu.docx
#include iostream#include string#include iomanip#inclu.docx#include iostream#include string#include iomanip#inclu.docx
#include iostream#include string#include iomanip#inclu.docx
 
Clean tests good tests
Clean tests   good testsClean tests   good tests
Clean tests good tests
 
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docxCOIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
COIT20245, 2016 Term One - Page 1 of 9 Assessment detail.docx
 
Code igniter unittest-part1
Code igniter unittest-part1Code igniter unittest-part1
Code igniter unittest-part1
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
 
Public class race track {public static void main(string[] args
Public class race track {public static void main(string[] argsPublic class race track {public static void main(string[] args
Public class race track {public static void main(string[] args
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Selenium web driver | java
Selenium web driver | javaSelenium web driver | java
Selenium web driver | java
 
Java Sorting CodeImplementing and testing all three sort algorithm.pdf
Java Sorting CodeImplementing and testing all three sort algorithm.pdfJava Sorting CodeImplementing and testing all three sort algorithm.pdf
Java Sorting CodeImplementing and testing all three sort algorithm.pdf
 
L9
L9L9
L9
 
Testing Options in Java
Testing Options in JavaTesting Options in Java
Testing Options in Java
 
Appium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation ExecutionAppium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation Execution
 
Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)Session 3 - Object oriented programming with Objective-C (part 1)
Session 3 - Object oriented programming with Objective-C (part 1)
 
Javascript coding-and-design-patterns
Javascript coding-and-design-patternsJavascript coding-and-design-patterns
Javascript coding-and-design-patterns
 
#include iostream #include string #include fstream std.docx
#include iostream #include string #include fstream  std.docx#include iostream #include string #include fstream  std.docx
#include iostream #include string #include fstream std.docx
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 
Automation tips
Automation tipsAutomation tips
Automation tips
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
Introduction to Intermediate Java
Introduction to Intermediate JavaIntroduction to Intermediate Java
Introduction to Intermediate Java
 

More from arsmobiles

For each NADH that supplies electrons to the electron transport syst.pdf
For each NADH that supplies electrons to the electron transport syst.pdfFor each NADH that supplies electrons to the electron transport syst.pdf
For each NADH that supplies electrons to the electron transport syst.pdfarsmobiles
 
Biology II . Please answer all of these questions only if you are su.pdf
Biology II . Please answer all of these questions only if you are su.pdfBiology II . Please answer all of these questions only if you are su.pdf
Biology II . Please answer all of these questions only if you are su.pdfarsmobiles
 
During spermatogenesis in the seminiferous tubules, the position of .pdf
During spermatogenesis in the seminiferous tubules, the position of .pdfDuring spermatogenesis in the seminiferous tubules, the position of .pdf
During spermatogenesis in the seminiferous tubules, the position of .pdfarsmobiles
 
A model of a system is best characterized by the following statement.pdf
A model of a system is best characterized by the following statement.pdfA model of a system is best characterized by the following statement.pdf
A model of a system is best characterized by the following statement.pdfarsmobiles
 
4- what is the link between asset diversification and the liquidit.pdf
4- what is the link between asset diversification and the liquidit.pdf4- what is the link between asset diversification and the liquidit.pdf
4- what is the link between asset diversification and the liquidit.pdfarsmobiles
 
A book by Nozick. The experience machine. “what does matter to us in.pdf
A book by Nozick. The experience machine. “what does matter to us in.pdfA book by Nozick. The experience machine. “what does matter to us in.pdf
A book by Nozick. The experience machine. “what does matter to us in.pdfarsmobiles
 
any one has knowladge in C unix opreating system .. please help Desc.pdf
any one has knowladge in C unix opreating system .. please help Desc.pdfany one has knowladge in C unix opreating system .. please help Desc.pdf
any one has knowladge in C unix opreating system .. please help Desc.pdfarsmobiles
 
A common symptom of pharyngitis is swelling of the. interior cervical.pdf
A common symptom of pharyngitis is swelling of the. interior cervical.pdfA common symptom of pharyngitis is swelling of the. interior cervical.pdf
A common symptom of pharyngitis is swelling of the. interior cervical.pdfarsmobiles
 
Define nonconcordant variation Why does nonconcordant variation mak.pdf
Define nonconcordant variation Why does nonconcordant variation mak.pdfDefine nonconcordant variation Why does nonconcordant variation mak.pdf
Define nonconcordant variation Why does nonconcordant variation mak.pdfarsmobiles
 
Describe the operation of a tubular bowl centrifugeSolutionTub.pdf
Describe the operation of a tubular bowl centrifugeSolutionTub.pdfDescribe the operation of a tubular bowl centrifugeSolutionTub.pdf
Describe the operation of a tubular bowl centrifugeSolutionTub.pdfarsmobiles
 
Could you help me with this question Thanks so much!Let T be an e.pdf
Could you help me with this question Thanks so much!Let T be an e.pdfCould you help me with this question Thanks so much!Let T be an e.pdf
Could you help me with this question Thanks so much!Let T be an e.pdfarsmobiles
 
Briefly discuss Philippa Foot’s contributions to virtue theory.S.pdf
Briefly discuss Philippa Foot’s contributions to virtue theory.S.pdfBriefly discuss Philippa Foot’s contributions to virtue theory.S.pdf
Briefly discuss Philippa Foot’s contributions to virtue theory.S.pdfarsmobiles
 
List for a company you either work or are familiar with, the interna.pdf
List for a company you either work or are familiar with, the interna.pdfList for a company you either work or are familiar with, the interna.pdf
List for a company you either work or are familiar with, the interna.pdfarsmobiles
 
Write a C program that uses a recursive function to print a triangle .pdf
Write a C program that uses a recursive function to print a triangle .pdfWrite a C program that uses a recursive function to print a triangle .pdf
Write a C program that uses a recursive function to print a triangle .pdfarsmobiles
 
Which avenue of research is least likely to reveal whether asexual or.pdf
Which avenue of research is least likely to reveal whether asexual or.pdfWhich avenue of research is least likely to reveal whether asexual or.pdf
Which avenue of research is least likely to reveal whether asexual or.pdfarsmobiles
 
When testing the difference between two population means, the varian.pdf
When testing the difference between two population means, the varian.pdfWhen testing the difference between two population means, the varian.pdf
When testing the difference between two population means, the varian.pdfarsmobiles
 
IT Project Management1. Please compare and contrast a traditional .pdf
IT Project Management1. Please compare and contrast a traditional .pdfIT Project Management1. Please compare and contrast a traditional .pdf
IT Project Management1. Please compare and contrast a traditional .pdfarsmobiles
 
Whats the relationship between DNA and RNA Whats the relat.pdf
Whats the relationship between DNA and RNA Whats the relat.pdfWhats the relationship between DNA and RNA Whats the relat.pdf
Whats the relationship between DNA and RNA Whats the relat.pdfarsmobiles
 
What does it mean to destroy something at the molecular level in ter.pdf
What does it mean to destroy something at the molecular level in ter.pdfWhat does it mean to destroy something at the molecular level in ter.pdf
What does it mean to destroy something at the molecular level in ter.pdfarsmobiles
 
What is a rain shadow, and why does it happenSolutionThe warm.pdf
What is a rain shadow, and why does it happenSolutionThe warm.pdfWhat is a rain shadow, and why does it happenSolutionThe warm.pdf
What is a rain shadow, and why does it happenSolutionThe warm.pdfarsmobiles
 

More from arsmobiles (20)

For each NADH that supplies electrons to the electron transport syst.pdf
For each NADH that supplies electrons to the electron transport syst.pdfFor each NADH that supplies electrons to the electron transport syst.pdf
For each NADH that supplies electrons to the electron transport syst.pdf
 
Biology II . Please answer all of these questions only if you are su.pdf
Biology II . Please answer all of these questions only if you are su.pdfBiology II . Please answer all of these questions only if you are su.pdf
Biology II . Please answer all of these questions only if you are su.pdf
 
During spermatogenesis in the seminiferous tubules, the position of .pdf
During spermatogenesis in the seminiferous tubules, the position of .pdfDuring spermatogenesis in the seminiferous tubules, the position of .pdf
During spermatogenesis in the seminiferous tubules, the position of .pdf
 
A model of a system is best characterized by the following statement.pdf
A model of a system is best characterized by the following statement.pdfA model of a system is best characterized by the following statement.pdf
A model of a system is best characterized by the following statement.pdf
 
4- what is the link between asset diversification and the liquidit.pdf
4- what is the link between asset diversification and the liquidit.pdf4- what is the link between asset diversification and the liquidit.pdf
4- what is the link between asset diversification and the liquidit.pdf
 
A book by Nozick. The experience machine. “what does matter to us in.pdf
A book by Nozick. The experience machine. “what does matter to us in.pdfA book by Nozick. The experience machine. “what does matter to us in.pdf
A book by Nozick. The experience machine. “what does matter to us in.pdf
 
any one has knowladge in C unix opreating system .. please help Desc.pdf
any one has knowladge in C unix opreating system .. please help Desc.pdfany one has knowladge in C unix opreating system .. please help Desc.pdf
any one has knowladge in C unix opreating system .. please help Desc.pdf
 
A common symptom of pharyngitis is swelling of the. interior cervical.pdf
A common symptom of pharyngitis is swelling of the. interior cervical.pdfA common symptom of pharyngitis is swelling of the. interior cervical.pdf
A common symptom of pharyngitis is swelling of the. interior cervical.pdf
 
Define nonconcordant variation Why does nonconcordant variation mak.pdf
Define nonconcordant variation Why does nonconcordant variation mak.pdfDefine nonconcordant variation Why does nonconcordant variation mak.pdf
Define nonconcordant variation Why does nonconcordant variation mak.pdf
 
Describe the operation of a tubular bowl centrifugeSolutionTub.pdf
Describe the operation of a tubular bowl centrifugeSolutionTub.pdfDescribe the operation of a tubular bowl centrifugeSolutionTub.pdf
Describe the operation of a tubular bowl centrifugeSolutionTub.pdf
 
Could you help me with this question Thanks so much!Let T be an e.pdf
Could you help me with this question Thanks so much!Let T be an e.pdfCould you help me with this question Thanks so much!Let T be an e.pdf
Could you help me with this question Thanks so much!Let T be an e.pdf
 
Briefly discuss Philippa Foot’s contributions to virtue theory.S.pdf
Briefly discuss Philippa Foot’s contributions to virtue theory.S.pdfBriefly discuss Philippa Foot’s contributions to virtue theory.S.pdf
Briefly discuss Philippa Foot’s contributions to virtue theory.S.pdf
 
List for a company you either work or are familiar with, the interna.pdf
List for a company you either work or are familiar with, the interna.pdfList for a company you either work or are familiar with, the interna.pdf
List for a company you either work or are familiar with, the interna.pdf
 
Write a C program that uses a recursive function to print a triangle .pdf
Write a C program that uses a recursive function to print a triangle .pdfWrite a C program that uses a recursive function to print a triangle .pdf
Write a C program that uses a recursive function to print a triangle .pdf
 
Which avenue of research is least likely to reveal whether asexual or.pdf
Which avenue of research is least likely to reveal whether asexual or.pdfWhich avenue of research is least likely to reveal whether asexual or.pdf
Which avenue of research is least likely to reveal whether asexual or.pdf
 
When testing the difference between two population means, the varian.pdf
When testing the difference between two population means, the varian.pdfWhen testing the difference between two population means, the varian.pdf
When testing the difference between two population means, the varian.pdf
 
IT Project Management1. Please compare and contrast a traditional .pdf
IT Project Management1. Please compare and contrast a traditional .pdfIT Project Management1. Please compare and contrast a traditional .pdf
IT Project Management1. Please compare and contrast a traditional .pdf
 
Whats the relationship between DNA and RNA Whats the relat.pdf
Whats the relationship between DNA and RNA Whats the relat.pdfWhats the relationship between DNA and RNA Whats the relat.pdf
Whats the relationship between DNA and RNA Whats the relat.pdf
 
What does it mean to destroy something at the molecular level in ter.pdf
What does it mean to destroy something at the molecular level in ter.pdfWhat does it mean to destroy something at the molecular level in ter.pdf
What does it mean to destroy something at the molecular level in ter.pdf
 
What is a rain shadow, and why does it happenSolutionThe warm.pdf
What is a rain shadow, and why does it happenSolutionThe warm.pdfWhat is a rain shadow, and why does it happenSolutionThe warm.pdf
What is a rain shadow, and why does it happenSolutionThe warm.pdf
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 

Goal The goal of this assignment is to help students understand the.pdf

  • 1. Goal: The goal of this assignment is to help students understand the use of JUnit to test Java code. Description: In this assignment you will create a set of unit tests, to test the behavior of the code written for Assignment 1. To keep things consistent, please use the solution to assignment 1 provided by the instructor. You can find the solution on TRACS, under Resources > Assignment-related material > Assignment Solution s. A unit test is an automated piece of code that invokes a unit of work in the system and then checks a single assumption about the behavior of that unit of work. A unit of work is a single logical functional use case in the system that can be invoked by some public interface (in most cases). A unit of work can span a single method, a whole class or multiple classes working together to achieve one single logical purpose that can be verified. Think of unit testing as a way to test the behavior of the code (or parts of the code) written, without actually having to run the program. For example, in the case of assignments 1, assume that the front-end (console user interface) part of the program and the back-end part of the program (dealership database management) are written by two different developers. How would the developer of the back-end be able to ensure that the code he/she has written does what it is supposed to do without having access to the front-end? A good unit test is: • Able to be fully automated • Has full control over all the pieces running. • Can be run in any order, if part of many other tests • Runs in memory (no DB or File access, for example) • Consistently returns the same result (You always run the same test, so no random numbers, for example.) • Runs fast • Tests a single logical concept in the system • Readable • Maintainable • Trustworthy (when you see its result, you don’t need to debug the code just to be sure) In this assignment, you are asked to create JUnit tests to test the classes and methods written for assignment 1. First you should consider testing the behavior of theses classes/methods under normal operation scenarios. For example, to test the method findCar(String vin) of the class Dealership, you may need to create a test method, which creates a mock Car object and adds it to
  • 2. the carList before the method findCar can be called to search for it. To ensure that everything worked as planned, you can then search for the car using its VIN and see if the correct car is found. Mock objects can be created either in the same test method or before any test methods are run, using the @BeforeClass annotation. Subsequently, you can consider creating test cases for unusual scenarios, e.g. when a certain input or behavior is expected to cause an exception to be thrown, or when a user input is not as expected. At the end create a TestRunner class, which has a main method that runs the unit tests that you have created, and prints out the test results. Tasks: 1. Implement the JUnit tests to test only the class Dealership.java, including all its methods. Try to be creative by coming up with test cases that can test as many different situations as possible. You don’t need to test the classes Car.java and MainClass.java. 2. Use a standard Java coding style to improve your program’s visual appearance and make it more readable. I suggest the BlueJ coding style: http://www.bluej.org/objects- first/styleguide.html 3. Use Javadoc to document your code. Dealership Program import java.io.IOException; import java.io.FileReader; import java.io.PrintWriter; import java.io.File; import java.util.ArrayList; import java.util.Scanner; /** * This class is used to represent a database interface for a list of * Car's. It using a plain-text file "cars.txt" to store and write * car objects in readable text form. It contains an ArrayList * called carList to store the database in a runtime friendly data * structure. The carList is written to "cars.txt" at the end of * the Dealership object's life by calling flush(). * This class also provides methods for adding, remove, and searching for cars * from the list. * * @author Vangelis Metsis */
  • 3. public class Dealership { private ArrayList carList; /** * This constructor is hard-coded to open "cars.txt" and * initialize the carList with its contents. If no such file * exists, then one is created. The contents of the file are "loaded" into * the carList ArrayList in no particular order. The file is then closed * during the duration of the program until flush() is called. * @throws IOException */ public Dealership() throws IOException { carList = new ArrayList<>(); Scanner carDatabase; File dataFile = new File("cars.txt"); // If data file does not exist, create it. if (!dataFile.exists()) { System.err.println("Cars.txt does not exist, creating one now . . ."); //if the file doesn't exists, create it PrintWriter pw = new PrintWriter("cars.txt"); //close newly created file so we can reopen it pw.close(); } carDatabase = new Scanner(new FileReader(dataFile)); //Initialize the Array List with cars from cars.txt while (carDatabase.hasNextLine()) { // split values using the space character as separator String[] temp = carDatabase.nextLine().split(" "); carList.add(new Car(temp[0], temp[1], temp[2], Integer.parseInt(temp[3]), Integer.parseInt(temp[4]), Float.parseFloat(temp[5]))); } //Cars list is now in the Array List Completely so we can close the file carDatabase.close(); } /** * Method showCars displays the current list of cars in the Arraylist in no * particular order.
  • 4. * */ public void showCars() { showCars(carList); } /** * Private method used as an auxiliary method to display a given ArrayList * of cars in a formatted manner. * * @param cars the car list to be displayed. */ private void showCars(ArrayList cars) { System.out.println(" _____________________________________________________________________________ _____"); System.out.println("| Entry | VIN | Make | Model | Year | Mileage | Price |"); System.out.println("|----------------------------------------------------------------------------------|"); for (int i = 0; i < cars.size(); i++) { System.out.println(String.format("| %-6s| %-7s| %-15s| %-15s| %-5s| %-8s| $%-12s|", Integer.toString(i + 1), cars.get(i).getVin(), cars.get(i).getMake(), cars.get(i).getModel(), Integer.toString(cars.get(i).getYear()), Integer.toString(cars.get(i).getMileage()), String.format("%.2f", cars.get(i).getPrice()))); } System.out.println("|____________________________________________________________ ______________________| "); } /** * This method displays cars that have a price within the range of * low to high. * * @param low a float that is the lower bound price. * @param high a float that is the upper bound price. */ public void showCarsRange(float low, float high) { ArrayList cars = new ArrayList<>(); for (Car car : carList) {
  • 5. if ((low <= car.getPrice()) && (car.getPrice() <= high)) { cars.add(car); } } showCars(cars); } /** * This method can be used to search for a car in the Arraylist of cars. * * @param vin a String that represents the vehicle * identification number of the car that to be searched for * @return the int index of the car in the Arraylist of cars, * or -1 if the search failed. */ public int findCar(String vin) { int index = -1; for (int i = 0; i < carList.size(); i++) { String temp = carList.get(i).getVin(); if (vin.equalsIgnoreCase(temp)) { index = i; break; } } return index; } /** * This method is used to add a car to the carList ArrayList. In order for a * car to be added to the ArrayList it must comply with the following: * * 1. The car is not already in the ArrayList according to the VIN * as the unique key. * * 2. The VIN string matches the following regular expression: * "[A-Za-z0-9]{5}" or in other words: it
  • 6. * is 5 avinhanumeric characters. * * 3. The make of the car is only alphanumeric characters. * * 4. The model of the car is only alphanumeric or contains a dash "-". * * 5. The year model must be exactly four digits. * * 6. The price must be non-negative. * * @param toAdd the Car object to add to the * carList */ public void addCar(String vin, String make, String model, String year, String mileage, String price) { if (this.findCar(vin) != -1) { System.err.println("Car already exists in database. "); return; } if (!vin.matches("[A-Za-z0-9]{5}")) { System.err.println("Invalid VIN: not proper format." + "VIN must be at least 5 alphanumeric characters."); return; } if (!make.matches("[A-Za-z0-9]+")) { System.err.println("Invalid make type: " + "The car's make must be an alphanumeric string."); return; }
  • 7. if (!model.matches("[A-Z0-9a-z-]+")) { System.err.println("Invalid model type: " + "The car's model must be a non-numeric alphabet string."); return; } if (!year.matches("[0-9]{4}") ) { System.err.println("Invalid year: " + "The car's year of manufacture must be a 4 digit number. "); return; } if (!mileage.matches("[0-9]{1,6}")) { System.err.println("Invalid milage: " + "The car's mileage has to be an integer number between 0 and 999999. "); return; } if (Float.parseFloat(price) < 0) { System.err.println("The entered price cannot be negative."); return; } //If passed all the checks, add the car to the list carList.add(new Car(vin, make, model, Integer.parseInt(year), Integer.parseInt(mileage), Float.parseFloat(price))); System.out.println("Car has been added. "); } /** * This method will remove a car from the carList ArrayList. It * will remove the first instance of a car that matches the car that was * passed to this method in the carList using the * equals() override in the class Car. If no such * car exists, it will produce an error message. *
  • 8. * @param toDelete the car object to be removed. */ public void removeCar(Car toDelete) { if (carList.remove(toDelete)) { System.out.println("Car was removed. "); } else { System.err.println("Car does not exist in database."); } } /** * This method is used to retrieve the Car object from the * carList at a given index. * * @param i the index of the desired car object. * @return the car object at the index or null if the index is * invalid. */ public Car getCar(int i) { if (i < carList.size() && i >= 0) { return carList.get(i); } else { System.err.println("Invalid Index. Please enter another command or 'h' to list the commands."); return null; } } /** * This should be the last method to be called from this class. It opens * "cars.txt" and overwrites it with a text representation of * all the cars in the carList. * @throws IOException */ public void flush() throws IOException { PrintWriter pw = new PrintWriter("cars.txt"); for (Car c : carList) { pw.print(c.toString());