Skip to content

Commit 519d4ec

Browse files
committed
Updated BaseDaoTest.java
Added exception testing for DaoBaseImpl methods
1 parent d3d6fa6 commit 519d4ec

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

service-layer/src/test/java/com/iluwatar/servicelayer/common/BaseDaoTest.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
import static org.junit.jupiter.api.Assertions.assertEquals;
2828
import static org.junit.jupiter.api.Assertions.assertNotNull;
2929
import static org.junit.jupiter.api.Assertions.assertNull;
30+
import static org.junit.jupiter.api.Assertions.assertThrows;
3031

3132
import com.iluwatar.servicelayer.hibernate.HibernateUtil;
3233
import java.util.concurrent.atomic.AtomicInteger;
3334
import java.util.function.Function;
35+
import com.iluwatar.servicelayer.spell.Spell;
36+
import org.hibernate.HibernateException;
3437
import org.junit.jupiter.api.AfterEach;
3538
import org.junit.jupiter.api.BeforeEach;
3639
import org.junit.jupiter.api.Test;
@@ -143,14 +146,49 @@ void testSetName() {
143146
}
144147
@Test
145148
void testFindByName() {
146-
final var dao = getDao();
147-
final var allEntities = dao.findAll();
149+
final var localDao = getDao();
150+
final var allEntities = localDao.findAll();
148151
for (final var entity : allEntities) {
149-
final var entityByName = dao.findByName(entity.getName());
152+
final var entityByName = localDao.findByName(entity.getName());
150153
assertNotNull(entityByName);
151154
assertEquals(entity.getId(), entityByName.getId());
152155
assertEquals(entity.getName(), entityByName.getName());
153156
}
154157
}
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+
}
155169

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+
}
156194
}

0 commit comments

Comments
 (0)