写出对应的测试类
时间: 2023-07-04 11:10:29 浏览: 87
我自己的测试类
以下是对上述代码进行测试的测试类:
```
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.HashMap;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class LibraryManagementSystemTest {
@Test
public void testAddBook() {
ArrayList<Book> books = new ArrayList<Book>();
books.add(new Book("978-7-121-34735-5", "Java编程思想", "Bruce Eckel", "2007-06-01", 98.0, "计算机"));
LibraryManagementSystem.addBook(books, new java.util.Scanner("978-7-121-34736-5 Java核心技术 原书第11版 Cay S. Horstmann 2018-04-01 109.0 计算机"));
assertEquals(2, books.size());
}
@Test
public void testDeleteBook() {
ArrayList<Book> books = new ArrayList<Book>();
books.add(new Book("978-7-121-34735-5", "Java编程思想", "Bruce Eckel", "2007-06-01", 98.0, "计算机"));
books.add(new Book("978-7-121-34736-5", "Java核心技术 原书第11版", "Cay S. Horstmann", "2018-04-01", 109.0, "计算机"));
LibraryManagementSystem.deleteBook(books, new java.util.Scanner("978-7-121-34735-5"));
assertEquals(1, books.size());
}
@Test
public void testSearchBook() {
ArrayList<Book> books = new ArrayList<Book>();
books.add(new Book("978-7-121-34735-5", "Java编程思想", "Bruce Eckel", "2007-06-01", 98.0, "计算机"));
books.add(new Book("978-7-121-34736-5", "Java核心技术 原书第11版", "Cay S. Horstmann", "2018-04-01", 109.0, "计算机"));
java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream();
System.setOut(new java.io.PrintStream(outContent));
LibraryManagementSystem.searchBook(books, new java.util.Scanner("978-7-121-34735-5"));
assertEquals("ISBN:978-7-121-34735-5\n书名:Java编程思想\n作者:Bruce Eckel\n出版日期:2007-06-01\n单价:98.0\n图书类别:计算机\n", outContent.toString());
}
@Test
public void testModifyBook() {
ArrayList<Book> books = new ArrayList<Book>();
books.add(new Book("978-7-121-34735-5", "Java编程思想", "Bruce Eckel", "2007-06-01", 98.0, "计算机"));
LibraryManagementSystem.modifyBook(books, new java.util.Scanner("978-7-121-34735-5\n\n\n\n110.0\n"));
Book book = books.get(0);
assertEquals(110.0, book.getPrice());
}
@Test
public void testReaderLogin() {
HashMap<String, String> readers = new HashMap<String, String>();
readers.put("Tom", "123456");
java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream();
System.setOut(new java.io.PrintStream(outContent));
LibraryLogin.readerLogin(readers, new java.util.Scanner("Tom\n123456\n"));
assertEquals("登录成功!欢迎Tom使用图书馆管理系统!\n", outContent.toString());
}
@Test
public void testManagerLogin() {
HashMap<String, String> managers = new HashMap<String, String>();
managers.put("Alice", "111111");
java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream();
System.setOut(new java.io.PrintStream(outContent));
LibraryLogin.managerLogin(managers, new java.util.Scanner("Alice\n111111\n"));
assertEquals("登录成功!欢迎Alice使用图书馆管理系统!\n", outContent.toString());
}
@Test
public void testBookManagerLogin() {
HashMap<String, String> bookManagers = new HashMap<String, String>();
bookManagers.put("Cindy", "333333");
java.io.ByteArrayOutputStream outContent = new java.io.ByteArrayOutputStream();
System.setOut(new java.io.PrintStream(outContent));
LibraryLogin.bookManagerLogin(bookManagers, new java.util.Scanner("Cindy\n333333\n"));
assertEquals("登录成功!欢迎Cindy使用图书馆管理系统!\n", outContent.toString());
}
}
```
阅读全文