package org.tests.query; import io.ebean.BaseTestCase; import io.ebean.DB; import io.ebean.FetchConfig; import io.ebean.Query; import org.junit.Test; import org.tests.model.basic.Customer; import org.tests.model.basic.ResetBasicData; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; public class TestQueryFindEach extends BaseTestCase { @Test public void test() { ResetBasicData.reset(); Query query = DB.find(Customer.class) .fetch("contacts", new FetchConfig().query(2)) .where().gt("id", 0).order("id") .setMaxRows(2).query(); final AtomicInteger counter = new AtomicInteger(0); query.findEach(customer -> { counter.incrementAndGet(); assertNotNull(customer.getName()); }); assertEquals(2, counter.get()); } /** * Test the behaviour when an exception is thrown inside the findVisit(). */ @Test(expected = IllegalStateException.class) public void testVisitThrowingException() { ResetBasicData.reset(); Query query = DB.find(Customer.class) .fetch("contacts", new FetchConfig().query(2)) .where().gt("id", 0).order("id") .setMaxRows(2).query(); final AtomicInteger counter = new AtomicInteger(0); query.findEach(customer -> { counter.incrementAndGet(); if (counter.intValue() > 0) { throw new IllegalStateException("cause a failure"); } }); fail("Never get here - exception thrown"); } }