Commit 3d6f570af580149aea7d27e3dd28ed61c422a71a
1 parent
b43364bf
Exists in
master
Refatoramento do teste unitário em função do método findAll
Showing
1 changed file
with
39 additions
and
21 deletions
Show diff stats
impl/extension/jpa/src/test/java/br/gov/frameworkdemoiselle/template/JPACrudTest.java
... | ... | @@ -129,28 +129,37 @@ public class JPACrudTest { |
129 | 129 | this.contactDAO.delete(this.contact.getId()); |
130 | 130 | verify(this.entityManager); |
131 | 131 | } |
132 | - | |
132 | + | |
133 | + private TypedQuery<Contact> makeTypedQuery() { | |
134 | + @SuppressWarnings("unchecked") | |
135 | + TypedQuery<Contact> typeQuery = EasyMock.createMock(TypedQuery.class); | |
136 | + expect(typeQuery.setFirstResult(EasyMock.anyInt())).andReturn(null); | |
137 | + expect(typeQuery.setMaxResults(EasyMock.anyInt())).andReturn(null); | |
138 | + expect(typeQuery.getResultList()).andReturn(createContacts(1)); | |
139 | + return typeQuery; | |
140 | + } | |
141 | + | |
133 | 142 | @Test |
134 | 143 | public void testCountAll() { |
135 | 144 | |
136 | 145 | Pagination pagination = new PaginationImpl(); |
146 | + pagination.setPageSize(10); | |
137 | 147 | PaginationContext actualContext = PowerMock.createMock(PaginationContext.class); |
138 | 148 | expect(actualContext.getPagination(Contact.class)).andReturn(pagination); |
139 | 149 | @SuppressWarnings("unchecked") |
140 | 150 | Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); |
141 | 151 | expect(paginationContext.get()).andReturn(actualContext); |
142 | 152 | setInternalState(this.contactDAO, "paginationContext", paginationContext); |
153 | + | |
154 | + TypedQuery<Contact> typeQuery = makeTypedQuery(); | |
143 | 155 | |
144 | 156 | Query query = EasyMock.createMock(Query.class); |
145 | - expect(query.setFirstResult(EasyMock.anyInt())).andReturn(query); | |
146 | - expect(query.setMaxResults(EasyMock.anyInt())).andReturn(query); | |
147 | 157 | expect(query.getSingleResult()).andReturn(10L); |
158 | + | |
159 | + expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); | |
160 | + expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query); | |
148 | 161 | |
149 | - expect(this.entityManager.createQuery("select this from Contact this")).andReturn(query); | |
150 | - expect(this.entityManager.createQuery("select count(this) from Contact this")).andReturn(query); | |
151 | - expect(query.getResultList()).andReturn(createContacts(1)); | |
152 | - | |
153 | - replayAll(query, this.entityManager, paginationContext); | |
162 | + replayAll(typeQuery, query, this.entityManager, paginationContext); | |
154 | 163 | |
155 | 164 | List<Contact> find = this.contactDAO.findAll(); |
156 | 165 | |
... | ... | @@ -171,11 +180,13 @@ public class JPACrudTest { |
171 | 180 | expect(paginationContext.get()).andReturn(actualContext); |
172 | 181 | setInternalState(this.contactDAO, "paginationContext", paginationContext); |
173 | 182 | |
183 | + TypedQuery<Contact> typeQuery = makeTypedQuery(); | |
184 | + | |
174 | 185 | Query query = EasyMock.createMock(Query.class); |
175 | 186 | |
176 | 187 | expect(query.getSingleResult()).andThrow(new DemoiselleException("")); |
177 | - expect(this.entityManager.createQuery("select this from Contact this")).andReturn(query); | |
178 | - expect(this.entityManager.createQuery("select count(this) from Contact this")).andReturn(query); | |
188 | + expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); | |
189 | + expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query); | |
179 | 190 | |
180 | 191 | replayAll(query, this.entityManager, paginationContext); |
181 | 192 | |
... | ... | @@ -280,27 +291,31 @@ public class JPACrudTest { |
280 | 291 | |
281 | 292 | @Test |
282 | 293 | public void testFindAll() { |
283 | - | |
294 | + | |
295 | + Pagination pagination = new PaginationImpl(); | |
296 | + pagination.setPageSize(10); | |
284 | 297 | PaginationContext actualContext = PowerMock.createMock(PaginationContext.class); |
285 | - expect(actualContext.getPagination(Contact.class)).andReturn(null); | |
298 | + expect(actualContext.getPagination(Contact.class)).andReturn(pagination); | |
286 | 299 | @SuppressWarnings("unchecked") |
287 | 300 | Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); |
288 | 301 | expect(paginationContext.get()).andReturn(actualContext); |
289 | 302 | setInternalState(this.contactDAO, "paginationContext", paginationContext); |
303 | + | |
304 | + TypedQuery<Contact> typeQuery = makeTypedQuery(); | |
290 | 305 | |
291 | 306 | Query query = EasyMock.createMock(Query.class); |
292 | - // expect(query.setFirstResult(EasyMock.anyInt())).andReturn(query); | |
293 | - // expect(query.setMaxResults(EasyMock.anyInt())).andReturn(query); | |
294 | - expect(this.entityManager.createQuery("select this from Contact this")).andReturn(query); | |
295 | - expect(query.getResultList()).andReturn(createContacts(1)); | |
307 | + expect(query.getSingleResult()).andReturn(10L); | |
308 | + | |
309 | + expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); | |
310 | + expect(this.entityManager.createQuery("SELECT COUNT(THIS) FROM CONTACT THIS")).andReturn(query); | |
296 | 311 | |
297 | - replayAll(query, this.entityManager, paginationContext); | |
312 | + replayAll(typeQuery, query, this.entityManager, paginationContext); | |
298 | 313 | |
299 | 314 | List<Contact> find = this.contactDAO.findAll(); |
300 | 315 | |
301 | 316 | assertEquals(1, find.size()); |
302 | 317 | assertTrue(find.iterator().next().getId().equals(1L)); |
303 | - | |
318 | + | |
304 | 319 | verifyAll(); |
305 | 320 | } |
306 | 321 | |
... | ... | @@ -313,12 +328,15 @@ public class JPACrudTest { |
313 | 328 | Instance<PaginationContext> paginationContext = PowerMock.createMock(Instance.class); |
314 | 329 | expect(paginationContext.get()).andReturn(actualContext); |
315 | 330 | setInternalState(this.contactDAO, "paginationContext", paginationContext); |
331 | + | |
332 | + @SuppressWarnings("unchecked") | |
333 | + TypedQuery<Contact> typeQuery = EasyMock.createMock(TypedQuery.class); | |
334 | + expect(typeQuery.getResultList()).andThrow(new DemoiselleException("")); | |
316 | 335 | |
317 | 336 | Query query = EasyMock.createMock(Query.class); |
318 | - expect(this.entityManager.createQuery("select this from Contact this")).andReturn(query); | |
319 | - expect(query.getResultList()).andThrow(new DemoiselleException("")); | |
337 | + expect(this.entityManager.createQuery("select this from Contact this", Contact.class)).andReturn(typeQuery); | |
320 | 338 | |
321 | - replayAll(query, this.entityManager, paginationContext); | |
339 | + replayAll(typeQuery, query, this.entityManager, paginationContext); | |
322 | 340 | |
323 | 341 | try { |
324 | 342 | this.contactDAO.findAll(); | ... | ... |