Migrate exception checking tests to use AssertJ

Migrate tests that use `@Test(expectedException=...)` or
`try...fail...catch` to use AssertJ's `assertThatException`
instead.
This commit is contained in:
Phillip Webb
2019-05-20 10:34:51 -07:00
parent fb26fc3f94
commit 02850f357f
561 changed files with 6592 additions and 10389 deletions

View File

@@ -29,12 +29,12 @@ import org.springframework.orm.jpa.domain.DriversLicense;
import org.springframework.orm.jpa.domain.Person;
import org.springframework.util.SerializationTestUtils;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Integration tests for LocalContainerEntityManagerFactoryBean.
@@ -95,30 +95,22 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
@Test
public void testBogusQuery() {
try {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
Query query = sharedEntityManager.createQuery("It's raining toads");
// required in OpenJPA case
query.executeUpdate();
fail("Should have thrown a RuntimeException");
}
catch (RuntimeException ex) {
// expected
}
});
}
@Test
public void testGetReferenceWhenNoRow() {
try {
Person notThere = sharedEntityManager.getReference(Person.class, 666);
// We may get here (as with Hibernate). Either behaviour is valid:
// throw exception on first access or on getReference itself.
notThere.getFirstName();
fail("Should have thrown an EntityNotFoundException or ObjectNotFoundException");
}
catch (Exception ex) {
assertTrue(ex.getClass().getName().endsWith("NotFoundException"));
}
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
Person notThere = sharedEntityManager.getReference(Person.class, 666);
// We may get here (as with Hibernate). Either behaviour is valid:
// throw exception on first access or on getReference itself.
notThere.getFirstName();
})
.matches(ex -> ex.getClass().getName().endsWith("NotFoundException"));
}
@Test
@@ -169,12 +161,8 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
@Test
public void testEntityManagerProxyRejectsProgrammaticTxManagement() {
try {
sharedEntityManager.getTransaction();
fail("Should not be able to create transactions on container managed EntityManager");
}
catch (IllegalStateException ex) {
}
assertThatIllegalStateException().as("Should not be able to create transactions on container managed EntityManager").isThrownBy(
sharedEntityManager::getTransaction);
}
@Test
@@ -200,13 +188,8 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertEquals(0, people.size());
try {
assertNull(q.getSingleResult());
fail("Should have thrown NoResultException");
}
catch (NoResultException ex) {
// expected
}
assertThatExceptionOfType(NoResultException.class).isThrownBy(
q::getSingleResult);
}
@Test
@@ -218,13 +201,8 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertEquals(0, people.size());
try {
assertNull(q.getSingleResult());
fail("Should have thrown NoResultException");
}
catch (NoResultException ex) {
// expected
}
assertThatExceptionOfType(NoResultException.class).isThrownBy(
q::getSingleResult);
}
@Test
@@ -234,13 +212,8 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
q.setFlushMode(FlushModeType.AUTO);
List<Person> people = q.getResultList();
assertEquals(0, people.size());
try {
assertNull(q.getSingleResult());
fail("Should have thrown NoResultException");
}
catch (NoResultException ex) {
// expected
}
assertThatExceptionOfType(NoResultException.class).isThrownBy(
q::getSingleResult);
}
@Test
@@ -253,24 +226,16 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
q.setFlushMode(FlushModeType.AUTO);
List<Person> people = q.getResultList();
assertEquals(0, people.size());
try {
assertNull(q.getSingleResult());
fail("Should have thrown IllegalStateException");
}
catch (Exception ex) {
// We would typically expect an IllegalStateException, but Hibernate throws a
// PersistenceException. So we assert the contents of the exception message instead.
assertTrue(ex.getMessage().contains("closed"));
}
q = em.createQuery("select p from Person as p");
q.setFlushMode(FlushModeType.AUTO);
try {
assertNull(q.getSingleResult());
fail("Should have thrown NoResultException");
}
catch (NoResultException ex) {
// expected
}
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
q.getSingleResult())
.withMessageContaining("closed");
// We would typically expect an IllegalStateException, but Hibernate throws a
// PersistenceException. So we assert the contents of the exception message instead.
Query q2 = em.createQuery("select p from Person as p");
q2.setFlushMode(FlushModeType.AUTO);
assertThatExceptionOfType(NoResultException.class).isThrownBy(
q2::getSingleResult);
}
@Test

View File

@@ -26,11 +26,11 @@ import org.junit.Test;
import org.springframework.orm.jpa.domain.Person;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* An application-managed entity manager can join an existing transaction,
@@ -72,13 +72,8 @@ public class ApplicationManagedEntityManagerIntegrationTests extends AbstractEnt
@Test
public void testCannotFlushWithoutGettingTransaction() {
EntityManager em = entityManagerFactory.createEntityManager();
try {
doInstantiateAndSave(em);
fail("Should have thrown TransactionRequiredException");
}
catch (TransactionRequiredException ex) {
// expected
}
assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(() ->
doInstantiateAndSave(em));
// TODO following lines are a workaround for Hibernate bug
// If Hibernate throws an exception due to flush(),

View File

@@ -30,13 +30,14 @@ import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.orm.jpa.domain.Person;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Integration tests using in-memory database for container-managed JPA
@@ -81,26 +82,16 @@ public class ContainerManagedEntityManagerIntegrationTests extends AbstractEntit
assertTrue(people.isEmpty());
assertTrue("Should be open to start with", em.isOpen());
try {
em.close();
fail("Close should not work on container managed EM");
}
catch (IllegalStateException ex) {
// OK
}
assertThatIllegalStateException().as("Close should not work on container managed EM").isThrownBy(
em::close);
assertTrue(em.isOpen());
}
// This would be legal, at least if not actually _starting_ a tx
@Test
public void testEntityManagerProxyRejectsProgrammaticTxManagement() {
try {
createContainerManagedEntityManager().getTransaction();
fail("Should have thrown an IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
assertThatIllegalStateException().isThrownBy(
createContainerManagedEntityManager()::getTransaction);
}
/*
@@ -115,14 +106,8 @@ public class ContainerManagedEntityManagerIntegrationTests extends AbstractEntit
@Test
public void testContainerEntityManagerProxyRejectsJoinTransactionWithoutTransaction() {
endTransaction();
try {
createContainerManagedEntityManager().joinTransaction();
fail("Should have thrown a TransactionRequiredException");
}
catch (TransactionRequiredException ex) {
// expected
}
assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(
createContainerManagedEntityManager()::joinTransaction);
}
@Test

View File

@@ -26,8 +26,8 @@ import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -43,14 +43,8 @@ public class DefaultJpaDialectTests {
public void testDefaultTransactionDefinition() throws Exception {
DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
definition.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
try {
dialect.beginTransaction(null, definition);
fail("expected exception");
}
catch (TransactionException e) {
// ok
}
assertThatExceptionOfType(TransactionException.class).isThrownBy(() ->
dialect.beginTransaction(null, definition));
}
@Test

View File

@@ -35,10 +35,10 @@ import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -57,13 +57,8 @@ public class EntityManagerFactoryUtilsTests {
@Test
public void testDoGetEntityManager() {
// test null assertion
try {
EntityManagerFactoryUtils.doGetTransactionalEntityManager(null, null);
fail("expected exception");
}
catch (IllegalArgumentException ex) {
// it's okay
}
assertThatIllegalArgumentException().isThrownBy(() ->
EntityManagerFactoryUtils.doGetTransactionalEntityManager(null, null));
EntityManagerFactory factory = mock(EntityManagerFactory.class);
// no tx active

View File

@@ -37,11 +37,10 @@ import org.springframework.transaction.support.TransactionSynchronizationAdapter
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.transaction.support.TransactionTemplate;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.mock;
@@ -164,7 +163,7 @@ public class JpaTransactionManagerTests {
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
@@ -172,13 +171,7 @@ public class JpaTransactionManagerTests {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
throw new RuntimeException("some exception");
}
});
fail("Should have propagated RuntimeException");
}
catch (RuntimeException ex) {
// expected
assertEquals("some exception", ex.getMessage());
}
})).withMessage("some exception");
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
@@ -197,7 +190,7 @@ public class JpaTransactionManagerTests {
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
@@ -205,12 +198,7 @@ public class JpaTransactionManagerTests {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
throw new RuntimeException("some exception");
}
});
fail("Should have propagated RuntimeException");
}
catch (RuntimeException ex) {
// expected
}
}));
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
@@ -293,7 +281,7 @@ public class JpaTransactionManagerTests {
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
try {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
@@ -306,12 +294,7 @@ public class JpaTransactionManagerTests {
}
});
}
});
fail("Should have propagated RuntimeException");
}
catch (RuntimeException ex) {
// expected
}
}));
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
@@ -334,28 +317,23 @@ public class JpaTransactionManagerTests {
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
try {
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() ->
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue(TransactionSynchronizationManager.hasResource(factory));
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status.setRollbackOnly();
return null;
}
});
}
});
fail("Should have thrown TransactionSystemException");
}
catch (TransactionSystemException tse) {
// expected
assertTrue(tse.getCause() instanceof RollbackException);
}
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status.setRollbackOnly();
return null;
}
});
}
}))
.withCauseInstanceOf(RollbackException.class);
assertTrue(!TransactionSynchronizationManager.hasResource(factory));
assertTrue(!TransactionSynchronizationManager.isSynchronizationActive());
@@ -770,17 +748,12 @@ public class JpaTransactionManagerTests {
given(manager.isOpen()).willReturn(true);
try {
assertThatExceptionOfType(InvalidIsolationLevelException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
}
});
fail("Should have thrown InvalidIsolationLevelException");
}
catch (InvalidIsolationLevelException ex) {
// expected
}
}));
verify(manager).close();
}

View File

@@ -39,6 +39,8 @@ import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
import org.springframework.util.SerializationTestUtils;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -46,7 +48,6 @@ import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.mock;
@@ -196,13 +197,8 @@ public class LocalContainerEntityManagerFactoryBeanTests extends AbstractEntityM
em.joinTransaction();
assertFalse(em.contains(testEntity));
try {
jpatm.commit(txStatus);
fail("Should have thrown OptimisticLockingFailureException");
}
catch (OptimisticLockingFailureException ex) {
// expected
}
assertThatExceptionOfType(OptimisticLockingFailureException.class).isThrownBy(() ->
jpatm.commit(txStatus));
cefb.destroy();
@@ -259,13 +255,8 @@ public class LocalContainerEntityManagerFactoryBeanTests extends AbstractEntityM
@Test
public void testInvalidPersistenceUnitName() throws Exception {
try {
createEntityManagerFactoryBean("org/springframework/orm/jpa/domain/persistence.xml", null, "call me Bob");
fail("Should not create factory with this name");
}
catch (IllegalArgumentException ex) {
// Ok
}
assertThatIllegalArgumentException().isThrownBy(() ->
createEntityManagerFactoryBean("org/springframework/orm/jpa/domain/persistence.xml", null, "call me Bob"));
}
protected LocalContainerEntityManagerFactoryBean createEntityManagerFactoryBean(
@@ -306,13 +297,8 @@ public class LocalContainerEntityManagerFactoryBeanTests extends AbstractEntityM
containerEmfb.setPersistenceUnitName(entityManagerName);
containerEmfb.setPersistenceProviderClass(DummyContainerPersistenceProvider.class);
try {
containerEmfb.afterPropertiesSet();
fail();
}
catch (IllegalArgumentException ex) {
// Ok
}
assertThatIllegalArgumentException().isThrownBy(
containerEmfb::afterPropertiesSet);
}

View File

@@ -27,11 +27,12 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willReturn;
import static org.mockito.Mockito.mock;
@@ -56,46 +57,52 @@ public class SharedEntityManagerCreatorTests {
assertThat(SharedEntityManagerCreator.createSharedEntityManager(emf), is(notNullValue()));
}
@Test(expected = TransactionRequiredException.class)
@Test
public void transactionRequiredExceptionOnJoinTransaction() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.joinTransaction();
assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(
em::joinTransaction);
}
@Test(expected = TransactionRequiredException.class)
@Test
public void transactionRequiredExceptionOnFlush() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.flush();
assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(
em::flush);
}
@Test(expected = TransactionRequiredException.class)
@Test
public void transactionRequiredExceptionOnPersist() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.persist(new Object());
assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(() ->
em.persist(new Object()));
}
@Test(expected = TransactionRequiredException.class)
@Test
public void transactionRequiredExceptionOnMerge() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.merge(new Object());
assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(() ->
em.merge(new Object()));
}
@Test(expected = TransactionRequiredException.class)
@Test
public void transactionRequiredExceptionOnRemove() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.remove(new Object());
assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(() ->
em.remove(new Object()));
}
@Test(expected = TransactionRequiredException.class)
@Test
public void transactionRequiredExceptionOnRefresh() {
EntityManagerFactory emf = mock(EntityManagerFactory.class);
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
em.refresh(new Object());
assertThatExceptionOfType(TransactionRequiredException.class).isThrownBy(() ->
em.refresh(new Object()));
}
@Test
@@ -180,13 +187,8 @@ public class SharedEntityManagerCreatorTests {
spq.registerStoredProcedureParameter(2, Object.class, ParameterMode.INOUT);
spq.execute();
assertEquals("y", spq.getOutputParameterValue(0));
try {
spq.getOutputParameterValue(1);
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
assertThatIllegalArgumentException().isThrownBy(() ->
spq.getOutputParameterValue(1));
assertEquals("z", spq.getOutputParameterValue(2));
verify(query).registerStoredProcedureParameter(0, String.class, ParameterMode.OUT);
@@ -216,13 +218,8 @@ public class SharedEntityManagerCreatorTests {
spq.registerStoredProcedureParameter("c", Object.class, ParameterMode.INOUT);
spq.execute();
assertEquals("y", spq.getOutputParameterValue("a"));
try {
spq.getOutputParameterValue("b");
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
}
assertThatIllegalArgumentException().isThrownBy(() ->
spq.getOutputParameterValue("b"));
assertEquals("z", spq.getOutputParameterValue("c"));
verify(query).registerStoredProcedureParameter("a", String.class, ParameterMode.OUT);

View File

@@ -25,10 +25,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.AbstractContainerEntityManagerFactoryIntegrationTests;
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Hibernate-specific JPA tests with multiple EntityManagerFactory instances.
@@ -61,11 +61,8 @@ public class HibernateMultiEntityManagerFactoryIntegrationTests extends Abstract
public void testEntityManagerFactory2() {
EntityManager em = this.entityManagerFactory2.createEntityManager();
try {
em.createQuery("select tb from TestBean");
fail("Should have thrown IllegalArgumentException");
}
catch (IllegalArgumentException ex) {
// expected
assertThatIllegalArgumentException().isThrownBy(() ->
em.createQuery("select tb from TestBean"));
}
finally {
em.close();

View File

@@ -35,6 +35,7 @@ import org.springframework.orm.jpa.hibernate.beans.MultiplePrototypesInSpringCon
import org.springframework.orm.jpa.hibernate.beans.NoDefinitionInSpringContextTestBean;
import org.springframework.orm.jpa.hibernate.beans.SinglePrototypeInSpringContextTestBean;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
@@ -246,32 +247,36 @@ public class HibernateNativeEntityManagerFactorySpringBeanContainerIntegrationTe
assertNull(instance.getApplicationContext());
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void testFallbackExceptionInCaseOfNoSpringBeanFound() {
getBeanContainer().getBean(NoDefinitionInSpringContextTestBean.class,
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE
);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
getBeanContainer().getBean(NoDefinitionInSpringContextTestBean.class,
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE
));
}
@Test(expected = BeanCreationException.class)
@Test
public void testOriginalExceptionInCaseOfFallbackProducerFailure() {
getBeanContainer().getBean(AttributeConverter.class,
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE
);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
getBeanContainer().getBean(AttributeConverter.class,
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE
));
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void testFallbackExceptionInCaseOfNoSpringBeanFoundByName() {
getBeanContainer().getBean("some name", NoDefinitionInSpringContextTestBean.class,
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE
);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
getBeanContainer().getBean("some name", NoDefinitionInSpringContextTestBean.class,
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE
));
}
@Test(expected = BeanCreationException.class)
@Test
public void testOriginalExceptionInCaseOfFallbackProducerFailureByName() {
getBeanContainer().getBean("invalid", AttributeConverter.class,
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE
);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
getBeanContainer().getBean("invalid", AttributeConverter.class,
NativeLifecycleOptions.INSTANCE, IneffectiveBeanInstanceProducer.INSTANCE
));
}

View File

@@ -36,13 +36,13 @@ import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.jdbc.datasource.lookup.MapDataSourceLookup;
import org.springframework.tests.mock.jndi.SimpleNamingContextBuilder;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* Unit and integration tests for the JPA XML resource parsing support.
@@ -264,12 +264,8 @@ public class PersistenceXmlParsingTests {
PersistenceUnitReader reader = new PersistenceUnitReader(
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
String resource = "/org/springframework/orm/jpa/persistence-invalid.xml";
try {
reader.readPersistenceUnitInfos(resource);
fail("expected invalid document exception");
}
catch (RuntimeException expected) {
}
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
reader.readPersistenceUnitInfos(resource));
}
@Ignore // not doing schema parsing anymore for JPA 2.0 compatibility
@@ -278,12 +274,8 @@ public class PersistenceXmlParsingTests {
PersistenceUnitReader reader = new PersistenceUnitReader(
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
String resource = "/org/springframework/orm/jpa/persistence-no-schema.xml";
try {
reader.readPersistenceUnitInfos(resource);
fail("expected invalid document exception");
}
catch (RuntimeException expected) {
}
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
reader.readPersistenceUnitInfos(resource));
}
@Test

View File

@@ -48,10 +48,10 @@ import org.springframework.tests.mock.jndi.ExpectedLookupTemplate;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.SerializationTestUtils;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -551,37 +551,22 @@ public class PersistenceInjectionTests extends AbstractEntityManagerFactoryBeanT
@Test
public void testFieldOfWrongTypeAnnotatedWithPersistenceUnit() {
PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor();
try {
pabpp.postProcessProperties(null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
fail("Can't inject this field");
}
catch (IllegalStateException ex) {
// Ok
}
assertThatIllegalStateException().isThrownBy(() ->
pabpp.postProcessProperties(null, new FieldOfWrongTypeAnnotatedWithPersistenceUnit(), "bean"));
}
@Test
public void testSetterOfWrongTypeAnnotatedWithPersistenceUnit() {
PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor();
try {
pabpp.postProcessProperties(null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), "bean");
fail("Can't inject this setter");
}
catch (IllegalStateException ex) {
// Ok
}
assertThatIllegalStateException().isThrownBy(() ->
pabpp.postProcessProperties(null, new SetterOfWrongTypeAnnotatedWithPersistenceUnit(), "bean"));
}
@Test
public void testSetterWithNoArgs() {
PersistenceAnnotationBeanPostProcessor pabpp = new PersistenceAnnotationBeanPostProcessor();
try {
pabpp.postProcessProperties(null, new SetterWithNoArgs(), "bean");
fail("Can't inject this setter");
}
catch (IllegalStateException ex) {
// Ok
}
assertThatIllegalStateException().isThrownBy(() ->
pabpp.postProcessProperties(null, new SetterWithNoArgs(), "bean"));
}
@Test

View File

@@ -25,10 +25,10 @@ import org.springframework.orm.jpa.EntityManagerHolder;
import org.springframework.orm.jpa.EntityManagerProxy;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -63,13 +63,8 @@ public class SharedEntityManagerFactoryTests {
assertTrue(proxy instanceof EntityManagerProxy);
EntityManagerProxy emProxy = (EntityManagerProxy) proxy;
try {
emProxy.getTargetEntityManager();
fail("Should have thrown IllegalStateException outside of transaction");
}
catch (IllegalStateException ex) {
// expected
}
assertThatIllegalStateException().as("outside of transaction").isThrownBy(
emProxy::getTargetEntityManager);
TransactionSynchronizationManager.bindResource(mockEmf, new EntityManagerHolder(mockEm));
try {