DATAJPA-1207 - Polishing.

Migrated Hamcrest to AssertJ.
Using SoftAsserts for assertions inside loops

Original Pull Request: #229
This commit is contained in:
Jens Schauder
2017-10-19 14:42:16 +02:00
committed by Christoph Strobl
parent 063a69ea86
commit de57fc4d60

View File

@@ -15,8 +15,7 @@
*/
package org.springframework.data.jpa.repository;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.jpa.support.EntityManagerTestUtils.*;
import java.util.List;
@@ -29,6 +28,7 @@ import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.assertj.core.api.SoftAssertions;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
@@ -101,9 +101,9 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
List<User> result = repository.findAll();
assertThat(result.size(), is(3));
assertThat(util.isLoaded(result.get(0), "roles"), is(true));
assertThat(result.get(0), is(tom));
assertThat(result.size()).isEqualTo(3);
assertThat(util.isLoaded(result.get(0), "roles")).isTrue();
assertThat(result.get(0)).isEqualTo(tom);
}
@Test // DATAJPA-689
@@ -115,9 +115,10 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
User user = repository.findById(tom.getId()).get();
assertThat(user, is(notNullValue()));
assertThat("colleages should be fetched with 'user.detail' fetchgraph", util.isLoaded(user, "colleagues"),
is(true));
assertThat(user).isNotNull();
assertThat(util.isLoaded(user, "colleagues")) //
.describedAs("colleages should be fetched with 'user.detail' fetchgraph") //
.isTrue();
}
@Test // DATAJPA-696
@@ -129,9 +130,10 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
User user = repository.getOneWithDefinedEntityGraphById(tom.getId());
assertThat(user, is(notNullValue()));
assertThat("colleages should be fetched with 'user.detail' fetchgraph", util.isLoaded(user, "colleagues"),
is(true));
assertThat(user).isNotNull();
assertThat(util.isLoaded(user, "colleagues")) //
.describedAs("colleages should be fetched with 'user.detail' fetchgraph") //
.isTrue();
}
@Test // DATAJPA-696
@@ -143,15 +145,20 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
User user = repository.getOneWithAttributeNamesById(tom.getId());
assertThat(user, is(notNullValue()));
assertThat(user).isNotNull();
assertThat("colleages should be fetched with 'user.detail' fetchgraph", util.isLoaded(user, "colleagues"),
is(true));
assertThat(util.isLoaded(user, "colleagues"), is(true));
assertThat(util.isLoaded(user, "colleagues")) //
.describedAs("colleages should be fetched with 'user.detail' fetchgraph") //
.isTrue();
assertThat(util.isLoaded(user, "colleagues")).isTrue();
SoftAssertions softly = new SoftAssertions();
for (User colleague : user.getColleagues()) {
assertThat(util.isLoaded(colleague, "roles"), is(true));
softly.assertThat(util.isLoaded(colleague, "roles")).isTrue();
}
softly.assertAll();
}
@Test // DATAJPA-790, DATAJPA-1087
@@ -164,9 +171,9 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
Page<User> page = repository.findAll(QUser.user.firstname.isNotNull(), PageRequest.of(0, 100));
List<User> result = page.getContent();
assertThat(result.size(), is(3));
assertThat(util.isLoaded(result.get(0).getRoles()), is(true));
assertThat(result.get(0), is(tom));
assertThat(result.size()).isEqualTo(3);
assertThat(util.isLoaded(result.get(0).getRoles())).isTrue();
assertThat(result.get(0)).isEqualTo(tom);
}
@Test // DATAJPA-1207
@@ -183,9 +190,9 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
List<User> result = page.getContent();
assertThat(result.size(), is(3));
assertThat(util.isLoaded(result.get(0).getRoles()), is(true));
assertThat(result.get(0), is(tom));
assertThat(result.size()).isEqualTo(3);
assertThat(util.isLoaded(result.get(0).getRoles())).isTrue();
assertThat(result.get(0)).isEqualTo(tom);
}
@Test // DATAJPA-1041
@@ -198,15 +205,19 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
User user = repository.findOneWithMultipleSubGraphsUsingNamedEntityGraphById(tom.getId());
assertThat(user, is(notNullValue()));
assertThat(user).isNotNull();
assertThat("colleagues on root should have been fetched by named 'User.colleagues' subgraph declaration",
util.isLoaded(user, "colleagues"), is(true));
SoftAssertions softly = new SoftAssertions();
softly.assertThat(util.isLoaded(user, "colleagues")) //
.describedAs("colleagues on root should have been fetched by named 'User.colleagues' subgraph declaration") //
.isTrue();
for (User colleague : user.getColleagues()) {
assertThat(util.isLoaded(colleague, "colleagues"), is(true));
assertThat(util.isLoaded(colleague, "roles"), is(true));
softly.assertThat(util.isLoaded(colleague, "colleagues")).isTrue();
softly.assertThat(util.isLoaded(colleague, "roles")).isTrue();
}
softly.assertAll();
}
@Test // DATAJPA-1041
@@ -219,15 +230,19 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
User user = repository.findOneWithMultipleSubGraphsById(tom.getId());
assertThat(user, is(notNullValue()));
assertThat(user).isNotNull();
assertThat("colleagues on root should have been fetched by dynamic subgraph declaration",
util.isLoaded(user, "colleagues"), is(true));
SoftAssertions softly = new SoftAssertions();
softly.assertThat(util.isLoaded(user, "colleagues")) //
.describedAs("colleagues on root should have been fetched by dynamic subgraph declaration") //
.isTrue();
for (User colleague : user.getColleagues()) {
assertThat(util.isLoaded(colleague, "colleagues"), is(true));
assertThat(util.isLoaded(colleague, "roles"), is(true));
softly.assertThat(util.isLoaded(colleague, "colleagues")).isTrue();
softly.assertThat(util.isLoaded(colleague, "roles")).isTrue();
}
softly.assertAll();
}
@Test // DATAJPA-1041, DATAJPA-1075
@@ -239,21 +254,26 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
User user = repository.findOneWithDeepGraphById(tom.getId());
assertThat(user, is(notNullValue()));
assertThat("Colleagues on root should have been fetched by dynamic subgraph declaration",
Persistence.getPersistenceUtil().isLoaded(user, "colleagues"), is(true));
assertThat(user).isNotNull();
SoftAssertions softly = new SoftAssertions();
softly.assertThat(Persistence.getPersistenceUtil().isLoaded(user, "colleagues")) //
.describedAs("Colleagues on root should have been fetched by dynamic subgraph declaration") //
.isTrue();
for (User colleague : user.getColleagues()) {
assertThat(Persistence.getPersistenceUtil().isLoaded(colleague, "colleagues"), is(true));
assertThat(Persistence.getPersistenceUtil().isLoaded(colleague, "roles"), is(true));
softly.assertThat(Persistence.getPersistenceUtil().isLoaded(colleague, "colleagues")).isTrue();
softly.assertThat(Persistence.getPersistenceUtil().isLoaded(colleague, "roles")).isTrue();
for (User colleagueOfColleague : colleague.getColleagues()) {
assertThat(Persistence.getPersistenceUtil().isLoaded(colleagueOfColleague, "roles"), is(true));
assertThat(Persistence.getPersistenceUtil().isLoaded(colleagueOfColleague, "colleagues"), is(false));
softly.assertThat(Persistence.getPersistenceUtil().isLoaded(colleagueOfColleague, "roles")).isTrue();
softly.assertThat(Persistence.getPersistenceUtil().isLoaded(colleagueOfColleague, "colleagues")).isFalse();
}
}
softly.assertAll();
}
private Predicate firstNameIsNotNull(Root<User> root, CriteriaQuery<?> __, CriteriaBuilder criteriaBuilder) {