public class BookDaoTest { @Test public void testAddBook() { //创建一个Book对象 Book book = new Book(); book.setBook_ISBN("456"); book.setBook_category("计算机科学"); book.setBook_name("java编程思想"); book.setBook_author("张三"); book.setPublish_house("机械工业出版社"); book.setPublish_time("2020-01"); book.setBook_price(65); book.setBook_sum(10); book.setBook_remain(2); book.setBook_introduce("无"); //调用addBook方法 BookDao bookDao = new BookDao(); bookDao.addBook(book); //获取添加后的图书信息 String sql = "select * from Book where book_ISBN=?"; Object[] params = { "456" }; ResultSet rs = DBHelper.executeQuery(sql, params); // 检查添加的图书信息是否正确 assertEquals("456", ((Book) rs).getBook_ISBN()); assertEquals("计算机科学", ((Book) rs).getBook_category()); assertEquals("java编程思想", ((Book) rs).getBook_name()); assertEquals("张三", ((Book) rs).getBook_author()); assertEquals("机械工业出版社", ((Book) rs).getPublish_house()); assertEquals("2020-01",((Book) rs).getPublish_time()); assertEquals(65, ((Book) rs).getBook_price(), 0.01); assertEquals(12, ((Book) rs).getBook_sum()); assertEquals(4, ((Book) rs).getBook_remain()); assertEquals("无", ((Book) rs).getBook_introduce()); } }怎么修改
时间: 2024-03-25 15:36:27 浏览: 56
在这段代码中,我们需要将ResultSet对象转换为Book对象进行属性值的比较。因此,需要在ResultSet对象中获取正确的属性值,并将其设置到Book对象中,然后再进行比较。修改后的代码如下:
```
public class BookDaoTest {
@Test
public void testAddBook() {
//创建一个Book对象
Book book = new Book();
book.setBook_ISBN("456");
book.setBook_category("计算机科学");
book.setBook_name("java编程思想");
book.setBook_author("张三");
book.setPublish_house("机械工业出版社");
book.setPublish_time("2020-01");
book.setBook_price(65);
book.setBook_sum(10);
book.setBook_remain(2);
book.setBook_introduce("无");
//调用addBook方法
BookDao bookDao = new BookDao();
bookDao.addBook(book);
//获取添加后的图书信息
String sql = "select * from Book where book_ISBN=?";
Object[] params = {"456"};
ResultSet rs = DBHelper.executeQuery(sql, params);
Book addedBook = new Book();
try {
if (rs.next()) {
addedBook.setBook_ISBN(rs.getString("book_ISBN"));
addedBook.setBook_category(rs.getString("book_category"));
addedBook.setBook_name(rs.getString("book_name"));
addedBook.setBook_author(rs.getString("book_author"));
addedBook.setPublish_house(rs.getString("publish_house"));
addedBook.setPublish_time(rs.getString("publish_time"));
addedBook.setBook_price(rs.getDouble("book_price"));
addedBook.setBook_sum(rs.getInt("book_sum"));
addedBook.setBook_remain(rs.getInt("book_remain"));
addedBook.setBook_introduce(rs.getString("book_introduce"));
}
} catch (SQLException e) {
e.printStackTrace();
}
//检查添加的图书信息是否正确
assertEquals("456", addedBook.getBook_ISBN());
assertEquals("计算机科学", addedBook.getBook_category());
assertEquals("java编程思想", addedBook.getBook_name());
assertEquals("张三", addedBook.getBook_author());
assertEquals("机械工业出版社", addedBook.getPublish_house());
assertEquals("2020-01", addedBook.getPublish_time());
assertEquals(65, addedBook.getBook_price(), 0.01);
assertEquals(10, addedBook.getBook_sum());
assertEquals(2, addedBook.getBook_remain());
assertEquals("无", addedBook.getBook_introduce());
}
}
```
这样就能正确比较添加的图书信息是否正确了。
阅读全文