Migrates JPA examples to JUnit 5.

Original pull request #602
Related tickest #583
This commit is contained in:
divya_jnu08
2021-01-25 20:54:57 +05:30
committed by Jens Schauder
parent 06eed6ec50
commit 6a894d9334
28 changed files with 362 additions and 145 deletions

View File

@@ -19,18 +19,18 @@ import static org.assertj.core.api.Assertions.*;
import javax.transaction.Transactional;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* Integration test for {@link CustomerRepository}.
*
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional
public class CustomerRepositoryIntegrationTests {

View File

@@ -15,15 +15,16 @@
*/
package example.springdata.jpa.auditing;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.transaction.annotation.Transactional;
@@ -31,7 +32,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Oliver Gierke
* @author Thomas Darimont
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
public class AuditableUserSample {
@@ -43,7 +44,7 @@ public class AuditableUserSample {
@Test
public void auditEntityCreation() throws Exception {
assertThat(ReflectionTestUtils.getField(listener, "handler"), is(notNullValue()));
assertThat(ReflectionTestUtils.getField(listener, "handler"),is(notNullValue()));
AuditableUser user = new AuditableUser();
user.setUsername("username");

View File

@@ -24,9 +24,9 @@ import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
/**
@@ -48,7 +48,7 @@ public class BasicFactorySetup {
*
* @throws Exception
*/
@Before
@BeforeEach
public void setUp() {
em = factory.createEntityManager();
@@ -69,7 +69,7 @@ public class BasicFactorySetup {
/**
* Rollback transaction.
*/
@After
@AfterEach
public void tearDown() {
em.getTransaction().rollback();

View File

@@ -27,9 +27,9 @@ import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.data.repository.CrudRepository;
@@ -47,7 +47,7 @@ public class BasicSample {
/**
* Sets up a {@link SimpleJpaRepository} instance.
*/
@Before
@BeforeEach
public void setUp() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa.sample.plain");
@@ -58,7 +58,7 @@ public class BasicSample {
em.getTransaction().begin();
}
@After
@AfterEach
public void tearDown() {
em.getTransaction().rollback();
}

View File

@@ -15,16 +15,16 @@
*/
package example.springdata.jpa.caching;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -34,7 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Thomas Darimont
* @author Andrea Rizzini
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
public class CachingRepositoryTests {

View File

@@ -15,15 +15,15 @@
*/
package example.springdata.jpa.compositions;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -31,7 +31,7 @@ import org.springframework.transaction.annotation.Transactional;
*
* @author Mark Paluch
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
public class ComposedRepositoryTests {

View File

@@ -15,15 +15,15 @@
*/
package example.springdata.jpa.custom;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -32,7 +32,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Oliver Gierke
* @author Thomas Darimont
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
// @ActiveProfiles("jdbc") // Uncomment @ActiveProfiles to enable the JDBC Implementation of the custom repository

View File

@@ -15,13 +15,13 @@
*/
package example.springdata.jpa.customall;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -30,7 +30,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Oliver Gierke
* @soundtrack Elen - It's you (Elen)
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
public class UserRepositoryCustomizationTests {

View File

@@ -15,14 +15,14 @@
*/
package example.springdata.jpa.projections;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Collection;
import java.util.Map;
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.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
@@ -32,7 +32,7 @@ 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;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -40,7 +40,7 @@ import org.springframework.transaction.annotation.Transactional;
*
* @author Oliver Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional
public class CustomerRepositoryIntegrationTest {
@@ -53,7 +53,7 @@ public class CustomerRepositoryIntegrationTest {
Customer dave, carter;
@Before
@BeforeEach
public void setUp() {
this.dave = customers.save(new Customer("Dave", "Matthews"));

View File

@@ -16,24 +16,27 @@
package example.springdata.jpa.simple;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.springframework.data.domain.Sort.Direction.*;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.iterableWithSize;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.springframework.data.domain.Sort.Direction.ASC;
import static org.springframework.data.domain.Sort.Direction.DESC;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -43,7 +46,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Thomas Darimont
* @author Christoph Strobl
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
public class SimpleUserRepositoryTests {
@@ -51,7 +54,7 @@ public class SimpleUserRepositoryTests {
@Autowired SimpleUserRepository repository;
User user;
@Before
@BeforeEach
public void setUp() {
user = new User();

View File

@@ -15,13 +15,13 @@
*/
package example.springdata.jpa.interceptors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class InterceptorIntegrationTest {

View File

@@ -25,12 +25,13 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@@ -40,7 +41,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Oliver Gierke
* @author Thomas Darimont
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional
@Slf4j
@@ -109,10 +110,12 @@ public class Java8IntegrationTests {
* Query methods using streaming need to be used inside a surrounding transaction to keep the connection open while
* the stream is consumed. We simulate that not being the case by actively disabling the transaction here.
*/
@Test(expected = InvalidDataAccessApiUsageException.class)
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void rejectsStreamExecutionIfNoSurroundingTransactionActive() {
repository.findAllByLastnameIsNotNull();
Assertions.assertThrows(InvalidDataAccessApiUsageException.class, () -> {
repository.findAllByLastnameIsNotNull();
});
}
/**

View File

@@ -15,18 +15,19 @@
*/
package example.springdata.jpa.fetchgraph;
import static org.assertj.core.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import java.util.Collections;
import javax.persistence.EntityManager;
import org.hibernate.LazyInitializationException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -35,7 +36,8 @@ import org.springframework.transaction.annotation.Transactional;
* @author Thomas Darimont
* @author Jens Schauder
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest(classes = FetchGraphConfiguration.class)
public class FetchGraphIntegrationTests {

View File

@@ -15,21 +15,21 @@
*/
package example.springdata.jpa.resultsetmappings;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.*;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
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.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Thomas Darimont
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@DataJpaTest
@Transactional
public class SubscriptionRepositoryIntegrationTests {
@@ -39,7 +39,7 @@ public class SubscriptionRepositoryIntegrationTests {
@Autowired SubscriptionRepository repository;
@Before
@BeforeEach
public void setUp() {
repository.save(new Subscription(SERVICE_1, 1));

View File

@@ -16,17 +16,17 @@
package example.springdata.jpa.storedprocedures;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.MatcherAssert.assertThat;
import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -35,7 +35,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Thomas Darimont
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional
public class UserRepositoryIntegrationTests {

View File

@@ -15,19 +15,20 @@
*/
package example.springdata.jpa.multipleds.customer;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.Optional;
import javax.persistence.EntityManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -35,7 +36,7 @@ import org.springframework.transaction.annotation.Transactional;
*
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional(transactionManager = "customerTransactionManager")
public class CustomerRepositoryTests {

View File

@@ -15,24 +15,24 @@
*/
package example.springdata.jpa.multipleds.order;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.MatcherAssert.assertThat;
import example.springdata.jpa.multipleds.customer.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
import example.springdata.jpa.multipleds.customer.CustomerRepository;
/**
* Integration test for {@link CustomerRepository}.
*
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional(transactionManager = "orderTransactionManager")
public class OrderRepositoryTests {

View File

@@ -15,16 +15,16 @@
*/
package example.springdata.jpa.querybyexample;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
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.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -34,7 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Mark Paluch
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
public class UserRepositoryInheritanceIntegrationTests {
@@ -43,7 +43,7 @@ public class UserRepositoryInheritanceIntegrationTests {
User skyler, walter, flynn;
@Before
@BeforeEach
public void setUp() {
repository.deleteAll();

View File

@@ -15,20 +15,21 @@
*/
package example.springdata.jpa.querybyexample;
import static org.assertj.core.api.Assertions.*;
import static org.springframework.data.domain.ExampleMatcher.*;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.data.domain.ExampleMatcher.matching;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.ignoreCase;
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;
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.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -38,7 +39,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Oliver Gierke
* @author Jens Schauder
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
public class UserRepositoryIntegrationTests {
@@ -47,7 +48,7 @@ public class UserRepositoryIntegrationTests {
User skyler, walter, flynn, marie, hank;
@Before
@BeforeEach
public void setUp() {
repository.deleteAll();

View File

@@ -15,21 +15,24 @@
*/
package example.springdata.jpa.security;
import static java.util.Collections.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static java.util.Collections.singleton;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import java.util.List;
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.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -38,7 +41,7 @@ import org.springframework.transaction.annotation.Transactional;
* @author Oliver Gierke
* @author Thomas Darimont
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest
@Transactional
public class SecurityIntegrationTests {
@@ -51,7 +54,7 @@ public class SecurityIntegrationTests {
UsernamePasswordAuthenticationToken olliAuth, tomAuth, adminAuth;
BusinessObject object1, object2, object3;
@Before
@BeforeEach
public void setup() {
tom = userRepository.save(new User("thomas", "darimont", "tdarimont@example.org"));

View File

@@ -0,0 +1,51 @@
/**
*
*/
package example.springdata.jpa.showcase;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.Nullable;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.web.ServletTestExecutionListener;
/**
*
*/
@ExtendWith(SpringExtension.class)
@TestExecutionListeners({ ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
EventPublishingTestExecutionListener.class })
public abstract class AbstractShowcaseContextTests implements ApplicationContextAware {
/**
* Logger available to subclasses.
*/
protected final Log logger = LogFactory.getLog(getClass());
/**
* The {@link ApplicationContext} that was injected into this test instance via
* {@link #setApplicationContext(ApplicationContext)}.
*/
@Nullable protected ApplicationContext applicationContext;
/**
* Set the {@link ApplicationContext} to be used by this test instance, provided via {@link ApplicationContextAware}
* semantics.
*
* @param applicationContext the ApplicationContext that this test runs in
*/
@Override
public final void setApplicationContext(final ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}

View File

@@ -15,9 +15,10 @@
*/
package example.springdata.jpa.showcase;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;
@@ -26,7 +27,8 @@ import org.springframework.transaction.annotation.Transactional;
*/
@SpringBootTest
@Transactional
public abstract class AbstractShowcaseTest extends AbstractTransactionalJUnit4SpringContextTests {
@ExtendWith(SpringExtension.class)
public abstract class AbstractShowcaseTest extends AbstractTransactionalShowcaseContextTests {
@SpringBootApplication
static class TestConfig {}

View File

@@ -0,0 +1,150 @@
/**
*
*/
package example.springdata.jpa.showcase;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.lang.Nullable;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.test.context.web.ServletTestExecutionListener;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
/**
*
*/
@TestExecutionListeners(listeners = { ServletTestExecutionListener.class,
DirtiesContextBeforeModesTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class, EventPublishingTestExecutionListener.class }, inheritListeners = false)
@Transactional
public class AbstractTransactionalShowcaseContextTests extends AbstractShowcaseContextTests{
/**
* The {@code JdbcTemplate} that this base class manages, available to subclasses.
* @since 3.2
*/
protected final JdbcTemplate jdbcTemplate = new JdbcTemplate();
@Nullable
private String sqlScriptEncoding;
/**
* Set the {@code DataSource}, typically provided via Dependency Injection.
* <p>This method also instantiates the {@link #jdbcTemplate} instance variable.
*/
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate.setDataSource(dataSource);
}
/**
* Specify the encoding for SQL scripts, if different from the platform encoding.
* @see #executeSqlScript
*/
public void setSqlScriptEncoding(String sqlScriptEncoding) {
this.sqlScriptEncoding = sqlScriptEncoding;
}
/**
* Convenience method for counting the rows in the given table.
* @param tableName table name to count rows in
* @return the number of rows in the table
* @see JdbcTestUtils#countRowsInTable
*/
protected int countRowsInTable(String tableName) {
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
}
/**
* Convenience method for counting the rows in the given table, using the
* provided {@code WHERE} clause.
* <p>See the Javadoc for {@link JdbcTestUtils#countRowsInTableWhere} for details.
* @param tableName the name of the table to count rows in
* @param whereClause the {@code WHERE} clause to append to the query
* @return the number of rows in the table that match the provided
* {@code WHERE} clause
* @since 3.2
* @see JdbcTestUtils#countRowsInTableWhere
*/
protected int countRowsInTableWhere(String tableName, String whereClause) {
return JdbcTestUtils.countRowsInTableWhere(this.jdbcTemplate, tableName, whereClause);
}
/**
* Convenience method for deleting all rows from the specified tables.
* <p>Use with caution outside of a transaction!
* @param names the names of the tables from which to delete
* @return the total number of rows deleted from all specified tables
* @see JdbcTestUtils#deleteFromTables
*/
protected int deleteFromTables(String... names) {
return JdbcTestUtils.deleteFromTables(this.jdbcTemplate, names);
}
/**
* Convenience method for deleting all rows from the given table, using the
* provided {@code WHERE} clause.
* <p>Use with caution outside of a transaction!
* <p>See the Javadoc for {@link JdbcTestUtils#deleteFromTableWhere} for details.
* @param tableName the name of the table to delete rows from
* @param whereClause the {@code WHERE} clause to append to the query
* @param args arguments to bind to the query (leaving it to the {@code
* PreparedStatement} to guess the corresponding SQL type); may also contain
* {@link org.springframework.jdbc.core.SqlParameterValue SqlParameterValue}
* objects which indicate not only the argument value but also the SQL type
* and optionally the scale.
* @return the number of rows deleted from the table
* @since 4.0
* @see JdbcTestUtils#deleteFromTableWhere
*/
protected int deleteFromTableWhere(String tableName, String whereClause, Object... args) {
return JdbcTestUtils.deleteFromTableWhere(this.jdbcTemplate, tableName, whereClause, args);
}
/**
* Convenience method for dropping all of the specified tables.
* <p>Use with caution outside of a transaction!
* @param names the names of the tables to drop
* @since 3.2
* @see JdbcTestUtils#dropTables
*/
protected void dropTables(String... names) {
JdbcTestUtils.dropTables(this.jdbcTemplate, names);
}
/**
* Execute the given SQL script.
* <p>Use with caution outside of a transaction!
* <p>The script will normally be loaded by classpath.
* <p><b>Do not use this method to execute DDL if you expect rollback.</b>
* @param sqlResourcePath the Spring resource path for the SQL script
* @param continueOnError whether or not to continue without throwing an
* exception in the event of an error
* @throws DataAccessException if there is an error executing a statement
* @see ResourceDatabasePopulator
* @see #setSqlScriptEncoding
*/
protected void executeSqlScript(String sqlResourcePath, boolean continueOnError) throws DataAccessException {
DataSource ds = this.jdbcTemplate.getDataSource();
Assert.state(ds != null, "No DataSource set");
Assert.state(this.applicationContext != null, "No ApplicationContext set");
Resource resource = this.applicationContext.getResource(sqlResourcePath);
new ResourceDatabasePopulator(continueOnError, false, this.sqlScriptEncoding, resource).execute(ds);
}
}

View File

@@ -25,7 +25,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
@@ -40,7 +40,6 @@ public class AccountRepositoryIntegrationTest extends AbstractShowcaseTest {
@Test
public void savesAccount() {
Account account = accountRepository.save(new Account());
assertThat(account.getId()).isNotNull();

View File

@@ -17,6 +17,7 @@ package example.springdata.jpa.showcase.after;
import static example.springdata.jpa.showcase.snippets.CustomerSpecifications.*;
import static org.assertj.core.api.Assertions.*;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Customer;
@@ -24,7 +25,7 @@ import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

View File

@@ -16,17 +16,16 @@
package example.springdata.jpa.showcase.before;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Account;
import example.springdata.jpa.showcase.core.Customer;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Integration test for {@link AccountService}.
*

View File

@@ -16,16 +16,17 @@
package example.springdata.jpa.showcase.before;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import example.springdata.jpa.showcase.AbstractShowcaseTest;
import example.springdata.jpa.showcase.core.Customer;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
/**
* Integration test for {@link CustomerService}.
*

View File

@@ -23,12 +23,12 @@ import io.vavr.control.Try;
import javax.persistence.NonUniqueResultException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -36,7 +36,7 @@ import org.springframework.transaction.annotation.Transactional;
*
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@Transactional
@SpringBootTest
public class PersonRepositoryIntegrationTests {