Skip to content

Refactored AssertJ assertions into more readable ones #2746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
*
* @author Oliver Gierke
* @author Jens Schauder
* @author Krzysztof Krason
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = Config.class)
Expand Down Expand Up @@ -192,7 +193,6 @@ void findsDeletedRevisions() {

assertThat(revisions).hasSize(2);
assertThat(revisions.getLatestRevision().getEntity()) //
.isNotNull() //
.extracting(c -> c.name, c -> c.code) //
.containsExactly(null, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package org.springframework.data.jpa.convert.threeten;

import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.assertj.core.api.Assumptions.*;
import static org.springframework.data.jpa.support.EntityManagerTestUtils.*;

import java.time.Instant;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class Jsr310JpaConvertersIntegrationTests extends AbstractAttributeConver
@Test // DATAJPA-650, DATAJPA-1631
void usesJsr310JpaConverters() {

assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();

DateTimeSample sample = new DateTimeSample();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.assertj.core.api.Assertions.*;

import java.time.LocalDateTime;
import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -42,6 +41,7 @@
*
* @author Oliver Gierke
* @author Jens Schauder
* @author Krzysztof Krason
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:auditing/auditing-entity-listener.xml")
Expand All @@ -58,14 +58,14 @@ public class AuditingEntityListenerTests {

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

assertThat(auditable.getCreatedDate().isPresent()).isTrue();
assertThat(auditable.getLastModifiedDate().isPresent()).isTrue();
assertThat(auditable.getCreatedDate()).isPresent();
assertThat(auditable.getLastModifiedDate()).isPresent();
}

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

assertThat(auditable.getCreatedBy()).isEqualTo(Optional.of(user));
assertThat(auditable.getLastModifiedBy()).isEqualTo(Optional.of(user));
assertThat(auditable.getCreatedBy()).contains(user);
assertThat(auditable.getLastModifiedBy()).contains(user);
}

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
*
* @author Oliver Gierke
* @author Jens Schauder
* @author Krzysztof Krason
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration
Expand Down Expand Up @@ -75,7 +76,7 @@ public Void doInTransaction(TransactionStatus status) {
ProxyIdAccessor accessor = PersistenceProvider.fromEntityManager(em);

assertThat(accessor.shouldUseAccessorFor(product)).isTrue();
assertThat(accessor.getIdentifierFrom(product).toString()).isEqualTo((Object) product.getId().toString());
assertThat(accessor.getIdentifierFrom(product)).hasToString(product.getId().toString());

return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* @author Jens Schauder
* @author Moritz Becker
* @author Andrey Kovalev
* @author Krzysztof Krason
*/
@ContextConfiguration(value = "classpath:eclipselink.xml")
@Disabled("hsqldb seems to hang on this test class without leaving a surefire report")
Expand Down Expand Up @@ -66,8 +67,7 @@ void queryProvidesCorrectNumberOfParametersForNativeQuery() {

Query query = em.createNativeQuery("select 1 from User where firstname=? and lastname=?");
assertThat(query.getParameters()).describedAs(
"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")
.hasSize(0);
"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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.jpa.repository;

import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assumptions.*;
import static org.springframework.data.jpa.support.EntityManagerTestUtils.*;

import jakarta.persistence.EntityManager;
Expand All @@ -29,7 +30,6 @@
import java.util.List;

import org.assertj.core.api.SoftAssertions;
import org.junit.Assume;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand All @@ -55,6 +55,7 @@
* @author Jocelyn Ntakpe
* @author Christoph Strobl
* @author Jens Schauder
* @author Krzysztof Krason
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:config/namespace-autoconfig-context.xml")
Expand Down Expand Up @@ -96,22 +97,22 @@ void setup() {
@Test // DATAJPA-612
void shouldRespectConfiguredJpaEntityGraph() {

Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();

em.flush();
em.clear();

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

assertThat(result.size()).isEqualTo(3);
assertThat(result).hasSize(3);
assertThat(util.isLoaded(result.get(0), "roles")).isTrue();
assertThat(result.get(0)).isEqualTo(tom);
}

@Test // DATAJPA-689
void shouldRespectConfiguredJpaEntityGraphInFindOne() {

Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();

em.flush();
em.clear();
Expand All @@ -127,7 +128,7 @@ void shouldRespectConfiguredJpaEntityGraphInFindOne() {
@Test // DATAJPA-696
void shouldRespectInferFetchGraphFromMethodName() {

Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();

em.flush();
em.clear();
Expand All @@ -143,7 +144,7 @@ void shouldRespectInferFetchGraphFromMethodName() {
@Test // DATAJPA-696
void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() {

Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();

em.flush();
em.clear();
Expand All @@ -169,23 +170,23 @@ void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() {
@Test // DATAJPA-790, DATAJPA-1087
void shouldRespectConfiguredJpaEntityGraphWithPaginationAndQueryDslPredicates() {

Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();

em.flush();
em.clear();

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

assertThat(result.size()).isEqualTo(3);
assertThat(result).hasSize(3);
assertThat(util.isLoaded(result.get(0), "roles")).isTrue();
assertThat(result.get(0)).isEqualTo(tom);
}

@Test // DATAJPA-1207
void shouldRespectConfiguredJpaEntityGraphWithPaginationAndSpecification() {

Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();

em.flush();
em.clear();
Expand All @@ -197,15 +198,15 @@ void shouldRespectConfiguredJpaEntityGraphWithPaginationAndSpecification() {

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

assertThat(result.size()).isEqualTo(3);
assertThat(result).hasSize(3);
assertThat(util.isLoaded(result.get(0), "roles")).isTrue();
assertThat(result.get(0)).isEqualTo(tom);
}

@Test // DATAJPA-1041
void shouldRespectNamedEntitySubGraph() {

Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();

em.flush();
em.clear();
Expand All @@ -230,7 +231,7 @@ void shouldRespectNamedEntitySubGraph() {
@Test // DATAJPA-1041
void shouldRespectMultipleSubGraphForSameAttributeWithDynamicFetchGraph() {

Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();

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

Assume.assumeTrue(currentEntityManagerIsAJpa21EntityManager(em));
assumeThat(currentEntityManagerIsAJpa21EntityManager(em)).isTrue();
em.flush();
em.clear();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
*
* @author Thomas Darimont
* @author Jens Schauder
* @author Krzysztof Krason
*/
@Transactional
@ExtendWith(SpringExtension.class)
Expand All @@ -66,8 +67,8 @@ void supportForExpressionBasedQueryMethods() {
List<ConcreteType1> concretes1 = concreteRepository1.findAllByAttribute1("foo");
List<ConcreteType2> concretes2 = concreteRepository2.findAllByAttribute1("foo");

assertThat(concretes1.size()).isEqualTo(1);
assertThat(concretes2.size()).isEqualTo(1);
assertThat(concretes1).hasSize(1);
assertThat(concretes2).hasSize(1);
}

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

assertThat(page.getNumberOfElements()).isEqualTo(1);
assertThat(page.getNumberOfElements()).isOne();
}

@Test // DATAJPA-1535
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
*
* @author Oliver Gierke
* @author Jens Schauder
* @author Krzysztof Krason
*/
@ContextConfiguration("classpath:openjpa.xml")
class OpenJpaNamespaceUserRepositoryTests extends NamespaceUserRepositoryTests {
Expand Down Expand Up @@ -78,7 +79,7 @@ void queryUsingIn() {
query.setParameter(parameter, Arrays.asList(1, 2));

List<User> resultList = query.getResultList();
assertThat(resultList.size()).isEqualTo(2);
assertThat(resultList).hasSize(2);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

/**
* @author Jens Schauder
* @author Krzysztof Krason
*/
@Transactional
@ExtendWith(SpringExtension.class)
Expand Down Expand Up @@ -76,11 +77,11 @@ public Predicate toPredicate(Root<Parent> root, CriteriaQuery<?> query, Criteria

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

assertThat(content.size()).isEqualTo(3);
assertThat(content).hasSize(3);
assertThat(page.getSize()).isEqualTo(5);
assertThat(page.getNumber()).isEqualTo(0);
assertThat(page.getNumber()).isZero();
assertThat(page.getTotalElements()).isEqualTo(3L);
assertThat(page.getTotalPages()).isEqualTo(1);
assertThat(page.getTotalPages()).isOne();
}

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

// according to the initial setup there should be
// 3 parents which children collection is not empty
assertThat(content.size()).isEqualTo(3);
assertThat(content).hasSize(3);
assertThat(page.getSize()).isEqualTo(5);
assertThat(page.getNumber()).isEqualTo(0);
assertThat(page.getNumber()).isZero();

// we get here wrong total elements number since
// count query doesn't take into account the distinct marker of query
assertThat(page.getTotalElements()).isEqualTo(3L);
assertThat(page.getTotalPages()).isEqualTo(1);
assertThat(page.getTotalPages()).isOne();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
/**
* @author Thomas Darimont
* @author Jens Schauder
* @author Krzysztof Krason
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SampleConfig.class)
Expand All @@ -62,7 +63,7 @@ void adjustedWellKnownPagedFindAllMethodShouldReturnOnlyTheUserWithFirstnameOliv

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

assertThat(page.getNumberOfElements()).isEqualTo(1);
assertThat(page.getNumberOfElements()).isOne();
assertThat(page.getContent().get(0).getFirstname()).isEqualTo("Oliver");
}

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

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

assertThat(result.isEmpty()).isTrue();
assertThat(result).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
* @author Mark Paluch
* @author Jens Schauder
* @author Ernst-Jan van der Laan
* @author Krzysztof Krason
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = SampleConfig.class)
Expand Down Expand Up @@ -127,7 +128,7 @@ void shouldSupportFindAllWithPageableAndEntityWithIdClass() {
Page<IdClassExampleEmployee> page = employeeRepositoryWithIdClass.findAll(PageRequest.of(0, 1));

assertThat(page).isNotNull();
assertThat(page.getTotalElements()).isEqualTo(1L);
assertThat(page.getTotalElements()).isOne();
}

@Test // DATAJPA-2414
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
*
* @author Mark Paluch
* @author Jens Schauder
* @author Krzysztof Krason
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = RepositoryWithIdClassKeyTests.TestConfig.class)
Expand Down Expand Up @@ -72,7 +73,7 @@ void shouldSaveAndLoadEntitiesWithDerivedIdentities() {
.findById(new ItemSiteId(new ItemId(item.getId(), item.getManufacturerId()), site.getId()));

assertThat(loaded).isNotNull();
assertThat(loaded.isPresent()).isTrue();
assertThat(loaded).isPresent();
}

@Configuration
Expand Down
Loading