Migrate Cassandra examples to JUnit 5.

See #583
This commit is contained in:
Mark Paluch
2021-03-23 08:50:52 +01:00
parent 4bcf378114
commit 60385ee4b4
25 changed files with 568 additions and 534 deletions

View File

@@ -22,30 +22,25 @@ import example.springdata.cassandra.util.CassandraKeyspace;
import java.time.Duration;
import java.time.Instant;
import org.junit.Before;
import org.junit.ClassRule;
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.test.context.junit4.SpringRunner;
/**
* Integration test showing the basic usage of Auditing through {@link AuditedPersonRepository}.
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BasicConfiguration.class)
public class AuditedPersonRepositoryTests {
@ClassRule public final static CassandraKeyspace CASSANDRA_KEYSPACE = CassandraKeyspace.onLocalhost();
@CassandraKeyspace
class AuditedPersonRepositoryTests {
@Autowired AuditedPersonRepository repository;
@Before
public void setUp() {
@BeforeEach
void setUp() {
repository.deleteAll();
}
@@ -53,7 +48,7 @@ public class AuditedPersonRepositoryTests {
* Saving an object using the Cassandra Repository will create a persistent representation of the object in Cassandra.
*/
@Test
public void insertShouldSetCreatedDate() {
void insertShouldSetCreatedDate() {
AuditedPerson person = new AuditedPerson();
person.setId(42L);
@@ -73,7 +68,7 @@ public class AuditedPersonRepositoryTests {
* Modifying an existing object will update the last modified fields.
*/
@Test
public void updateShouldSetLastModifiedDate() {
void updateShouldSetLastModifiedDate() {
AuditedPerson person = new AuditedPerson();
person.setId(42L);

View File

@@ -16,20 +16,17 @@
package example.springdata.cassandra.basic;
import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.assertj.core.api.Assumptions.*;
import example.springdata.cassandra.util.CassandraKeyspace;
import example.springdata.cassandra.util.CassandraVersion;
import org.junit.Before;
import org.junit.ClassRule;
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.data.util.Version;
import org.springframework.test.context.junit4.SpringRunner;
import com.datastax.oss.driver.api.core.CqlSession;
@@ -41,20 +38,18 @@ import com.datastax.oss.driver.api.core.CqlSession;
* @author Christoph Strobl
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BasicConfiguration.class)
public class BasicUserRepositoryTests {
@CassandraKeyspace
class BasicUserRepositoryTests {
public final static Version CASSANDRA_3_4 = Version.parse("3.4");
@ClassRule public final static CassandraKeyspace CASSANDRA_KEYSPACE = CassandraKeyspace.onLocalhost();
private final static Version CASSANDRA_3_4 = Version.parse("3.4");
@Autowired BasicUserRepository repository;
@Autowired CqlSession session;
User user;
private User user;
@Before
public void setUp() {
@BeforeEach
void setUp() {
user = new User();
user.setId(42L);
@@ -67,7 +62,7 @@ public class BasicUserRepositoryTests {
* Saving an object using the Cassandra Repository will create a persistent representation of the object in Cassandra.
*/
@Test
public void findSavedUserById() {
void findSavedUserById() {
user = repository.save(user);
@@ -78,7 +73,7 @@ public class BasicUserRepositoryTests {
* Cassandra can be queries by using query methods annotated with {@link @Query}.
*/
@Test
public void findByAnnotatedQueryMethod() {
void findByAnnotatedQueryMethod() {
repository.save(user);
@@ -92,7 +87,7 @@ public class BasicUserRepositoryTests {
* key requires a secondary index.
*/
@Test
public void findByDerivedQueryMethod() throws InterruptedException {
void findByDerivedQueryMethod() throws InterruptedException {
session.execute("CREATE INDEX IF NOT EXISTS user_username ON users (uname);");
/*
@@ -110,9 +105,9 @@ public class BasicUserRepositoryTests {
* Spring Data Cassandra supports {@code LIKE} and {@code CONTAINS} query keywords to for SASI indexes.
*/
@Test
public void findByDerivedQueryMethodWithSASI() throws InterruptedException {
void findByDerivedQueryMethodWithSASI() throws InterruptedException {
assumeTrue(CassandraVersion.getReleaseVersion(session).isGreaterThanOrEqualTo(CASSANDRA_3_4));
assumeThat(CassandraVersion.getReleaseVersion(session).isGreaterThanOrEqualTo(CASSANDRA_3_4)).isTrue();
session.execute("CREATE CUSTOM INDEX ON users (lname) USING 'org.apache.cassandra.index.sasi.SASIIndex';");
/*

View File

@@ -24,17 +24,14 @@ import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.ClassRule;
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.data.cassandra.core.AsyncCassandraTemplate;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.concurrent.ListenableFuture;
import com.datastax.oss.driver.api.core.CqlSession;
@@ -47,16 +44,15 @@ import com.datastax.oss.driver.api.querybuilder.insert.Insert;
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BasicConfiguration.class)
@CassandraKeyspace
public class CassandraOperationsIntegrationTests {
@ClassRule public final static CassandraKeyspace CASSANDRA_KEYSPACE = CassandraKeyspace.onLocalhost();
@Autowired CqlSession session;
@Autowired CassandraOperations template;
@Before
@BeforeEach
public void setUp() throws Exception {
template.getCqlOperations().execute("TRUNCATE users");
}

View File

@@ -17,9 +17,6 @@ package example.springdata.cassandra.convert;
import static org.assertj.core.api.Assertions.*;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.data.TupleValue;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import example.springdata.cassandra.util.CassandraKeyspace;
import java.util.Arrays;
@@ -27,29 +24,29 @@ import java.util.Currency;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.ClassRule;
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.data.cassandra.core.CassandraOperations;
import org.springframework.test.context.junit4.SpringRunner;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.data.TupleValue;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
/**
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@CassandraKeyspace
@SpringBootTest(classes = ConverterConfiguration.class)
public class ConversionIntegrationTests {
@ClassRule public final static CassandraKeyspace CASSANDRA_KEYSPACE = CassandraKeyspace.onLocalhost();
class ConversionIntegrationTests {
@Autowired CassandraOperations operations;
@Before
public void setUp() {
@BeforeEach
void setUp() {
operations.truncate(Addressbook.class);
}
@@ -58,7 +55,7 @@ public class ConversionIntegrationTests {
* custom {@link example.springdata.cassandra.convert.ConverterConfiguration.PersonWriteConverter}.
*/
@Test
public void shouldCreateAddressbook() {
void shouldCreateAddressbook() {
Addressbook addressbook = new Addressbook();
addressbook.setId("private");
@@ -82,7 +79,7 @@ public class ConversionIntegrationTests {
* custom {@link example.springdata.cassandra.convert.ConverterConfiguration.PersonReadConverter}.
*/
@Test
public void shouldReadAddressbook() {
void shouldReadAddressbook() {
Addressbook addressbook = new Addressbook();
addressbook.setId("private");
@@ -104,7 +101,7 @@ public class ConversionIntegrationTests {
* {@link example.springdata.cassandra.convert.ConverterConfiguration.CustomAddressbookReadConverter}.
*/
@Test
public void shouldReadCustomAddressbook() {
void shouldReadCustomAddressbook() {
Addressbook addressbook = new Addressbook();
addressbook.setId("private");
@@ -124,7 +121,7 @@ public class ConversionIntegrationTests {
* Creates and stores a new {@link Addressbook} inside of Cassandra writing map and tuple columns.
*/
@Test
public void shouldWriteConvertedMapsAndTuples() {
void shouldWriteConvertedMapsAndTuples() {
Addressbook addressbook = new Addressbook();
addressbook.setId("private");

View File

@@ -20,14 +20,12 @@ import example.springdata.cassandra.util.CassandraKeyspace;
import java.util.List;
import java.util.stream.Stream;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Test showing differences between fetching results as {@link List} and {@link Stream streaming} results using
@@ -35,12 +33,10 @@ import org.springframework.test.context.junit4.SpringRunner;
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BasicConfiguration.class)
@CassandraKeyspace
public class LifecycleEventsTests {
@ClassRule public final static CassandraKeyspace CASSANDRA_KEYSPACE = CassandraKeyspace.onLocalhost();
@Autowired CassandraOperations operations;
@Test

View File

@@ -17,12 +17,8 @@ package example.springdata.cassandra.optimisticlocking;
import static org.assertj.core.api.Assertions.*;
import example.springdata.cassandra.util.CassandraKeyspace;
import org.junit.Before;
import org.junit.ClassRule;
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;
@@ -31,23 +27,19 @@ import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.EntityWriteResult;
import org.springframework.data.cassandra.core.UpdateOptions;
import org.springframework.data.cassandra.core.query.Criteria;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Integration test showing the basic usage of Optimistic Locking through {@link OptimisticPersonRepository}.
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BasicConfiguration.class)
public class OptimisticPersonRepositoryTests {
@ClassRule public final static CassandraKeyspace CASSANDRA_KEYSPACE = CassandraKeyspace.onLocalhost();
@Autowired OptimisticPersonRepository repository;
@Autowired CassandraOperations operations;
@Before
@BeforeEach
public void setUp() {
repository.deleteAll();
}

View File

@@ -21,32 +21,28 @@ import example.springdata.cassandra.util.CassandraKeyspace;
import java.util.Collection;
import org.junit.Before;
import org.junit.ClassRule;
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.data.projection.TargetAware;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Integration tests for {@link CustomerRepository} to show projection capabilities.
*
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ProjectionConfiguration.class)
public class CustomerRepositoryIntegrationTest {
@ClassRule public final static CassandraKeyspace CASSANDRA_KEYSPACE = CassandraKeyspace.onLocalhost();
@CassandraKeyspace
class CustomerRepositoryIntegrationTest {
@Autowired CustomerRepository customers;
Customer dave, carter;
private Customer dave, carter;
@Before
public void setUp() {
@BeforeEach
void setUp() {
customers.deleteAll();
@@ -55,7 +51,7 @@ public class CustomerRepositoryIntegrationTest {
}
@Test
public void projectsEntityIntoInterface() {
void projectsEntityIntoInterface() {
Collection<CustomerProjection> result = customers.findAllProjectedBy();
@@ -64,7 +60,7 @@ public class CustomerRepositoryIntegrationTest {
}
@Test
public void projectsDynamically() {
void projectsDynamically() {
Collection<CustomerProjection> result = customers.findById("d", CustomerProjection.class);
@@ -73,7 +69,7 @@ public class CustomerRepositoryIntegrationTest {
}
@Test
public void projectsIndividualDynamically() {
void projectsIndividualDynamically() {
CustomerSummary result = customers.findProjectedById(dave.getId(), CustomerSummary.class);
@@ -85,7 +81,7 @@ public class CustomerRepositoryIntegrationTest {
}
@Test
public void projectIndividualInstance() {
void projectIndividualInstance() {
CustomerProjection result = customers.findProjectedById(dave.getId());

View File

@@ -21,10 +21,8 @@ import example.springdata.cassandra.util.CassandraKeyspace;
import java.util.Collections;
import org.junit.Before;
import org.junit.ClassRule;
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;
@@ -33,7 +31,6 @@ import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.core.CassandraAdminOperations;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.test.context.junit4.SpringRunner;
import com.datastax.oss.driver.api.core.data.UdtValue;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
@@ -45,11 +42,9 @@ import com.datastax.oss.driver.api.core.type.UserDefinedType;
* @author Mark Paluch
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserDefinedTypeIntegrationTest {
@ClassRule public final static CassandraKeyspace CASSANDRA_KEYSPACE = CassandraKeyspace.onLocalhost();
@CassandraKeyspace
class UserDefinedTypeIntegrationTest {
@Configuration
static class Config extends AbstractCassandraConfiguration {
@@ -78,8 +73,8 @@ public class UserDefinedTypeIntegrationTest {
@Autowired CassandraOperations operations;
@Autowired CassandraAdminOperations adminOperations;
@Before
public void before() throws Exception {
@BeforeEach
void before() throws Exception {
operations.getCqlOperations().execute("TRUNCATE person");
}
@@ -87,7 +82,7 @@ public class UserDefinedTypeIntegrationTest {
* Insert a row with a mapped User-defined type.
*/
@Test
public void insertMappedUdt() {
void insertMappedUdt() {
Person person = new Person();
person.setId(42);
@@ -109,7 +104,7 @@ public class UserDefinedTypeIntegrationTest {
* Insert a row with a raw User-defined type.
*/
@Test
public void insertRawUdt() {
void insertRawUdt() {
KeyspaceMetadata keyspaceMetadata = adminOperations.getKeyspaceMetadata();
UserDefinedType address = keyspaceMetadata.getUserDefinedType("address").get();