Skip to content

Commit c19d457

Browse files
committed
Polishing.
Introduce JUnit extension to declare tests that dirty or provide their state. See #3817 Original pull request: #3987.
1 parent 1119a4a commit c19d457

11 files changed

+354
-136
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ExecutableFindOperationSupportTests.java

+20-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.springframework.data.mongodb.core.query.Criteria.*;
2020
import static org.springframework.data.mongodb.core.query.Query.*;
21+
import static org.springframework.data.mongodb.test.util.DirtiesStateExtension.*;
2122

2223
import lombok.AllArgsConstructor;
2324
import lombok.Data;
@@ -31,9 +32,9 @@
3132
import org.bson.BsonString;
3233
import org.bson.BsonValue;
3334
import org.bson.Document;
34-
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Disabled;
3636
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.api.TestInstance;
3738
import org.junit.jupiter.api.extension.ExtendWith;
3839

3940
import org.springframework.beans.factory.annotation.Value;
@@ -50,6 +51,7 @@
5051
import org.springframework.data.mongodb.core.mapping.Field;
5152
import org.springframework.data.mongodb.core.query.BasicQuery;
5253
import org.springframework.data.mongodb.core.query.NearQuery;
54+
import org.springframework.data.mongodb.test.util.DirtiesStateExtension;
5355
import org.springframework.data.mongodb.test.util.MongoTemplateExtension;
5456
import org.springframework.data.mongodb.test.util.MongoTestTemplate;
5557
import org.springframework.data.mongodb.test.util.Template;
@@ -60,8 +62,9 @@
6062
* @author Christoph Strobl
6163
* @author Mark Paluch
6264
*/
63-
@ExtendWith(MongoTemplateExtension.class)
64-
class ExecutableFindOperationSupportTests {
65+
@ExtendWith({ MongoTemplateExtension.class, DirtiesStateExtension.class })
66+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
67+
class ExecutableFindOperationSupportTests implements StateFunctions {
6568

6669
private static final String STAR_WARS = "star-wars";
6770
private static final String STAR_WARS_PLANETS = "star-wars-universe";
@@ -75,11 +78,13 @@ class ExecutableFindOperationSupportTests {
7578
private Planet alderan;
7679
private Planet dantooine;
7780

78-
@BeforeEach
79-
void setUp() {
80-
81+
@Override
82+
public void clear() {
8183
template.flush();
84+
}
8285

86+
@Override
87+
public void setupState() {
8388
template.indexOps(Planet.class).ensureIndex(
8489
new GeospatialIndex("coordinates").typed(GeoSpatialIndexType.GEO_2DSPHERE).named("planet-coordinate-idx"));
8590

@@ -118,6 +123,7 @@ void findAllWithProjection() {
118123
}
119124

120125
@Test // DATAMONGO-2041
126+
@DirtiesState
121127
void findAllWithProjectionOnEmbeddedType() {
122128

123129
luke.father = new Person();
@@ -364,6 +370,7 @@ void existsShouldReturnTrueIfAtLeastOneElementExistsInCollection() {
364370
}
365371

366372
@Test // DATAMONGO-1734
373+
@DirtiesState
367374
void existsShouldReturnFalseIfNoElementExistsInCollection() {
368375

369376
template.remove(new BasicQuery("{}"), STAR_WARS);
@@ -491,6 +498,7 @@ void distinctReturnsRawValuesIfReturnTypeIsBsonValue() {
491498
}
492499

493500
@Test // DATAMONGO-1761
501+
@DirtiesState
494502
void distinctReturnsValuesMappedToTheirJavaTypeEvenWhenNotExplicitlyDefinedByTheDomainType() {
495503

496504
template.save(new Document("darth", "vader"), STAR_WARS);
@@ -499,6 +507,7 @@ void distinctReturnsValuesMappedToTheirJavaTypeEvenWhenNotExplicitlyDefinedByThe
499507
}
500508

501509
@Test // DATAMONGO-1761
510+
@DirtiesState
502511
void distinctReturnsMappedDomainTypeForProjections() {
503512

504513
luke.father = new Person();
@@ -511,6 +520,7 @@ void distinctReturnsMappedDomainTypeForProjections() {
511520
}
512521

513522
@Test // DATAMONGO-1761
523+
@DirtiesState
514524
void distinctAlllowsQueryUsingObjectSourceType() {
515525

516526
luke.father = new Person();
@@ -523,6 +533,7 @@ void distinctAlllowsQueryUsingObjectSourceType() {
523533
}
524534

525535
@Test // DATAMONGO-1761
536+
@DirtiesState
526537
void distinctReturnsMappedDomainTypeExtractedFromPropertyWhenNoExplicitTypePresent() {
527538

528539
luke.father = new Person();
@@ -570,6 +581,7 @@ void projectionOnDbRef() {
570581

571582
@Test // GH-2860
572583
@Disabled("GH-3913")
584+
@DirtiesState
573585
void propertyProjectionOnDbRef() {
574586

575587
WithRefs source = new WithRefs();
@@ -586,6 +598,7 @@ void propertyProjectionOnDbRef() {
586598
}
587599

588600
@Test // GH-2860
601+
@DirtiesState
589602
void projectionOnDocRef() {
590603

591604
WithRefs source = new WithRefs();
@@ -602,6 +615,7 @@ void projectionOnDocRef() {
602615
}
603616

604617
@Test // GH-2860
618+
@DirtiesState
605619
void propertyProjectionOnDocRef() {
606620

607621
WithRefs source = new WithRefs();

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ExecutableUpdateOperationSupportTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ExecutableUpdateOperationSupportTests {
5858
@BeforeEach
5959
void setUp() {
6060

61-
template.flush();
61+
template.remove(Person.class).all();
6262

6363
han = new Person();
6464
han.firstname = "han";

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveFindOperationSupportTests.java

+39-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.assertj.core.api.Assertions.*;
1919
import static org.springframework.data.mongodb.core.query.Criteria.*;
2020
import static org.springframework.data.mongodb.core.query.Query.*;
21+
import static org.springframework.data.mongodb.test.util.DirtiesStateExtension.*;
2122

2223
import lombok.AllArgsConstructor;
2324
import lombok.Data;
@@ -35,9 +36,10 @@
3536
import org.bson.BsonString;
3637
import org.bson.BsonValue;
3738
import org.bson.Document;
38-
import org.junit.jupiter.api.BeforeEach;
3939
import org.junit.jupiter.api.Test;
40+
import org.junit.jupiter.api.TestInstance;
4041
import org.junit.jupiter.api.extension.ExtendWith;
42+
4143
import org.springframework.beans.factory.annotation.Value;
4244
import org.springframework.dao.IncorrectResultSizeDataAccessException;
4345
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -50,6 +52,7 @@
5052
import org.springframework.data.mongodb.core.query.Criteria;
5153
import org.springframework.data.mongodb.core.query.NearQuery;
5254
import org.springframework.data.mongodb.test.util.Client;
55+
import org.springframework.data.mongodb.test.util.DirtiesStateExtension;
5356
import org.springframework.data.mongodb.test.util.MongoClientExtension;
5457

5558
import com.mongodb.client.MongoClient;
@@ -61,8 +64,9 @@
6164
* @author Christoph Strobl
6265
* @author Juergen Zimmermann
6366
*/
64-
@ExtendWith(MongoClientExtension.class)
65-
class ReactiveFindOperationSupportTests {
67+
@ExtendWith({ MongoClientExtension.class, DirtiesStateExtension.class })
68+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
69+
class ReactiveFindOperationSupportTests implements StateFunctions {
6670

6771
private static final String STAR_WARS = "star-wars";
6872
private MongoTemplate blocking;
@@ -74,15 +78,25 @@ class ReactiveFindOperationSupportTests {
7478
private Person han;
7579
private Person luke;
7680

77-
@BeforeEach
7881
void setUp() {
79-
8082
blocking = new MongoTemplate(new SimpleMongoClientDatabaseFactory(client, "ExecutableFindOperationSupportTests"));
83+
template = new ReactiveMongoTemplate(reactiveClient, "ExecutableFindOperationSupportTests");
84+
}
85+
86+
@Override
87+
public void clear() {
88+
if (blocking == null) {
89+
setUp();
90+
}
8191
recreateCollection(STAR_WARS, false);
92+
}
8293

94+
@Override
95+
public void setupState() {
96+
if (blocking == null) {
97+
setUp();
98+
}
8399
insertObjects();
84-
85-
template = new ReactiveMongoTemplate(reactiveClient, "ExecutableFindOperationSupportTests");
86100
}
87101

88102
void insertObjects() {
@@ -244,6 +258,7 @@ void findByTooManyResults() {
244258
}
245259

246260
@Test // DATAMONGO-1719
261+
@DirtiesState
247262
void findAllNearBy() {
248263

249264
blocking.indexOps(Planet.class).ensureIndex(
@@ -264,6 +279,7 @@ void findAllNearBy() {
264279
}
265280

266281
@Test // DATAMONGO-1719
282+
@DirtiesState
267283
void findAllNearByWithCollectionAndProjection() {
268284

269285
blocking.indexOps(Planet.class).ensureIndex(
@@ -287,6 +303,7 @@ void findAllNearByWithCollectionAndProjection() {
287303
}
288304

289305
@Test // DATAMONGO-1719
306+
@DirtiesState
290307
void findAllNearByReturningGeoResultContentAsClosedInterfaceProjection() {
291308

292309
blocking.indexOps(Planet.class).ensureIndex(
@@ -310,6 +327,7 @@ void findAllNearByReturningGeoResultContentAsClosedInterfaceProjection() {
310327
}
311328

312329
@Test // DATAMONGO-1719
330+
@DirtiesState
313331
void findAllNearByReturningGeoResultContentAsOpenInterfaceProjection() {
314332

315333
blocking.indexOps(Planet.class).ensureIndex(
@@ -333,6 +351,7 @@ void findAllNearByReturningGeoResultContentAsOpenInterfaceProjection() {
333351
}
334352

335353
@Test // DATAMONGO-2080
354+
@ProvidesState
336355
void tail() throws InterruptedException {
337356

338357
recreateCollection(STAR_WARS, true);
@@ -361,6 +380,7 @@ void tail() throws InterruptedException {
361380
}
362381

363382
@Test // DATAMONGO-2080
383+
@ProvidesState
364384
void tailWithProjection() {
365385

366386
recreateCollection(STAR_WARS, true);
@@ -374,6 +394,7 @@ void tailWithProjection() {
374394
}
375395

376396
@Test // DATAMONGO-2080
397+
@ProvidesState
377398
void tailWithClosedInterfaceProjection() {
378399

379400
recreateCollection(STAR_WARS, true);
@@ -391,6 +412,7 @@ void tailWithClosedInterfaceProjection() {
391412
}
392413

393414
@Test // DATAMONGO-2080
415+
@ProvidesState
394416
void tailWithOpenInterfaceProjection() {
395417

396418
recreateCollection(STAR_WARS, true);
@@ -431,6 +453,7 @@ void existsShouldReturnTrueIfAtLeastOneElementExistsInCollection() {
431453
}
432454

433455
@Test // DATAMONGO-1719
456+
@DirtiesState
434457
void existsShouldReturnFalseIfNoElementExistsInCollection() {
435458

436459
blocking.remove(new BasicQuery("{}"), STAR_WARS);
@@ -462,6 +485,7 @@ void distinctReturnsEmptyListIfNoMatchFound() {
462485
}
463486

464487
@Test // DATAMONGO-1761
488+
@DirtiesState
465489
void distinctReturnsSimpleFieldValuesCorrectlyForCollectionHavingReturnTypeSpecifiedThatCanBeConvertedDirectlyByACodec() {
466490

467491
Person anakin = new Person();
@@ -476,6 +500,7 @@ void distinctReturnsSimpleFieldValuesCorrectlyForCollectionHavingReturnTypeSpeci
476500
}
477501

478502
@Test // DATAMONGO-1761
503+
@DirtiesState
479504
void distinctReturnsSimpleFieldValuesCorrectly() {
480505

481506
Person anakin = new Person();
@@ -504,6 +529,7 @@ void distinctReturnsSimpleFieldValuesCorrectly() {
504529
}
505530

506531
@Test // DATAMONGO-1761
532+
@DirtiesState
507533
void distinctReturnsComplexValuesCorrectly() {
508534

509535
Sith sith = new Sith();
@@ -521,6 +547,7 @@ void distinctReturnsComplexValuesCorrectly() {
521547
}
522548

523549
@Test // DATAMONGO-1761
550+
@DirtiesState
524551
void distinctReturnsComplexValuesCorrectlyHavingReturnTypeSpecified() {
525552

526553
Sith sith = new Sith();
@@ -538,6 +565,7 @@ void distinctReturnsComplexValuesCorrectlyHavingReturnTypeSpecified() {
538565
}
539566

540567
@Test // DATAMONGO-1761
568+
@DirtiesState
541569
void distinctReturnsComplexValuesCorrectlyReturnTypeDocumentSpecified() {
542570

543571
Sith sith = new Sith();
@@ -573,6 +601,7 @@ void distinctReturnsRawValuesIfReturnTypeIsBsonValue() {
573601
}
574602

575603
@Test // DATAMONGO-1761
604+
@DirtiesState
576605
void distinctReturnsValuesMappedToTheirJavaTypeEvenWhenNotExplicitlyDefinedByTheDomainType() {
577606

578607
blocking.save(new Document("darth", "vader"), STAR_WARS);
@@ -583,6 +612,7 @@ void distinctReturnsValuesMappedToTheirJavaTypeEvenWhenNotExplicitlyDefinedByThe
583612
}
584613

585614
@Test // DATAMONGO-1761
615+
@DirtiesState
586616
void distinctReturnsMappedDomainTypeForProjections() {
587617

588618
luke.father = new Person();
@@ -596,6 +626,7 @@ void distinctReturnsMappedDomainTypeForProjections() {
596626
}
597627

598628
@Test // DATAMONGO-1761
629+
@DirtiesState
599630
void distinctAlllowsQueryUsingObjectSourceType() {
600631

601632
luke.father = new Person();
@@ -609,6 +640,7 @@ void distinctAlllowsQueryUsingObjectSourceType() {
609640
}
610641

611642
@Test // DATAMONGO-1761
643+
@DirtiesState
612644
void distinctReturnsMappedDomainTypeExtractedFromPropertyWhenNoExplicitTypePresent() {
613645

614646
luke.father = new Person();

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/ReactiveMongoTemplateUpdateTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ public void aggregateUpdateWithReplaceWith() {
186186
two.author = new Author("Grace", "Hopper");
187187

188188
template.insertAll(Arrays.asList(one, two)).then().as(StepVerifier::create).verifyComplete();
189-
;
190189

191190
AggregationUpdate update = AggregationUpdate.update()
192191
.replaceWith(ReplaceWithOperation.replaceWithValueOf("author"));

0 commit comments

Comments
 (0)