lundi 6 avril 2015

How Should I write JUnit test in the following case?

I am new in the Unit Testing. I have a class Ckeckout which main function is to print the amount to be paid for books. The user types the titles of the books in the command line, and based on some calculations I have to output the final price. Here is the Book class:



public class Book {
private String title;
private double price;
private int year;

public Book(String title, double price, int year) {
super();
this.title = title;
this.price = price;
this.year = year;
}
public String getTitle(){
return title;
}
public double getPrice(){
return price;
}
public int getYear(){
return year;
}
}


And here is the Checkout class:



public class Checkout {
private static final Map<String, Book> library = new HashMap<String, Book>();

private List<Book> books;

public Checkout(List<Book> books) {
super();
this.books = books;
}

//calculate the final price
private double getPrice(){
//return some double
}

public static void main(String[] args) throws IOException {
//fill the HashMap with all books
fill(library);
System.out.println("Add books: ");
//I assume that the input is correctly given
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = in.readLine().trim();
//list of ordered titles
List<String> titles = Arrays.asList(input.split(", "));
//convert String of Titles to List of Book objects
List<Book> orders = getOrders(titles);
Checkout check = new Checkout(orders);
//get the final price
double price = check.getPrice();
//format the output
price = Math.floor(price * 100) / 100.0;
System.out.println("Total amount: " + price);
}

//convert the input from titles as String to Book object
private static List<Book> getOrders(List<String> input) {

}

//fill HashMap with books as values and titles as keys
private static void fill(Map<String, Book> library) {

}
}


What I want to test is just getPrice method. However, to do so, do I have to create list of Book objects in my CheckoutTest? Also, I will have to verify the final result with some very long number (like 62.01997301). Isn't it easier, to test the main() method, since in my Unit test, there won't be any need to create the Book objects (I will work only with Strings) and I can verify the output with shorter number (like 62.01)?


Aucun commentaire:

Enregistrer un commentaire