После многочисленных запусков теста он начинает выдавать fail
После многочисленных успешных запусков тестов в этом методе и схожих, где используются коллекции у меня выдает fail. Вот код метода теста:
@Test
void findAllWithBooks() {
var book1 = bookDao.save(generateBook("Test1", "Test1"));
var book2 = bookDao.save(generateBook("Test2", "Test2"));
var book3 = bookDao.save(generateBook("Test3", "Test3"));
var book4 = bookDao.save(generateBook("Test4", "Test4"));
var reader1 = readerDao.save(generateReader("Test1"));
var reader2 = readerDao.save(generateReader("Test2"));
bookDao.borrow(book1.getId(), reader1.getId());
bookDao.borrow(book2.getId(), reader1.getId());
bookDao.borrow(book3.getId(), reader2.getId());
Map<Reader, List<Book>> expectedMap = Map.of(
reader1, List.of(book2, book1),
reader2, List.of(book3)
);
Map<Reader, List<Book>> actualMap = readerDao.findAllWithBooks();
assertThat(actualMap).isEqualTo(expectedMap);
}
Метод bookDao.borrow работает корректно, но вот его код:
@Override
public void borrow(long bookId, long readerId) {
var query = "UPDATE book SET reader_id = ? WHERE id = ?";
try (var connection = DBUtil.getConnection();
var borrowStatement = connection.prepareStatement(query)) {
borrowStatement.setLong(1, readerId);
borrowStatement.setLong(2, bookId);
borrowStatement.executeUpdate();
} catch (SQLException e) {
throw new DaoOperationException(
String.format("Error borrowing book with id: %d for reader id: %d", bookId, readerId), e);
}
}
Код из метода findAllWithBooks():
@Override
public Map<Reader, List<Book>> findAllWithBooks() {
var query =
"""
SELECT
reader.id AS readerId,
reader.name AS readerName,
book.id AS bookId,
book.name AS bookName,
book.author AS bookAuthor,
book.reader_id
FROM reader
LEFT JOIN book ON reader.id = book.reader_id
""";
try (var connection = DBUtil.getConnection();
var selectAllReadersWithBooksStatement = connection.createStatement()) {
var resultSet = selectAllReadersWithBooksStatement.executeQuery(query);
Map<Reader, List<Book>> map = new HashMap<>();
while (resultSet.next()) {
var borrowedBooks =
map.computeIfAbsent(DaoUtils.mapResultSetToReader(resultSet), k -> new ArrayList<>());
if (resultSet.getString("bookName") != null) {
var book = DaoUtils.mapResultSetToBook(resultSet);
borrowedBooks.add(book);
}
}
return map;
} catch (SQLException e) {
throw new DaoOperationException("Error finding readers with borrowed books list!");
}
}
И вот сообщение, которое показывается при fail-e:
org.opentest4j.AssertionFailedError: expected: {Reader{id=114, name='Test2'}=[Book{id=146, name='Test3', author='Test3', readerId=0}], Reader{id=113, name='Test1'}=[Book{id=145, name='Test2', author='Test2', readerId=0}, Book{id=144, name='Test1', author='Test1', readerId=0}]} but was: {Reader{id=113, name='Test1'}=[Book{id=144, name='Test1', author='Test1', readerId=113}, Book{id=145, name='Test2', author='Test2', readerId=113}], Reader{id=114, name='Test2'}=[Book{id=146, name='Test3', author='Test3', readerId=114}]}
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at dao.ReaderDaoIT.findAllWithBooks(ReaderDaoIT.java:73) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)