Skip to content

Commit bb84b92

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-1685 - Polishing.
Migrate assertions to AssertJ. Fix Javadoc. Original pull request: spring-projects#460.
1 parent af85b46 commit bb84b92

File tree

3 files changed

+35
-77
lines changed

3 files changed

+35
-77
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepository.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
/**
4242
* Repository base implementation for Mongo.
43-
*
43+
*
4444
* @author Oliver Gierke
4545
* @author Christoph Strobl
4646
* @author Thomas Darimont
@@ -53,7 +53,7 @@ public class SimpleMongoRepository<T, ID> implements MongoRepository<T, ID> {
5353

5454
/**
5555
* Creates a new {@link SimpleMongoRepository} for the given {@link MongoEntityInformation} and {@link MongoTemplate}.
56-
*
56+
*
5757
* @param metadata must not be {@literal null}.
5858
* @param mongoOperations must not be {@literal null}.
5959
*/
@@ -109,7 +109,7 @@ public <S extends T> List<S> saveAll(Iterable<S> entities) {
109109

110110
/*
111111
* (non-Javadoc)
112-
* @see org.springframework.data.repository.CrudRepository#findOne(java.io.Serializable)
112+
* @see org.springframework.data.repository.CrudRepository#findById(java.io.Serializable)
113113
*/
114114
@Override
115115
public Optional<T> findById(ID id) {

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/ContactRepositoryIntegrationTests.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.repository;
1717

18-
import static org.hamcrest.Matchers.*;
19-
import static org.junit.Assert.*;
18+
import static org.assertj.core.api.Assertions.*;
2019

2120
import org.junit.Before;
2221
import org.junit.Test;
@@ -50,14 +49,14 @@ public void readsAndWritesContactCorrectly() {
5049
Person person = new Person("Oliver", "Gierke");
5150
Contact result = repository.save(person);
5251

53-
assertTrue(repository.findById(result.getId().toString()).get() instanceof Person);
52+
assertThat(repository.findById(result.getId().toString())).containsInstanceOf(Person.class);
5453
}
5554

5655
@Test // DATAMONGO-1245
5756
public void findsContactByTypedExample() {
5857

5958
Person person = repository.save(new Person("Oliver", "Gierke"));
6059

61-
assertThat(repository.findOne(Example.of(person)).get(), instanceOf(Person.class));
60+
assertThat(repository.findOne(Example.of(person))).containsInstanceOf(Person.class);
6261
}
6362
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/SimpleMongoRepositoryTests.java

+29-70
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
*/
1616
package org.springframework.data.mongodb.repository.support;
1717

18-
import static org.hamcrest.Matchers.*;
19-
import static org.junit.Assert.*;
18+
import static org.assertj.core.api.Assertions.*;
2019
import static org.springframework.data.domain.ExampleMatcher.*;
2120

2221
import java.util.ArrayList;
@@ -29,15 +28,14 @@
2928
import java.util.Set;
3029
import java.util.UUID;
3130

32-
import org.assertj.core.api.Assertions;
3331
import org.junit.Before;
3432
import org.junit.Test;
3533
import org.junit.runner.RunWith;
3634
import org.springframework.beans.factory.annotation.Autowired;
3735
import org.springframework.data.domain.Example;
38-
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
3936
import org.springframework.data.domain.Page;
4037
import org.springframework.data.domain.PageRequest;
38+
import org.springframework.data.domain.ExampleMatcher.*;
4139
import org.springframework.data.geo.Point;
4240
import org.springframework.data.mongodb.core.MongoTemplate;
4341
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
@@ -87,32 +85,28 @@ public void setUp() {
8785

8886
@Test
8987
public void findALlFromCustomCollectionName() {
90-
List<Person> result = repository.findAll();
91-
assertThat(result, hasSize(all.size()));
88+
assertThat(repository.findAll()).hasSize(all.size());
9289
}
9390

9491
@Test
9592
public void findOneFromCustomCollectionName() {
96-
Person result = repository.findById(dave.getId()).get();
97-
assertThat(result, is(dave));
93+
assertThat(repository.findById(dave.getId()).get()).isEqualTo(dave);
9894
}
9995

10096
@Test
10197
public void deleteFromCustomCollectionName() {
98+
10299
repository.delete(dave);
103-
List<Person> result = repository.findAll();
104100

105-
assertThat(result, hasSize(all.size() - 1));
106-
assertThat(result, not(hasItem(dave)));
101+
assertThat(repository.findAll()).hasSize(all.size() - 1).doesNotContain(dave);
107102
}
108103

109104
@Test
110105
public void deleteByIdFromCustomCollectionName() {
106+
111107
repository.deleteById(dave.getId());
112-
List<Person> result = repository.findAll();
113108

114-
assertThat(result, hasSize(all.size() - 1));
115-
assertThat(result, not(hasItem(dave)));
109+
assertThat(repository.findAll()).hasSize(all.size() - 1).doesNotContain(dave);
116110
}
117111

118112
@Test // DATAMONGO-1054
@@ -123,9 +117,7 @@ public void shouldInsertSingle() {
123117
Person person1 = new Person("First1" + randomId, "Last2" + randomId, 42);
124118
person1 = repository.insert(person1);
125119

126-
Person saved = repository.findById(person1.getId()).get();
127-
128-
assertThat(saved, is(equalTo(person1)));
120+
assertThat(repository.findById(person1.getId())).contains(person1);
129121
}
130122

131123
@Test // DATAMONGO-1054
@@ -143,7 +135,7 @@ public void shouldInsertMultipleFromList() {
143135

144136
List<Person> saved = repository.insert(persons);
145137

146-
assertThat(saved, hasSize(persons.size()));
138+
assertThat(saved).hasSize(persons.size());
147139
assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved);
148140
}
149141

@@ -162,7 +154,7 @@ public void shouldInsertMutlipleFromSet() {
162154

163155
List<Person> saved = repository.insert(persons);
164156

165-
assertThat(saved, hasSize(persons.size()));
157+
assertThat(saved).hasSize(persons.size());
166158
assertThatAllReferencePersonsWereStoredCorrectly(idToPerson, saved);
167159
}
168160

@@ -175,9 +167,8 @@ public void findByExampleShouldLookUpEntriesCorrectly() {
175167

176168
Page<Person> result = repository.findAll(Example.of(sample), PageRequest.of(0, 10));
177169

178-
assertThat(result.getContent(), hasItems(dave, oliver));
179-
assertThat(result.getContent(), hasSize(2));
180-
assertThat(result.getTotalPages(), is(1));
170+
assertThat(result.getContent()).hasSize(2).contains(dave, oliver);
171+
assertThat(result.getTotalPages()).isEqualTo(1);
181172
}
182173

183174
@Test // DATAMONGO-1464
@@ -189,8 +180,8 @@ public void findByExampleMultiplePagesShouldLookUpEntriesCorrectly() {
189180

190181
Page<Person> result = repository.findAll(Example.of(sample), PageRequest.of(0, 1));
191182

192-
assertThat(result.getContent(), hasSize(1));
193-
assertThat(result.getTotalPages(), is(2));
183+
assertThat(result.getContent()).hasSize(1);
184+
assertThat(result.getTotalPages()).isEqualTo(2);
194185
}
195186

196187
@Test // DATAMONGO-1245
@@ -200,10 +191,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectly() {
200191
sample.setLastname("Matthews");
201192
trimDomainType(sample, "id", "createdAt", "email");
202193

203-
List<Person> result = repository.findAll(Example.of(sample));
204-
205-
assertThat(result, containsInAnyOrder(dave, oliver));
206-
assertThat(result, hasSize(2));
194+
assertThat(repository.findAll(Example.of(sample))).hasSize(2).contains(dave, oliver);
207195
}
208196

209197
@Test // DATAMONGO-1245
@@ -219,10 +207,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObject()
219207
sample.setAddress(dave.getAddress());
220208
trimDomainType(sample, "id", "createdAt", "email");
221209

222-
List<Person> result = repository.findAll(Example.of(sample));
223-
224-
assertThat(result, hasItem(dave));
225-
assertThat(result, hasSize(1));
210+
assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(dave);
226211
}
227212

228213
@Test // DATAMONGO-1245
@@ -238,10 +223,7 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingPartialNestedOb
238223
sample.setAddress(new Address(null, null, "Washington"));
239224
trimDomainType(sample, "id", "createdAt", "email");
240225

241-
List<Person> result = repository.findAll(Example.of(sample));
242-
243-
assertThat(result, hasItems(dave, oliver));
244-
assertThat(result, hasSize(2));
226+
assertThat(repository.findAll(Example.of(sample))).hasSize(2).contains(dave, oliver);
245227
}
246228

247229
@Test // DATAMONGO-1245
@@ -255,9 +237,8 @@ public void findAllByExampleShouldNotFindEntriesWhenUsingPartialNestedObjectInSt
255237
trimDomainType(sample, "id", "createdAt", "email");
256238

257239
Example<Person> example = Example.of(sample, matching().withIncludeNullValues());
258-
List<Person> result = repository.findAll(example);
259240

260-
assertThat(result, empty());
241+
assertThat(repository.findAll(example)).isEmpty();
261242
}
262243

263244
@Test // DATAMONGO-1245
@@ -271,10 +252,8 @@ public void findAllByExampleShouldLookUpEntriesCorrectlyWhenUsingNestedObjectInS
271252
trimDomainType(sample, "id", "createdAt", "email");
272253

273254
Example<Person> example = Example.of(sample, matching().withIncludeNullValues());
274-
List<Person> result = repository.findAll(example);
275255

276-
assertThat(result, hasItem(dave));
277-
assertThat(result, hasSize(1));
256+
assertThat(repository.findAll(example)).hasSize(1).contains(dave);
278257
}
279258

280259
@Test // DATAMONGO-1245
@@ -285,10 +264,8 @@ public void findAllByExampleShouldRespectStringMatchMode() {
285264
trimDomainType(sample, "id", "createdAt", "email");
286265

287266
Example<Person> example = Example.of(sample, matching().withStringMatcher(StringMatcher.STARTING));
288-
List<Person> result = repository.findAll(example);
289267

290-
assertThat(result, hasItems(dave, oliver));
291-
assertThat(result, hasSize(2));
268+
assertThat(repository.findAll(example)).hasSize(2).contains(dave, oliver);
292269
}
293270

294271
@Test // DATAMONGO-1245
@@ -308,10 +285,7 @@ public void findAllByExampleShouldResolveDbRefCorrectly() {
308285
sample.setCreator(user);
309286
trimDomainType(sample, "id", "createdAt", "email");
310287

311-
List<Person> result = repository.findAll(Example.of(sample));
312-
313-
assertThat(result, hasItem(megan));
314-
assertThat(result, hasSize(1));
288+
assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(megan);
315289
}
316290

317291
@Test // DATAMONGO-1245
@@ -326,10 +300,7 @@ public void findAllByExampleShouldResolveLegacyCoordinatesCorrectly() {
326300
sample.setLocation(megan.getLocation());
327301
trimDomainType(sample, "id", "createdAt", "email");
328302

329-
List<Person> result = repository.findAll(Example.of(sample));
330-
331-
assertThat(result, hasItem(megan));
332-
assertThat(result, hasSize(1));
303+
assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(megan);
333304
}
334305

335306
@Test // DATAMONGO-1245
@@ -344,10 +315,7 @@ public void findAllByExampleShouldResolveGeoJsonCoordinatesCorrectly() {
344315
sample.setLocation(megan.getLocation());
345316
trimDomainType(sample, "id", "createdAt", "email");
346317

347-
List<Person> result = repository.findAll(Example.of(sample));
348-
349-
assertThat(result, hasItem(megan));
350-
assertThat(result, hasSize(1));
318+
assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(megan);
351319
}
352320

353321
@Test // DATAMONGO-1245
@@ -363,10 +331,7 @@ public void findAllByExampleShouldProcessInheritanceCorrectly() {
363331

364332
trimDomainType(sample, "id", "createdAt", "email");
365333

366-
List<PersonExtended> result = repository.findAll(Example.of(sample));
367-
368-
assertThat(result, hasSize(1));
369-
assertThat(result, hasItem(reference));
334+
assertThat(repository.findAll(Example.of(sample))).hasSize(1).contains(reference);
370335
}
371336

372337
@Test // DATAMONGO-1245
@@ -377,9 +342,7 @@ public void findOneByExampleShouldLookUpEntriesCorrectly() {
377342
sample.setLastname("Matthews");
378343
trimDomainType(sample, "id", "createdAt", "email");
379344

380-
Optional<Person> result = repository.findOne(Example.of(sample));
381-
382-
Assertions.assertThat(result).isPresent().contains(dave);
345+
assertThat(repository.findOne(Example.of(sample))).isPresent().contains(dave);
383346
}
384347

385348
@Test // DATAMONGO-1245
@@ -390,9 +353,7 @@ public void existsByExampleShouldLookUpEntriesCorrectly() {
390353
sample.setLastname("Matthews");
391354
trimDomainType(sample, "id", "createdAt", "email");
392355

393-
boolean result = repository.exists(Example.of(sample));
394-
395-
assertThat(result, is(true));
356+
assertThat(repository.exists(Example.of(sample))).isTrue();
396357
}
397358

398359
@Test // DATAMONGO-1245
@@ -402,16 +363,14 @@ public void countByExampleShouldLookUpEntriesCorrectly() {
402363
sample.setLastname("Matthews");
403364
trimDomainType(sample, "id", "createdAt", "email");
404365

405-
long result = repository.count(Example.of(sample));
406-
407-
assertThat(result, is(equalTo(2L)));
366+
assertThat(repository.count(Example.of(sample))).isEqualTo(2L);
408367
}
409368

410369
private void assertThatAllReferencePersonsWereStoredCorrectly(Map<String, Person> references, List<Person> saved) {
411370

412371
for (Person person : saved) {
413372
Person reference = references.get(person.getId());
414-
assertThat(person, is(equalTo(reference)));
373+
assertThat(person).isEqualTo(reference);
415374
}
416375
}
417376

0 commit comments

Comments
 (0)