Skip to content

Commit 5308e6c

Browse files
christophstroblmp911de
authored andcommitted
DATAMONGO-2390 - Polishing.
Switch touched test files to JUnit Jupiter. Original pull request: #800.
1 parent bc1c6c9 commit 5308e6c

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
import org.bson.Document;
2424
import org.bson.conversions.Bson;
25-
import org.junit.Before;
26-
import org.junit.Test;
27-
import org.junit.runner.RunWith;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.extension.ExtendWith;
2828
import org.mockito.Mock;
29-
import org.mockito.junit.MockitoJUnitRunner;
29+
import org.mockito.junit.jupiter.MockitoExtension;
3030
import org.springframework.dao.DataAccessException;
3131
import org.springframework.data.geo.Point;
3232
import org.springframework.data.mapping.MappingException;
@@ -49,7 +49,7 @@
4949
* @author Thomas Darimont
5050
* @author Christoph Strobl
5151
*/
52-
@RunWith(MockitoJUnitRunner.class)
52+
@ExtendWith(MockitoExtension.class)
5353
public abstract class MongoOperationsUnitTests {
5454

5555
@Mock CollectionCallback<Object> collectionCallback;
@@ -59,7 +59,7 @@ public abstract class MongoOperationsUnitTests {
5959
Person person;
6060
List<Person> persons;
6161

62-
@Before
62+
@BeforeEach
6363
public final void operationsSetUp() {
6464

6565
person = new Person("Oliver");

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

+21-19
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@
3737
import org.bson.Document;
3838
import org.bson.conversions.Bson;
3939
import org.bson.types.ObjectId;
40-
import org.junit.Before;
41-
import org.junit.Ignore;
42-
import org.junit.Test;
43-
import org.junit.runner.RunWith;
40+
import org.junit.jupiter.api.BeforeEach;
41+
import org.junit.jupiter.api.Disabled;
42+
import org.junit.jupiter.api.Test;
4443
import org.mockito.ArgumentCaptor;
4544
import org.mockito.ArgumentMatcher;
4645
import org.mockito.Mock;
4746
import org.mockito.Mockito;
48-
import org.mockito.junit.MockitoJUnitRunner;
47+
import org.mockito.junit.jupiter.MockitoSettings;
48+
import org.mockito.quality.Strictness;
4949
import org.springframework.beans.factory.annotation.Value;
5050
import org.springframework.context.ApplicationContext;
5151
import org.springframework.context.ApplicationListener;
@@ -122,7 +122,7 @@
122122
* @author Mark Paluch
123123
* @author Michael J. Simons
124124
*/
125-
@RunWith(MockitoJUnitRunner.class)
125+
@MockitoSettings(strictness = Strictness.LENIENT)
126126
public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
127127

128128
MongoTemplate template;
@@ -146,8 +146,8 @@ public class MongoTemplateUnitTests extends MongoOperationsUnitTests {
146146
MappingMongoConverter converter;
147147
MongoMappingContext mappingContext;
148148

149-
@Before
150-
public void setUp() {
149+
@BeforeEach
150+
public void beforeEach() {
151151

152152
when(findIterable.iterator()).thenReturn(cursor);
153153
when(factory.getDb()).thenReturn(db);
@@ -209,34 +209,36 @@ public void rejectsNullMongoClient() {
209209
.isThrownBy(() -> new MongoTemplate((com.mongodb.client.MongoClient) null, "database"));
210210
}
211211

212-
@Test(expected = IllegalArgumentException.class) // DATAMONGO-1870
213-
public void removeHandlesMongoExceptionProperly() throws Exception {
212+
@Test // DATAMONGO-1870
213+
public void removeHandlesMongoExceptionProperly() {
214214

215215
MongoTemplate template = mockOutGetDb();
216216

217-
template.remove(null, "collection");
217+
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> template.remove(null, "collection"));
218218
}
219219

220220
@Test
221-
public void defaultsConverterToMappingMongoConverter() throws Exception {
221+
public void defaultsConverterToMappingMongoConverter() {
222222
MongoTemplate template = new MongoTemplate(mongo, "database");
223223
assertThat(ReflectionTestUtils.getField(template, "mongoConverter") instanceof MappingMongoConverter).isTrue();
224224
}
225225

226-
@Test(expected = InvalidDataAccessApiUsageException.class)
226+
@Test
227227
public void rejectsNotFoundMapReduceResource() {
228228

229229
GenericApplicationContext ctx = new GenericApplicationContext();
230230
ctx.refresh();
231231
template.setApplicationContext(ctx);
232-
template.mapReduce("foo", "classpath:doesNotExist.js", "function() {}", Person.class);
232+
233+
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class)
234+
.isThrownBy(() -> template.mapReduce("foo", "classpath:doesNotExist.js", "function() {}", Person.class));
233235
}
234236

235-
@Test(expected = InvalidDataAccessApiUsageException.class) // DATAMONGO-322
237+
@Test // DATAMONGO-322
236238
public void rejectsEntityWithNullIdIfNotSupportedIdType() {
237239

238240
Object entity = new NotAutogenerateableId();
239-
template.save(entity);
241+
assertThatExceptionOfType(InvalidDataAccessApiUsageException.class).isThrownBy(() -> template.save(entity));
240242
}
241243

242244
@Test // DATAMONGO-322
@@ -466,7 +468,7 @@ public void geoNearShouldIgnoreReadPreferenceWhenNotSet() {
466468
}
467469

468470
@Test // DATAMONGO-1334
469-
@Ignore("TODO: mongo3 - a bit hard to tests with the immutable object stuff")
471+
@Disabled("TODO: mongo3 - a bit hard to tests with the immutable object stuff")
470472
public void mapReduceShouldUseZeroAsDefaultLimit() {
471473

472474
MongoCursor cursor = mock(MongoCursor.class);
@@ -1323,7 +1325,7 @@ public void findAndReplaceShouldUseDefaultCollationWhenPresent() {
13231325
assertThat(options.getValue().getCollation().getLocale()).isEqualTo("de_AT");
13241326
}
13251327

1326-
@Test // DATAMONGO-18545
1328+
@Test // DATAMONGO-1854
13271329
public void findAndReplaceShouldUseCollationEvenIfDefaultCollationIsPresent() {
13281330

13291331
template.findAndReplace(new BasicQuery("{}").collation(Collation.of("fr")), new Sith());
@@ -1822,7 +1824,7 @@ private MongoTemplate mockOutGetDb() {
18221824
@Override
18231825
protected MongoOperations getOperationsForExceptionHandling() {
18241826
MongoTemplate template = spy(this.template);
1825-
when(template.getDb()).thenThrow(new MongoException("Error!"));
1827+
lenient().when(template.getDb()).thenThrow(new MongoException("Error!"));
18261828
return template;
18271829
}
18281830

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

+13-10
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@
3535
import org.bson.Document;
3636
import org.bson.conversions.Bson;
3737
import org.bson.types.ObjectId;
38-
import org.junit.Before;
39-
import org.junit.Test;
40-
import org.junit.runner.RunWith;
38+
import org.junit.jupiter.api.BeforeEach;
39+
import org.junit.jupiter.api.Test;
40+
import org.junit.jupiter.api.extension.ExtendWith;
4141
import org.mockito.ArgumentCaptor;
4242
import org.mockito.Mock;
43-
import org.mockito.junit.MockitoJUnitRunner;
43+
import org.mockito.junit.jupiter.MockitoExtension;
44+
import org.mockito.junit.jupiter.MockitoSettings;
45+
import org.mockito.quality.Strictness;
4446
import org.reactivestreams.Publisher;
4547
import org.springframework.beans.factory.annotation.Value;
4648
import org.springframework.context.ApplicationContext;
@@ -91,7 +93,8 @@
9193
* @author Mark Paluch
9294
* @author Christoph Strobl
9395
*/
94-
@RunWith(MockitoJUnitRunner.Silent.class)
96+
@ExtendWith(MockitoExtension.class)
97+
@MockitoSettings(strictness = Strictness.LENIENT)
9598
public class ReactiveMongoTemplateUnitTests {
9699

97100
ReactiveMongoTemplate template;
@@ -114,8 +117,8 @@ public class ReactiveMongoTemplateUnitTests {
114117
MappingMongoConverter converter;
115118
MongoMappingContext mappingContext;
116119

117-
@Before
118-
public void setUp() {
120+
@BeforeEach
121+
public void beforeEach() {
119122

120123
when(factory.getExceptionTranslator()).thenReturn(exceptionTranslator);
121124
when(factory.getMongoDatabase()).thenReturn(db);
@@ -615,7 +618,7 @@ public void aggregateShouldApplyMaxTimeIfSet() {
615618
verify(aggregatePublisher).maxTime(eq(10000L), eq(TimeUnit.MILLISECONDS));
616619
}
617620

618-
@Test // DATAMONGO-18545
621+
@Test // DATAMONGO-1854
619622
public void findAndReplaceShouldUseCollationWhenPresent() {
620623

621624
template.findAndReplace(new BasicQuery("{}").collation(Collation.of("fr")), new Jedi()).subscribe();
@@ -626,7 +629,7 @@ public void findAndReplaceShouldUseCollationWhenPresent() {
626629
assertThat(options.getValue().getCollation().getLocale()).isEqualTo("fr");
627630
}
628631

629-
@Test // DATAMONGO-18545
632+
@Test // DATAMONGO-1854
630633
public void findAndReplaceShouldUseDefaultCollationWhenPresent() {
631634

632635
template.findAndReplace(new BasicQuery("{}"), new Sith()).subscribe();
@@ -637,7 +640,7 @@ public void findAndReplaceShouldUseDefaultCollationWhenPresent() {
637640
assertThat(options.getValue().getCollation().getLocale()).isEqualTo("de_AT");
638641
}
639642

640-
@Test // DATAMONGO-18545
643+
@Test // DATAMONGO-1854
641644
public void findAndReplaceShouldUseCollationEvenIfDefaultCollationIsPresent() {
642645

643646
template.findAndReplace(new BasicQuery("{}").collation(Collation.of("fr")), new MongoTemplateUnitTests.Sith())

0 commit comments

Comments
 (0)