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

@@ -15,21 +15,16 @@
*/
package example.springdata.mongodb.querybyexample;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.domain.ExampleMatcher.*;
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.domain.Example;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration test showing the usage of MongoDB Query-by-Example support through Spring Data repositories for a case
@@ -39,19 +34,18 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Oliver Gierke
* @soundtrack Paul van Dyk - VONYC Sessions Episode 496 with guest Armin van Buuren
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class ContactRepositoryIntegrationTests {
@DataMongoTest
class ContactRepositoryIntegrationTests {
@Autowired UserRepository userRepository;
@Autowired ContactRepository contactRepository;
@Autowired MongoOperations mongoOperations;
Person skyler, walter, flynn;
Relative marie, hank;
private Person skyler, walter, flynn;
private Relative marie, hank;
@Before
public void setUp() {
@BeforeEach
void setUp() {
contactRepository.deleteAll();
@@ -66,36 +60,36 @@ public class ContactRepositoryIntegrationTests {
* @see #153
*/
@Test
public void countByConcreteSubtypeExample() {
void countByConcreteSubtypeExample() {
var example = Example.of(new Person(null, null, null));
assertThat(userRepository.count(example), is(3L));
assertThat(userRepository.count(example)).isEqualTo(3L);
}
/**
* @see #153
*/
@Test
public void findAllPersonsBySimpleExample() {
void findAllPersonsBySimpleExample() {
var example = Example.of(new Person(".*", null, null), //
matching().withStringMatcher(StringMatcher.REGEX));
assertThat(userRepository.findAll(example), containsInAnyOrder(skyler, walter, flynn));
assertThat(userRepository.findAll(example), not(containsInAnyOrder(hank, marie)));
assertThat(userRepository.findAll(example)).contains(skyler, walter, flynn);
assertThat((Iterable) userRepository.findAll(example)).doesNotContain(hank, marie);
}
/**
* @see #153
*/
@Test
public void findAllRelativesBySimpleExample() {
void findAllRelativesBySimpleExample() {
var example = Example.of(new Relative(".*", null, null), //
matching().withStringMatcher(StringMatcher.REGEX));
assertThat(contactRepository.findAll(example), containsInAnyOrder(hank, marie));
assertThat(contactRepository.findAll(example), not(containsInAnyOrder(skyler, walter, flynn)));
assertThat(contactRepository.findAll(example)).contains(hank, marie);
assertThat((Iterable) contactRepository.findAll(example)).doesNotContain(skyler, walter, flynn);
}
}

View File

@@ -16,8 +16,7 @@
package example.springdata.mongodb.querybyexample;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.domain.ExampleMatcher.*;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.*;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;
@@ -26,16 +25,15 @@ import static org.springframework.data.mongodb.core.query.Query.*;
import java.util.Optional;
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.domain.Example;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration test showing the usage of MongoDB Query-by-Example support through Spring Data repositories.
@@ -44,16 +42,15 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Oliver Gierke
*/
@SuppressWarnings("unused")
@RunWith(SpringRunner.class)
@SpringBootTest
public class MongoOperationsIntegrationTests {
@DataMongoTest
class MongoOperationsIntegrationTests {
@Autowired MongoOperations operations;
Person skyler, walter, flynn, marie, hank;
private Person skyler, walter, flynn, marie, hank;
@Before
public void setUp() {
@BeforeEach
void setUp() {
operations.remove(new Query(), Person.class);
@@ -74,74 +71,74 @@ public class MongoOperationsIntegrationTests {
* @see #153
*/
@Test
public void ignoreNullProperties() {
void ignoreNullProperties() {
var query = query(byExample(new Person(null, null, 17)));
assertThat(operations.find(query, Person.class), hasItems(flynn));
assertThat(operations.find(query, Person.class)).contains(flynn);
}
/**
* @see #153
*/
@Test
public void substringMatching() {
void substringMatching() {
var example = Example.of(new Person("er", null, null), matching().//
withStringMatcher(StringMatcher.ENDING));
assertThat(operations.find(query(byExample(example)), Person.class), hasItems(skyler, walter));
assertThat(operations.find(query(byExample(example)), Person.class)).contains(skyler, walter);
}
/**
* @see #154
*/
@Test
public void regexMatching() {
void regexMatching() {
var example = Example.of(new Person("(Skyl|Walt)er", null, null), matching().//
withMatcher("firstname", GenericPropertyMatcher::regex));
assertThat(operations.find(query(byExample(example)), Person.class), hasItems(skyler, walter));
assertThat(operations.find(query(byExample(example)), Person.class)).contains(skyler, walter);
}
/**
* @see #153
*/
@Test
public void matchStartingStringsIgnoreCase() {
void matchStartingStringsIgnoreCase() {
var example = Example.of(new Person("Walter", "WHITE", null), matching(). //
withIgnorePaths("age").//
withMatcher("firstname", startsWith()).//
withMatcher("lastname", ignoreCase()));
assertThat(operations.find(query(byExample(example)), Person.class), hasItems(flynn, walter));
assertThat(operations.find(query(byExample(example)), Person.class)).contains(flynn, walter);
}
/**
* @see #153
*/
@Test
public void configuringMatchersUsingLambdas() {
void configuringMatchersUsingLambdas() {
var example = Example.of(new Person("Walter", "WHITE", null), matching().//
withIgnorePaths("age"). //
withMatcher("firstname", GenericPropertyMatcher::startsWith). //
withMatcher("lastname", GenericPropertyMatcher::ignoreCase));
assertThat(operations.find(query(byExample(example)), Person.class), hasItems(flynn, walter));
assertThat(operations.find(query(byExample(example)), Person.class)).contains(flynn, walter);
}
/**
* @see #153
*/
@Test
public void valueTransformer() {
void valueTransformer() {
var example = Example.of(new Person(null, "White", 99), matching(). //
withMatcher("age", matcher -> matcher.transform(value -> Optional.of(50))));
assertThat(operations.find(query(byExample(example)), Person.class), hasItems(walter));
assertThat(operations.find(query(byExample(example)), Person.class)).contains(walter);
}
}

View File

@@ -21,14 +21,13 @@ import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatc
import java.util.Optional;
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.domain.Example;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.data.domain.ExampleMatcher.*;
/**
* Integration test showing the usage of MongoDB Query-by-Example support through Spring Data repositories.
@@ -38,16 +37,15 @@ import org.springframework.test.context.junit4.SpringRunner;
* @author Jens Schauder
*/
@SuppressWarnings("unused")
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryIntegrationTests {
@DataMongoTest
class UserRepositoryIntegrationTests {
@Autowired UserRepository repository;
Person skyler, walter, flynn, marie, hank;
private Person skyler, walter, flynn, marie, hank;
@Before
public void setUp() {
@BeforeEach
void setUp() {
repository.deleteAll();
@@ -62,7 +60,7 @@ public class UserRepositoryIntegrationTests {
* @see #153
*/
@Test
public void countBySimpleExample() {
void countBySimpleExample() {
var example = Example.of(new Person(null, "White", null));
@@ -73,7 +71,7 @@ public class UserRepositoryIntegrationTests {
* @see #153
*/
@Test
public void ignorePropertiesAndMatchByAge() {
void ignorePropertiesAndMatchByAge() {
var example = Example.of(flynn, matching(). //
withIgnorePaths("firstname", "lastname"));
@@ -85,7 +83,7 @@ public class UserRepositoryIntegrationTests {
* @see #153
*/
@Test
public void substringMatching() {
void substringMatching() {
var example = Example.of(new Person("er", null, null), matching(). //
withStringMatcher(StringMatcher.ENDING));
@@ -97,7 +95,7 @@ public class UserRepositoryIntegrationTests {
* @see #153
*/
@Test
public void regexMatching() {
void regexMatching() {
var example = Example.of(new Person("(Skyl|Walt)er", null, null), matching(). //
withMatcher("firstname", GenericPropertyMatcher::regex));
@@ -109,7 +107,7 @@ public class UserRepositoryIntegrationTests {
* @see #153
*/
@Test
public void matchStartingStringsIgnoreCase() {
void matchStartingStringsIgnoreCase() {
var example = Example.of(new Person("Walter", "WHITE", null), matching(). //
withIgnorePaths("age"). //
@@ -123,7 +121,7 @@ public class UserRepositoryIntegrationTests {
* @see #153
*/
@Test
public void configuringMatchersUsingLambdas() {
void configuringMatchersUsingLambdas() {
var example = Example.of(new Person("Walter", "WHITE", null), matching(). //
withIgnorePaths("age"). //
@@ -137,7 +135,7 @@ public class UserRepositoryIntegrationTests {
* @see #153
*/
@Test
public void valueTransformer() {
void valueTransformer() {
var example = Example.of(new Person(null, "White", 99), matching(). //
withMatcher("age", matcher -> matcher.transform(value -> Optional.of(50))));