首页 > 解决方案 > 编写正确的 jUnit 测试

问题描述

在这里,我做了一个关于我用 Java 制作的库系统的作业。我对我的书面 jUnit 有一些负面评论。我的导师告诉我,我不应该在单元测试中使用 if else 语句。那么正确的方法是什么?因为就我的想法而言,我一直在正确使用它。

目录.java

import java.util.ArrayList;

/**
 * The type Catalog.
 */
public class Catalog {

    private static ArrayList<Book> bookArrayList;

    /**
     * Instantiates a new Catalog.
     */
    public Catalog() {
        bookArrayList = new ArrayList<>();
    }

    /**
     * Gets book array list.
     *
     * @return the book array list
     */
    public static ArrayList<Book> getBookArrayList() {
        return bookArrayList;
    }

    /**
     * Sets book array list.
     *
     * @param bookArrayList the book array list
     */
    public static void setBookArrayList(ArrayList<Book> bookArrayList) {
        Catalog.bookArrayList = bookArrayList;
    }

    /**
     * Find book by title book.
     *
     * @param title the title
     * @return the book
     */
    public static Book findBookByTitle(String title) {
        for (Book book : bookArrayList) {
            if (book.getBookDetails().getTitle().equalsIgnoreCase(title))
                return book;
        }
        return null;
    }

    /**
     * Find book by isbn book.
     *
     * @param ISBN the isbn
     * @return the book
     */
    public static Book findBookByISBN(Long ISBN) {
        for (Book book : bookArrayList) {
            if (book.getISBN().equals(ISBN))
                return book;
        }
        return null;
    }

    /**
     * Loan book boolean.
     *
     * @param customer the customer
     * @param ISBN     the isbn
     * @return the boolean
     */
    public static boolean loanBook(Customer customer, Long ISBN) {
        for (Book book : bookArrayList) {
            if (book.getISBN().equals(ISBN)) {
                if (book.getAvailable()) {
                    book.setAvailable(false);
                    ArrayList<Book> b = customer.getRentedBooks();
                    b.add(book);
                    customer.setRentedBooks(b);
                    return true;
                } else return false;
            }
        }
        return false;
    }

    /**
     * Return book boolean.
     *
     * @param customer the customer
     * @param ISBN     the isbn
     * @return the boolean
     */
    public static boolean returnBook(Customer customer, Long ISBN) {
        for (Book book : bookArrayList) {
            if (book.getISBN().equals(ISBN)) {
                if (!book.getAvailable()) {
                    book.setAvailable(true);
                    ArrayList<Book> b = customer.getRentedBooks();
                    b.remove(book);
                    customer.setRentedBooks(b);
                    return true;
                } else return false;
            }
        }
        return false;
    }
}

目录测试.java

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

class CatalogTest { //catalog Testing class
    static Customer customer1, customer2, customer3;    //all class 3 objects to store dummy data to test the methods
    static Book book1, book2, book3;
    static BookDetails bookDetails1, bookDetails2, bookDetails3;
    static Author author1, author2, author3;
    static ArrayList<Book> bookArrayList = new ArrayList<>();   //arrayList

    @Test
    void findBookByTitle() {   //testing method
        String bookName = "JavaBook1";  //finding JavaBook1 in book list
        Catalog.setBookArrayList(bookArrayList);    //set booklist in Catalog
        Book result = Catalog.findBookByTitle(bookName);    //find bookList
        if (result == null) {   //result null book not found
            Assertions.assertNull(result);
            System.out.println("Book not found!");
        } else {
            Assertions.assertEquals(bookName, result.getBookDetails().getTitle());  //book found
            System.out.println("Book Found");
        }
    }

    @Test
    void findBookByISBN() { //testingMethod
        long isbn = book1.getISBN();  //checking isbn
        Catalog.setBookArrayList(bookArrayList);    //setting arrayList into Catalog
        Book result = Catalog.findBookByISBN(isbn); //find book by isbn
        if (result == null) {   //result null book not found
            Assertions.assertNull(result);
            System.out.println("Book not found!");
        } else {    //else book found
            Assertions.assertEquals(isbn, result.getISBN());
            System.out.println("Book Found");
        }
    }

    @Test
    void loanBook() {   //loan books
        long isbn = book1.getISBN();    //get isbn
        long isbn2 = book2.getISBN();
        Catalog.setBookArrayList(bookArrayList);    //set booklist
        Assertions.assertTrue(Catalog.loanBook(customer1, isbn));   //loan book1 to customer
        Assertions.assertTrue(Catalog.loanBook(customer1, isbn2));  //loan book 2 to customer
        System.out.println("Total rented Books to customer 1  " + customer1.getRentedBooks().size());   //get size of loan books
    }

    @Test
    void returnBook() {
        long isbn = book1.getISBN();    //get isbn
        long isbn2 = book2.getISBN();
        Catalog.setBookArrayList(bookArrayList);    //set arrayList
        Catalog.loanBook(customer1, isbn2); //first loan books
        Catalog.loanBook(customer1, isbn);
        Assertions.assertTrue(Catalog.returnBook(customer1, isbn)); //test if book return fucntion work
        System.out.println("Book Returned");
        System.out.println("Total rented Books to customer 1  " + customer1.getRentedBooks().size());   //get size of loan books
    }

    @BeforeAll
    static void addDataForTest() {   //add Data for Test
        customer1 = new Customer("javaprogrammer1@email.com", "Java1", "73929474", "Netherland St:1", "1234", "Male", "cityNetherLand1", new ArrayList<>());
        customer2 = new Customer("javaprogrammer2@email.com", "Java2", "73929475", "Netherland St:2", "1235", "Male", "cityNetherLand2", new ArrayList<>());
        customer3 = new Customer("javaprogrammer3@email.com", "Java3", "73929476", "Netherland St:3", "1236", "Male", "cityNetherLand3", new ArrayList<>());
        author1 = new Author("author1@email.com", "Author1", "73926774", "Netherland1", "1237", "Male", "cityNetherLand1", 40);
        author2 = new Author("author2@email.com", "Author2", "73926775", "Netherland2", "1238", "Male", "cityNetherLand2", 50);
        author3 = new Author("author3@email.com", "Author3", "73926776", "Netherland3", "1239", "Male", "cityNetherLand3", 30);
        bookDetails1 = new BookDetails("JavaBook1", author1, 2019, "www.java1.com", "English", 400, "Netherland");
        bookDetails2 = new BookDetails("JavaBook2", author2, 2018, "www.java2.com", "English", 600, "Netherland");
        bookDetails3 = new BookDetails("JavaBook3", author3, 2010, "www.java3.com", "English", 500, "Netherland");
        book1 = new Book(Book.generateISBN(), bookDetails1, true);
        book2 = new Book(Book.generateISBN(), bookDetails2, true);
        book3 = new Book(Book.generateISBN(), bookDetails3, true);
        bookArrayList.add(book1);
        bookArrayList.add(book2);
        bookArrayList.add(book3);   //this Method is used for adding demo data to test Catalog Class Methods..
    }
}

标签: javajunit

解决方案


我假设你想测试你的 find 方法是否找到了请求的书。如果 find 方法返回 null,则测试需要立即失败;在遇到该故障条件后没有应该执行的逻辑。这就是为什么if不合适的原因。

将断言视为“除非此条件为真,否则立即停止”:

@Test
void findBookByTitle() {   //testing method
    String bookName = "JavaBook1";  //finding JavaBook1 in book list
    Catalog.setBookArrayList(bookArrayList);    //set booklist in Catalog
    Book result = Catalog.findBookByTitle(bookName);    //find bookList

    Assertions.assertNotNull(result,
        "Failed to find a Book with the title \"" + bookName + "\".");

    Assertions.assertEquals(bookName, result.getBookDetails().getTitle(),
        "Returned book does not have the correct title.");
}

推荐阅读