diff --git a/ebean-core/src/test/java/org/tests/lazyloadconf/MyTest.java b/ebean-core/src/test/java/org/tests/lazyloadconf/BeanCollectionLazyLoadingTest.java similarity index 75% rename from ebean-core/src/test/java/org/tests/lazyloadconf/MyTest.java rename to ebean-core/src/test/java/org/tests/lazyloadconf/BeanCollectionLazyLoadingTest.java index 34ec36ea7..b0ef7592b 100644 --- a/ebean-core/src/test/java/org/tests/lazyloadconf/MyTest.java +++ b/ebean-core/src/test/java/org/tests/lazyloadconf/BeanCollectionLazyLoadingTest.java @@ -1,6 +1,6 @@ package org.tests.lazyloadconf; -import io.ebean.Ebean; +import io.ebean.DB; import io.ebean.Query; import org.junit.Test; @@ -12,10 +12,10 @@ import java.util.stream.Collectors; import static org.assertj.core.api.Assertions.assertThat; -public class MyTest { +public class BeanCollectionLazyLoadingTest { @Test - public void testRe() { + public void test() { AppConfig globalAppConfig = new AppConfig(); globalAppConfig.setId(1); @@ -25,7 +25,7 @@ public class MyTest { global.setName("global"); global.setAppConfig(globalAppConfig); globalAppConfig.getItems().add(global); - Ebean.save(globalAppConfig); + DB.save(globalAppConfig); AppConfig userAppConfig = new AppConfig(); userAppConfig.setId(2); @@ -35,32 +35,31 @@ public class MyTest { user.setName("user"); user.setAppConfig(userAppConfig); userAppConfig.getItems().add(user); - Ebean.save(userAppConfig); + DB.save(userAppConfig); AppConfig otherAppConfig = new AppConfig(); otherAppConfig.setId(3); otherAppConfig.setItems(new ArrayList<>()); - Ebean.save(otherAppConfig); + DB.save(otherAppConfig); Relationship globalRe = new Relationship(); globalRe.setId(1); globalRe.setAppConfig(globalAppConfig); - Ebean.save(globalRe); + DB.save(globalRe); Relationship userRe = new Relationship(); userRe.setId(2); userRe.setAppConfig(userAppConfig); - Ebean.save(userRe); + DB.save(userRe); Relationship otherRe = new Relationship(); otherRe.setId(3); otherRe.setAppConfig(otherAppConfig); - Ebean.save(otherRe); + DB.save(otherRe); - -// Start business processing - Query relationshipQuery = Ebean.find(Relationship.class); + // Start business processing + Query relationshipQuery = DB.find(Relationship.class); List relationshipList = relationshipQuery.where().idIn(1, 2, 3).findList(); assertThat(relationshipList.size()).isEqualTo(3); @@ -70,25 +69,21 @@ public class MyTest { .map((ac) -> new AbstractMap.SimpleImmutableEntry<>(ac.getId(), ac)) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); -// final List items = DB.find(AppConfig.class) -// .fetch("items") -// .findList(); - AppConfig g = map.get(1); AppConfig u = map.get(2); AppConfig o = map.get(3); - if(!u.getItems().isEmpty()){ + if (!u.getItems().isEmpty()) { + // a source of the problem came from invoking lazy loading here + // with the setItems call which is unnecessary due to being a ToMany g.setItems(u.getItems()); } assertThat(g.getItems().size()).isEqualTo(1); -// If this line of code is commented out, this test case will be successfully passed + // If this line of code is commented out, this test case will be successfully passed assertThat(o.getItems().size()).isEqualTo(0); -// org.junit.ComparisonFailure: -// Expected :1 -// Actual :2 + // org.junit.ComparisonFailure: Expected :1 Actual :2 assertThat(g.getItems().size()).isEqualTo(1); assertThat(g.getItems().get(0).getName()).isEqualTo("user");