Migrate MongoDB to JUnit 5 (except for ClassRule).

See #583.
This commit is contained in:
Mark Paluch
2021-04-28 15:37:28 +02:00
parent e565f33872
commit ead318ce8d
29 changed files with 366 additions and 455 deletions

View File

@@ -20,34 +20,32 @@ import static org.assertj.core.api.Assertions.*;
import example.springdata.mongodb.customer.Customer;
import org.bson.Document;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Meta;
import org.springframework.test.context.junit4.SpringRunner;
import com.mongodb.BasicDBObject;
import com.mongodb.client.FindIterable;
/**
* @author Christoph Strobl
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class AdvancedIntegrationTests {
@DataMongoTest
class AdvancedIntegrationTests {
@Autowired AdvancedRepository repository;
@Autowired MongoOperations operations;
Customer dave, oliver, carter;
private Customer dave, oliver, carter;
@Before
public void setUp() {
@BeforeEach
void setUp() {
repository.deleteAll();
@@ -63,7 +61,7 @@ public class AdvancedIntegrationTests {
* <strong>NOTE</strong>: Requires MongoDB v. 2.6.4+
*/
@Test
public void findByFirstnameUsingMetaAttributes() {
void findByFirstnameUsingMetaAttributes() {
// execute derived finder method just to get the comment in the profile log
repository.findByFirstname(dave.getFirstname());

View File

@@ -22,30 +22,29 @@ import example.springdata.mongodb.customer.Customer;
import java.util.Map;
import org.bson.Document;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.script.ExecutableMongoScript;
import org.springframework.data.mongodb.core.script.NamedMongoScript;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author Christoph Strobl
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ServersideScriptTests {
@DataMongoTest
class ServersideScriptTests {
@Autowired AdvancedRepository repository;
@Autowired MongoOperations operations;
@Before
public void setUp() {
@BeforeEach
void setUp() {
if (!operations.collectionExists(Customer.class)) {
operations.createCollection(Customer.class);
@@ -60,7 +59,7 @@ public class ServersideScriptTests {
* Store and call an arbitrary JavaScript function (in this case a simple echo script) via its name.
*/
@Test
public void saveAndCallScriptViaName() {
void saveAndCallScriptViaName() {
operations.scriptOps()
.register(new NamedMongoScript("echoScript", new ExecutableMongoScript("function(x) { return x; }")));
@@ -73,8 +72,8 @@ public class ServersideScriptTests {
* {@link Map#putIfAbsent(Object, Object)}
*/
@Test
@Ignore
public void complexScriptExecutionSimulatingPutIfAbsent() {
@Disabled
void complexScriptExecutionSimulatingPutIfAbsent() {
var ned = new Customer("Ned", "Stark");
ned.setId("ned-stark");

View File

@@ -15,42 +15,38 @@
*/
package example.springdata.mongodb.customer;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.data.Offset.offset;
import java.util.List;
import java.util.stream.Stream;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Metrics;
import org.springframework.data.geo.Point;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.index.GeospatialIndex;
import org.springframework.data.querydsl.QSort;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration test for {@link CustomerRepository}.
*
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryIntegrationTest {
@DataMongoTest
class CustomerRepositoryIntegrationTest {
@Autowired CustomerRepository repository;
@Autowired MongoOperations operations;
Customer dave, oliver, carter;
private Customer dave, oliver, carter;
@Before
public void setUp() {
@BeforeEach
void setUp() {
repository.deleteAll();
@@ -63,31 +59,31 @@ public class CustomerRepositoryIntegrationTest {
* Test case to show that automatically generated ids are assigned to the domain objects.
*/
@Test
public void setsIdOnSave() {
void setsIdOnSave() {
var dave = repository.save(new Customer("Dave", "Matthews"));
assertThat(dave.getId(), is(notNullValue()));
assertThat(dave.getId()).isNotNull();
}
/**
* Test case to show the usage of the Querydsl-specific {@link QSort} to define the sort order in a type-safe way.
*/
@Test
public void findCustomersUsingQuerydslSort() {
void findCustomersUsingQuerydslSort() {
var customer = QCustomer.customer;
var result = repository.findByLastname("Matthews", new QSort(customer.firstname.asc()));
assertThat(result, hasSize(2));
assertThat(result.get(0), is(dave));
assertThat(result.get(1), is(oliver));
assertThat(result).hasSize(2);
assertThat(result.get(0)).isEqualTo(dave);
assertThat(result.get(1)).isEqualTo(oliver);
}
/**
* Test case to show the usage of Java {@link Stream}.
*/
@Test
public void findCustomersAsStream() {
void findCustomersAsStream() {
try (var result = repository.findAllByCustomQueryWithStream()) {
result.forEach(System.out::println);
@@ -98,7 +94,7 @@ public class CustomerRepositoryIntegrationTest {
* Test case to show the usage of the geo-spatial APIs to lookup people within a given distance of a reference point.
*/
@Test
public void exposesGeoSpatialFunctionality() {
void exposesGeoSpatialFunctionality() {
var indexDefinition = new GeospatialIndex("address.location");
indexDefinition.getIndexOptions().put("min", -180);
@@ -115,10 +111,10 @@ public class CustomerRepositoryIntegrationTest {
var result = repository.findByAddressLocationNear(referenceLocation, oneKilometer);
assertThat(result.getContent(), hasSize(1));
assertThat(result.getContent()).hasSize(1);
var distanceToFirstStore = result.getContent().get(0).getDistance();
assertThat(distanceToFirstStore.getMetric(), is(Metrics.KILOMETERS));
assertThat(distanceToFirstStore.getValue(), closeTo(0.862, 0.001));
assertThat(distanceToFirstStore.getMetric()).isEqualTo(Metrics.KILOMETERS);
assertThat(distanceToFirstStore.getValue()).isCloseTo(0.862, offset(0.001));
}
}

View File

@@ -17,14 +17,13 @@ package example.springdata.mongodb.immutable;
import static org.assertj.core.api.Assertions.*;
import org.bson.types.ObjectId;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration test for {@link ImmutablePerson} showing features around immutable object support.
@@ -32,14 +31,13 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Mark Paluch
* @author Christoph Strobl
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ImmutableEntityIntegrationTest {
@DataMongoTest
class ImmutableEntityIntegrationTest {
@Autowired MongoOperations operations;
@Before
public void setUp() {
@BeforeEach
void setUp() {
operations.dropCollection(ImmutablePerson.class);
}
@@ -48,7 +46,7 @@ public class ImmutableEntityIntegrationTest {
* {@link ImmutablePerson#getRandomNumber()} gets set via {@link ApplicationConfiguration#beforeConvertCallback()}.
*/
@Test
public void setsRandomNumberOnSave() {
void setsRandomNumberOnSave() {
var unsaved = new ImmutablePerson();
assertThat(unsaved.getRandomNumber()).isZero();
@@ -64,7 +62,7 @@ public class ImmutableEntityIntegrationTest {
* {@link ImmutableRecord#getRandomNumber()} gets set via {@link ApplicationConfiguration#beforeConvertCallback()}.
*/
@Test
public void setsRandomNumberOnSaveRecord() {
void setsRandomNumberOnSaveRecord() {
var unsaved = new ImmutableRecord(null, 0);
assertThat(unsaved.randomNumber()).isZero();

View File

@@ -15,33 +15,27 @@
*/
package example.springdata.mongodb.projections;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import java.util.Collection;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.projection.TargetAware;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration tests for {@link CustomerRepository} to show projection capabilities.
*
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class CustomerRepositoryIntegrationTest {
@DataMongoTest
class CustomerRepositoryIntegrationTest {
@Configuration
@EnableAutoConfiguration
@@ -49,10 +43,10 @@ public class CustomerRepositoryIntegrationTest {
@Autowired CustomerRepository customers;
Customer dave, carter;
private Customer dave, carter;
@Before
public void setUp() {
@BeforeEach
void setUp() {
customers.deleteAll();
@@ -61,60 +55,60 @@ public class CustomerRepositoryIntegrationTest {
}
@Test
public void projectsEntityIntoInterface() {
void projectsEntityIntoInterface() {
var result = customers.findAllProjectedBy();
assertThat(result, hasSize(2));
assertThat(result.iterator().next().getFirstname(), is("Dave"));
assertThat(result).hasSize(2);
assertThat(result.iterator().next().getFirstname()).isEqualTo("Dave");
}
@Test
public void projectsToDto() {
void projectsToDto() {
var result = customers.findAllDtoedBy();
assertThat(result, hasSize(2));
assertThat(result.iterator().next().firstname(), is("Dave"));
assertThat(result).hasSize(2);
assertThat(result.iterator().next().firstname()).isEqualTo("Dave");
}
@Test
public void projectsDynamically() {
void projectsDynamically() {
var result = customers.findByFirstname("Dave", CustomerProjection.class);
assertThat(result, hasSize(1));
assertThat(result.iterator().next().getFirstname(), is("Dave"));
assertThat(result).hasSize(1);
assertThat(result.iterator().next().getFirstname()).isEqualTo("Dave");
}
@Test
public void projectsIndividualDynamically() {
void projectsIndividualDynamically() {
var result = customers.findProjectedById(dave.getId(), CustomerSummary.class);
assertThat(result, is(notNullValue()));
assertThat(result.getFullName(), is("Dave Matthews"));
assertThat(result).isNotNull();
assertThat(result.getFullName()).isEqualTo("Dave Matthews");
// Proxy backed by original instance as the projection uses dynamic elements
assertThat(((TargetAware) result).getTarget(), is(instanceOf(Customer.class)));
assertThat(((TargetAware) result).getTarget()).isInstanceOf(Customer.class);
}
@Test
public void projectIndividualInstance() {
void projectIndividualInstance() {
var result = customers.findProjectedById(dave.getId());
assertThat(result, is(notNullValue()));
assertThat(result.getFirstname(), is("Dave"));
assertThat(((TargetAware) result).getTarget(), is(instanceOf(Customer.class)));
assertThat(result).isNotNull();
assertThat(result.getFirstname()).isEqualTo("Dave");
assertThat(((TargetAware) result).getTarget()).isInstanceOf(Customer.class);
}
@Test
public void supportsProjectionInCombinationWithPagination() {
void supportsProjectionInCombinationWithPagination() {
var page = customers
.findPagedProjectedBy(PageRequest.of(0, 1, Sort.by(Direction.ASC, "lastname")));
assertThat(page.getContent().get(0).getFirstname(), is("Carter"));
assertThat(page.getContent().get(0).getFirstname()).isEqualTo("Carter");
}
}