SlideShare a Scribd company logo
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.docx
mayank272369
 
Clean tests good tests
Clean tests   good testsClean tests   good tests
Clean tests good tests
Shopsys Framework
 
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
clarebernice
 
Code igniter unittest-part1
Code igniter unittest-part1Code igniter unittest-part1
Code igniter unittest-part1
Albert 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 Scenarios
Flutter 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[] args
YASHU40
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
Bhushan Nagaraj
 
Selenium web driver | java
Selenium web driver | javaSelenium web driver | java
Selenium web driver | java
Rajesh 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.pdf
forecastfashions
 
L9
L9L9
L9
lksoo
 
Testing Options in Java
Testing Options in JavaTesting Options in Java
Testing Options in Java
Michael 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 Execution
pCloudy
 
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-patterns
Hernan 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.docx
ajoy21
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
Visual Engineering
 
Java: Java Applets
Java: Java AppletsJava: Java Applets
Java: Java Applets
Tareq 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.pdf
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 
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
arsmobiles
 

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

CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 

Recently uploaded (20)

CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 

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());