Skip to content

Commit dfc3a27

Browse files
committed
Refactored AssertJ assertions into more readable ones
1 parent e737efd commit dfc3a27

File tree

40 files changed

+189
-167
lines changed

40 files changed

+189
-167
lines changed

spring-data-envers/src/test/java/org/springframework/data/envers/repository/support/RepositoryIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
*
4848
* @author Oliver Gierke
4949
* @author Jens Schauder
50+
* @author Krzysztof Krason
5051
*/
5152
@ExtendWith(SpringExtension.class)
5253
@ContextConfiguration(classes = Config.class)
@@ -192,7 +193,6 @@ void findsDeletedRevisions() {
192193

193194
assertThat(revisions).hasSize(2);
194195
assertThat(revisions.getLatestRevision().getEntity()) //
195-
.isNotNull() //
196196
.extracting(c -> c.name, c -> c.code) //
197197
.containsExactly(null, null);
198198
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/convert/threeten/Jsr310JpaConvertersIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.springframework.data.jpa.convert.threeten;
1717

1818
import static org.assertj.core.api.Assertions.*;
19-
import static org.junit.Assume.*;
19+
import static org.assertj.core.api.Assumptions.*;
2020
import static org.springframework.data.jpa.support.EntityManagerTestUtils.*;
2121

2222
import java.time.Instant;
@@ -51,7 +51,7 @@ public class Jsr310JpaConvertersIntegrationTests extends AbstractAttributeConver
5151
@Test // DATAJPA-650, DATAJPA-1631
5252
void usesJsr310JpaConverters() {
5353

54-
assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
54+
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
5555

5656
DateTimeSample sample = new DateTimeSample();
5757

spring-data-jpa/src/test/java/org/springframework/data/jpa/domain/support/AuditingEntityListenerTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.assertj.core.api.Assertions.*;
1919

2020
import java.time.LocalDateTime;
21-
import java.util.Optional;
2221

2322
import org.junit.jupiter.api.BeforeEach;
2423
import org.junit.jupiter.api.Test;
@@ -42,6 +41,7 @@
4241
*
4342
* @author Oliver Gierke
4443
* @author Jens Schauder
44+
* @author Krzysztof Krason
4545
*/
4646
@ExtendWith(SpringExtension.class)
4747
@ContextConfiguration("classpath:auditing/auditing-entity-listener.xml")
@@ -58,14 +58,14 @@ public class AuditingEntityListenerTests {
5858

5959
private static void assertDatesSet(Auditable<?, ?, LocalDateTime> auditable) {
6060

61-
assertThat(auditable.getCreatedDate().isPresent()).isTrue();
62-
assertThat(auditable.getLastModifiedDate().isPresent()).isTrue();
61+
assertThat(auditable.getCreatedDate()).isPresent();
62+
assertThat(auditable.getLastModifiedDate()).isPresent();
6363
}
6464

6565
private static void assertUserIsAuditor(AuditableUser user, Auditable<AuditableUser, ?, LocalDateTime> auditable) {
6666

67-
assertThat(auditable.getCreatedBy()).isEqualTo(Optional.of(user));
68-
assertThat(auditable.getLastModifiedBy()).isEqualTo(Optional.of(user));
67+
assertThat(auditable.getCreatedBy()).contains(user);
68+
assertThat(auditable.getLastModifiedBy()).contains(user);
6969
}
7070

7171
@BeforeEach

spring-data-jpa/src/test/java/org/springframework/data/jpa/provider/PersistenceProviderIntegrationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*
4646
* @author Oliver Gierke
4747
* @author Jens Schauder
48+
* @author Krzysztof Krason
4849
*/
4950
@ExtendWith(SpringExtension.class)
5051
@ContextConfiguration
@@ -75,7 +76,7 @@ public Void doInTransaction(TransactionStatus status) {
7576
ProxyIdAccessor accessor = PersistenceProvider.fromEntityManager(em);
7677

7778
assertThat(accessor.shouldUseAccessorFor(product)).isTrue();
78-
assertThat(accessor.getIdentifierFrom(product).toString()).isEqualTo((Object) product.getId().toString());
79+
assertThat(accessor.getIdentifierFrom(product)).hasToString(product.getId().toString());
7980

8081
return null;
8182
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/EclipseLinkNamespaceUserRepositoryTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* @author Jens Schauder
3434
* @author Moritz Becker
3535
* @author Andrey Kovalev
36+
* @author Krzysztof Krason
3637
*/
3738
@ContextConfiguration(value = "classpath:eclipselink.xml")
3839
@Disabled("hsqldb seems to hang on this test class without leaving a surefire report")
@@ -66,8 +67,7 @@ void queryProvidesCorrectNumberOfParametersForNativeQuery() {
6667

6768
Query query = em.createNativeQuery("select 1 from User where firstname=? and lastname=?");
6869
assertThat(query.getParameters()).describedAs(
69-
"Due to a bug eclipse has size 0; If this is no longer the case the special code path triggered in NamedOrIndexedQueryParameterSetter.registerExcessParameters can be removed")
70-
.hasSize(0);
70+
"Due to a bug eclipse has size 0; If this is no longer the case the special code path triggered in NamedOrIndexedQueryParameterSetter.registerExcessParameters can be removed").isEmpty();
7171
}
7272

7373
/**

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/EntityGraphRepositoryMethodsIntegrationTests.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.data.jpa.repository;
1717

1818
import static org.assertj.core.api.Assertions.*;
19+
import static org.assertj.core.api.Assumptions.*;
1920
import static org.springframework.data.jpa.support.EntityManagerTestUtils.*;
2021

2122
import jakarta.persistence.EntityManager;
@@ -29,7 +30,6 @@
2930
import java.util.List;
3031

3132
import org.assertj.core.api.SoftAssertions;
32-
import org.junit.Assume;
3333
import org.junit.jupiter.api.BeforeEach;
3434
import org.junit.jupiter.api.Disabled;
3535
import org.junit.jupiter.api.Test;
@@ -55,6 +55,7 @@
5555
* @author Jocelyn Ntakpe
5656
* @author Christoph Strobl
5757
* @author Jens Schauder
58+
* @author Krzysztof Krason
5859
*/
5960
@ExtendWith(SpringExtension.class)
6061
@ContextConfiguration("classpath:config/namespace-autoconfig-context.xml")
@@ -96,22 +97,22 @@ void setup() {
9697
@Test // DATAJPA-612
9798
void shouldRespectConfiguredJpaEntityGraph() {
9899

99-
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
100+
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
100101

101102
em.flush();
102103
em.clear();
103104

104105
List<User> result = repository.findAll();
105106

106-
assertThat(result.size()).isEqualTo(3);
107+
assertThat(result).hasSize(3);
107108
assertThat(util.isLoaded(result.get(0), "roles")).isTrue();
108109
assertThat(result.get(0)).isEqualTo(tom);
109110
}
110111

111112
@Test // DATAJPA-689
112113
void shouldRespectConfiguredJpaEntityGraphInFindOne() {
113114

114-
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
115+
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
115116

116117
em.flush();
117118
em.clear();
@@ -127,7 +128,7 @@ void shouldRespectConfiguredJpaEntityGraphInFindOne() {
127128
@Test // DATAJPA-696
128129
void shouldRespectInferFetchGraphFromMethodName() {
129130

130-
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
131+
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
131132

132133
em.flush();
133134
em.clear();
@@ -143,7 +144,7 @@ void shouldRespectInferFetchGraphFromMethodName() {
143144
@Test // DATAJPA-696
144145
void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() {
145146

146-
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
147+
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
147148

148149
em.flush();
149150
em.clear();
@@ -169,23 +170,23 @@ void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() {
169170
@Test // DATAJPA-790, DATAJPA-1087
170171
void shouldRespectConfiguredJpaEntityGraphWithPaginationAndQueryDslPredicates() {
171172

172-
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
173+
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
173174

174175
em.flush();
175176
em.clear();
176177

177178
Page<User> page = repository.findAll(QUser.user.firstname.isNotNull(), PageRequest.of(0, 100));
178179
List<User> result = page.getContent();
179180

180-
assertThat(result.size()).isEqualTo(3);
181+
assertThat(result).hasSize(3);
181182
assertThat(util.isLoaded(result.get(0), "roles")).isTrue();
182183
assertThat(result.get(0)).isEqualTo(tom);
183184
}
184185

185186
@Test // DATAJPA-1207
186187
void shouldRespectConfiguredJpaEntityGraphWithPaginationAndSpecification() {
187188

188-
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
189+
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
189190

190191
em.flush();
191192
em.clear();
@@ -197,15 +198,15 @@ void shouldRespectConfiguredJpaEntityGraphWithPaginationAndSpecification() {
197198

198199
List<User> result = page.getContent();
199200

200-
assertThat(result.size()).isEqualTo(3);
201+
assertThat(result).hasSize(3);
201202
assertThat(util.isLoaded(result.get(0), "roles")).isTrue();
202203
assertThat(result.get(0)).isEqualTo(tom);
203204
}
204205

205206
@Test // DATAJPA-1041
206207
void shouldRespectNamedEntitySubGraph() {
207208

208-
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
209+
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
209210

210211
em.flush();
211212
em.clear();
@@ -230,7 +231,7 @@ void shouldRespectNamedEntitySubGraph() {
230231
@Test // DATAJPA-1041
231232
void shouldRespectMultipleSubGraphForSameAttributeWithDynamicFetchGraph() {
232233

233-
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
234+
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
234235

235236
em.flush();
236237
em.clear();
@@ -256,7 +257,7 @@ void shouldRespectMultipleSubGraphForSameAttributeWithDynamicFetchGraph() {
256257
@Disabled // likely broken due to the fixes made for HHH-15391
257258
void shouldCreateDynamicGraphWithMultipleLevelsOfSubgraphs() {
258259

259-
Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
260+
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
260261
em.flush();
261262
em.clear();
262263

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/MappedTypeRepositoryIntegrationTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*
4747
* @author Thomas Darimont
4848
* @author Jens Schauder
49+
* @author Krzysztof Krason
4950
*/
5051
@Transactional
5152
@ExtendWith(SpringExtension.class)
@@ -66,8 +67,8 @@ void supportForExpressionBasedQueryMethods() {
6667
List<ConcreteType1> concretes1 = concreteRepository1.findAllByAttribute1("foo");
6768
List<ConcreteType2> concretes2 = concreteRepository2.findAllByAttribute1("foo");
6869

69-
assertThat(concretes1.size()).isEqualTo(1);
70-
assertThat(concretes2.size()).isEqualTo(1);
70+
assertThat(concretes1).hasSize(1);
71+
assertThat(concretes2).hasSize(1);
7172
}
7273

7374
@Test // DATAJPA-424
@@ -79,7 +80,7 @@ void supportForPaginationCustomQueryMethodsWithEntityExpression() {
7980
Page<ConcreteType2> page = concreteRepository2.findByAttribute1Custom("foo",
8081
PageRequest.of(0, 10, Sort.Direction.DESC, "attribute1"));
8182

82-
assertThat(page.getNumberOfElements()).isEqualTo(1);
83+
assertThat(page.getNumberOfElements()).isOne();
8384
}
8485

8586
@Test // DATAJPA-1535

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/OpenJpaNamespaceUserRepositoryTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
*
4141
* @author Oliver Gierke
4242
* @author Jens Schauder
43+
* @author Krzysztof Krason
4344
*/
4445
@ContextConfiguration("classpath:openjpa.xml")
4546
class OpenJpaNamespaceUserRepositoryTests extends NamespaceUserRepositoryTests {
@@ -78,7 +79,7 @@ void queryUsingIn() {
7879
query.setParameter(parameter, Arrays.asList(1, 2));
7980

8081
List<User> resultList = query.getResultList();
81-
assertThat(resultList.size()).isEqualTo(2);
82+
assertThat(resultList).hasSize(2);
8283
}
8384

8485
/**

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/ParentRepositoryIntegrationTests.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
/**
4646
* @author Jens Schauder
47+
* @author Krzysztof Krason
4748
*/
4849
@Transactional
4950
@ExtendWith(SpringExtension.class)
@@ -76,11 +77,11 @@ public Predicate toPredicate(Root<Parent> root, CriteriaQuery<?> query, Criteria
7677

7778
List<Parent> content = page.getContent();
7879

79-
assertThat(content.size()).isEqualTo(3);
80+
assertThat(content).hasSize(3);
8081
assertThat(page.getSize()).isEqualTo(5);
81-
assertThat(page.getNumber()).isEqualTo(0);
82+
assertThat(page.getNumber()).isZero();
8283
assertThat(page.getTotalElements()).isEqualTo(3L);
83-
assertThat(page.getTotalPages()).isEqualTo(1);
84+
assertThat(page.getTotalPages()).isOne();
8485
}
8586

8687
@Test // DATAJPA-287
@@ -99,13 +100,13 @@ public Predicate toPredicate(Root<Parent> root, CriteriaQuery<?> query, Criteria
99100

100101
// according to the initial setup there should be
101102
// 3 parents which children collection is not empty
102-
assertThat(content.size()).isEqualTo(3);
103+
assertThat(content).hasSize(3);
103104
assertThat(page.getSize()).isEqualTo(5);
104-
assertThat(page.getNumber()).isEqualTo(0);
105+
assertThat(page.getNumber()).isZero();
105106

106107
// we get here wrong total elements number since
107108
// count query doesn't take into account the distinct marker of query
108109
assertThat(page.getTotalElements()).isEqualTo(3L);
109-
assertThat(page.getTotalPages()).isEqualTo(1);
110+
assertThat(page.getTotalPages()).isOne();
110111
}
111112
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/RedeclaringRepositoryMethodsTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
/**
3737
* @author Thomas Darimont
3838
* @author Jens Schauder
39+
* @author Krzysztof Krason
3940
*/
4041
@ExtendWith(SpringExtension.class)
4142
@ContextConfiguration(classes = SampleConfig.class)
@@ -62,7 +63,7 @@ void adjustedWellKnownPagedFindAllMethodShouldReturnOnlyTheUserWithFirstnameOliv
6263

6364
Page<User> page = repository.findAll(PageRequest.of(0, 2));
6465

65-
assertThat(page.getNumberOfElements()).isEqualTo(1);
66+
assertThat(page.getNumberOfElements()).isOne();
6667
assertThat(page.getContent().get(0).getFirstname()).isEqualTo("Oliver");
6768
}
6869

@@ -74,6 +75,6 @@ void adjustedWllKnownFindAllMethodShouldReturnAnEmptyList() {
7475

7576
List<User> result = repository.findAll();
7677

77-
assertThat(result.isEmpty()).isTrue();
78+
assertThat(result).isEmpty();
7879
}
7980
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* @author Mark Paluch
5151
* @author Jens Schauder
5252
* @author Ernst-Jan van der Laan
53+
* @author Krzysztof Krason
5354
*/
5455
@ExtendWith(SpringExtension.class)
5556
@ContextConfiguration(classes = SampleConfig.class)
@@ -127,7 +128,7 @@ void shouldSupportFindAllWithPageableAndEntityWithIdClass() {
127128
Page<IdClassExampleEmployee> page = employeeRepositoryWithIdClass.findAll(PageRequest.of(0, 1));
128129

129130
assertThat(page).isNotNull();
130-
assertThat(page.getTotalElements()).isEqualTo(1L);
131+
assertThat(page.getTotalElements()).isOne();
131132
}
132133

133134
@Test // DATAJPA-2414

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/RepositoryWithIdClassKeyTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
*
4545
* @author Mark Paluch
4646
* @author Jens Schauder
47+
* @author Krzysztof Krason
4748
*/
4849
@ExtendWith(SpringExtension.class)
4950
@ContextConfiguration(classes = RepositoryWithIdClassKeyTests.TestConfig.class)
@@ -72,7 +73,7 @@ void shouldSaveAndLoadEntitiesWithDerivedIdentities() {
7273
.findById(new ItemSiteId(new ItemId(item.getId(), item.getManufacturerId()), site.getId()));
7374

7475
assertThat(loaded).isNotNull();
75-
assertThat(loaded.isPresent()).isTrue();
76+
assertThat(loaded).isPresent();
7677
}
7778

7879
@Configuration

0 commit comments

Comments
 (0)