|
27 | 27 | import static org.junit.jupiter.api.Assertions.assertEquals; |
28 | 28 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
29 | 29 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
30 | 31 |
|
31 | 32 | import com.iluwatar.servicelayer.hibernate.HibernateUtil; |
32 | 33 | import java.util.concurrent.atomic.AtomicInteger; |
33 | 34 | import java.util.function.Function; |
| 35 | +import com.iluwatar.servicelayer.spell.Spell; |
| 36 | +import org.hibernate.HibernateException; |
34 | 37 | import org.junit.jupiter.api.AfterEach; |
35 | 38 | import org.junit.jupiter.api.BeforeEach; |
36 | 39 | import org.junit.jupiter.api.Test; |
@@ -143,14 +146,49 @@ void testSetName() { |
143 | 146 | } |
144 | 147 | @Test |
145 | 148 | void testFindByName() { |
146 | | - final var dao = getDao(); |
147 | | - final var allEntities = dao.findAll(); |
| 149 | + final var localDao = getDao(); |
| 150 | + final var allEntities = localDao.findAll(); |
148 | 151 | for (final var entity : allEntities) { |
149 | | - final var entityByName = dao.findByName(entity.getName()); |
| 152 | + final var entityByName = localDao.findByName(entity.getName()); |
150 | 153 | assertNotNull(entityByName); |
151 | 154 | assertEquals(entity.getId(), entityByName.getId()); |
152 | 155 | assertEquals(entity.getName(), entityByName.getName()); |
153 | 156 | } |
154 | 157 | } |
| 158 | + @Test |
| 159 | + void testPersistException() { |
| 160 | + final var faultyDao = new DaoBaseImpl<Spell>() { |
| 161 | + @Override |
| 162 | + public void persist(Spell entity) { |
| 163 | + throw new HibernateException("Simulated Hibernate exception"); |
| 164 | + } |
| 165 | + }; |
| 166 | + Spell faultyEntity = new Spell(); |
| 167 | + assertThrows(HibernateException.class, () -> faultyDao.persist(faultyEntity)); |
| 168 | + } |
155 | 169 |
|
| 170 | + @Test |
| 171 | + void testFindException() { |
| 172 | + final var faultyDao = new DaoBaseImpl<Spell>() { |
| 173 | + @Override |
| 174 | + public Spell find(Long id) { |
| 175 | + throw new HibernateException("Simulated Hibernate exception"); |
| 176 | + } |
| 177 | + }; |
| 178 | + |
| 179 | + assertThrows(HibernateException.class, () -> faultyDao.find(1L)); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void testDeleteException() { |
| 184 | + final var faultyDao = new DaoBaseImpl<Spell>() { |
| 185 | + @Override |
| 186 | + public void delete(Spell entity) { |
| 187 | + throw new HibernateException("Simulated Hibernate exception"); |
| 188 | + } |
| 189 | + }; |
| 190 | + |
| 191 | + Spell faultyEntity = new Spell(); |
| 192 | + assertThrows(HibernateException.class, () -> faultyDao.delete(faultyEntity)); |
| 193 | + } |
156 | 194 | } |
0 commit comments