我需要用Java语言编写的完整的图书管理系统单元测试源代码
时间: 2023-08-14 17:07:40 浏览: 93
好的,下面是一个用Java语言编写的完整的图书管理系统单元测试源代码,您可以参考一下:
```java
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class LibraryTest {
private Library library;
private Book book;
private User user;
@BeforeEach
public void setUp() {
library = new Library();
book = new Book("123456", "The Lord of the Rings", "J.R.R. Tolkien", "Fantasy");
user = new User("001", "John Smith");
}
@Test
public void testAddBook() {
library.addBook(book);
Assertions.assertEquals(library.getBooks().get("123456"), book);
}
@Test
public void testRemoveBook() {
library.addBook(book);
library.removeBook("123456");
Assertions.assertEquals(library.getBooks().size(), 0);
}
@Test
public void testRegisterUser() {
library.registerUser(user);
Assertions.assertEquals(library.getUsers().get("001"), user);
}
@Test
public void testUnregisterUser() {
library.registerUser(user);
library.unregisterUser("001");
Assertions.assertEquals(library.getUsers().size(), 0);
}
@Test
public void testCheckOutBook() {
library.addBook(book);
library.registerUser(user);
library.checkOutBook("123456", "001");
Assertions.assertTrue(book.isCheckedOut());
Assertions.assertEquals(user.getBorrowedBooks(), "123456");
}
@Test
public void testCheckInBook() {
library.addBook(book);
library.registerUser(user);
library.checkOutBook("123456", "001");
library.checkInBook("123456", "001");
Assertions.assertFalse(book.isCheckedOut());
Assertions.assertEquals(user.getBorrowedBooks(), "");
}
@Test
public void testBookInfo() {
Assertions.assertEquals(book.bookInfo(), "123456 - The Lord of the Rings by J.R.R. Tolkien (Genre: Fantasy)");
}
@Test
public void testUserInfo() {
Assertions.assertEquals(user.userInfo(), "001 - John Smith");
}
@Test
public void testBorrowBook() {
user.borrowBook("123456");
Assertions.assertEquals(user.getBorrowedBooks(), "123456");
}
@Test
public void testReturnBook() {
user.borrowBook("123456");
user.returnBook("123456");
Assertions.assertEquals(user.getBorrowedBooks(), "");
}
}
```
上述代码是一个基于 JUnit 5 的单元测试框架,包括了对于 Book、User 和 Library 三个类的测试用例。您可以根据需要进行修改和完善,例如添加更多的测试用例,或者对于具体的项目进行适当的调整。
阅读全文