测试 | Mock object
实例:
一个简单的图书管理系统包括三个类:
- Book提供了书籍的定义
- BookService提供了有关书籍借阅和归还的服务
- BookMananger查找书籍并把对书籍状态的更改更新到数据库中
目前,Book类和Bookservice类都已完成,但BookManager只是一个接口,对它的实现还没有完成。
目标: 对BookService类中的borrowBook和returnBook两个方法进行单元测试的代码,要求使用EasyMock模拟BookManager对象。
package org.demo.test.library; public class Book { private String name; private String isbn; //书号 private double price; private boolean inShell; // 是否在架上 public Book(){} public Book(String name, String isbn, double price, boolean inShell){ this.name = name; this.isbn = isbn; this.price = price; this.inShell = inShell; } /** * get set方法省略 **/ public boolean isInShell(){ return this.inShell; } public void setInShell(boolean inShell){ this.inShell = inShell; } }
package org.demo.test.library; public class BookService { private BookManager bookManager; public BookService(BookManager bookManager){ this.bookManager = bookManager; } public boolean borrowBook (String isbn){ Book book = bookManager.findBook(isbn); if (book != null && book.isInShell()){ //修改shell值 book.setInShell(false); bookManager.updateBook(book); return true; } return false; } public boolean returnBook (String isbn){ Book book = bookManager.findBook(isbn); if (book != null && !book.isInShell()){ book.setInShell(true); return true; } return false; } }
package org.demo.test.library; public interface BookManager { Book findBook(String isbn); // 根据isbn在数据库中找到相应的书籍 void updateBook(Book book); // 更新数据库中书籍的状态 }
package org.demo.test.library; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.junit.Assert.*; import org.demo.test.account.AccountManager; import org.junit.Before; import org.junit.Test; import org.junit.rules.ExpectedException; public class BookServiceTest { private BookManager bookManager; private BookService bookService; @Before public void setUp() throws Exception { bookManager = createMock("bookManager",BookManager.class); bookService = new BookService(bookManager); } @Test public void testBorrowBook() { Book book1 = new Book("Java","123",20.2,true); Book book2 = new Book("Web","124",20.9,false); Book book3 = new Book("JavaScript","125",20.9,false); bookManager.updateBook(book1); bookManager.updateBook(book2); /** * 定义查询接口返回值 */ expect(bookManager.findBook("123")).andReturn(book1); expect(bookManager.findBook("124")).andReturn(book2); expect(bookManager.findBook("126")).andReturn(null); /** * */ replay(bookManager); /** * 执行测试 */ bookService.borrowBook("123"); bookService.borrowBook("124"); bookService.borrowBook("126"); assertEquals(false, book1.isInShell()); assertEquals(false, book2.isInShell()); } @Test public void testReturnBook() { Book book1 = new Book("Java","123",20.2,true); Book book2 = new Book("Web","124",20.9,false); bookManager.updateBook(book1); bookManager.updateBook(book2); expect(bookManager.findBook("123")).andReturn(book1); expect(bookManager.findBook("124")).andReturn(book2); expect(bookManager.findBook("126")).andReturn(null); replay(bookManager); bookService.returnBook("123"); bookService.returnBook("124"); bookService.returnBook("126"); assertEquals(true, book1.isInShell()); assertEquals(true, book2.isInShell()); } }