Browse Source

Use new AssertJ exception assertions

pull/28720/head
Sam Brannen 2 years ago
parent
commit
1beb7068f6
  1. 8
      integration-tests/src/test/java/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests.java
  2. 10
      integration-tests/src/test/java/org/springframework/aop/framework/autoproxy/AspectJAutoProxyAdviceOrderIntegrationTests.java
  3. 7
      integration-tests/src/test/java/org/springframework/cache/annotation/EnableCachingIntegrationTests.java
  4. 6
      integration-tests/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementIntegrationTests.java
  5. 6
      spring-aop/src/test/java/org/springframework/aop/framework/ProxyFactoryTests.java
  6. 7
      spring-aop/src/test/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptorTests.java
  7. 29
      spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionAspectTests.java
  8. 5
      spring-beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTests.java
  9. 14
      spring-beans/src/test/java/org/springframework/beans/factory/xml/DuplicateBeanIdTests.java
  10. 8
      spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheCustomInterceptorTests.java
  11. 6
      spring-context/src/test/java/org/springframework/aop/aspectj/autoproxy/spr3064/SPR3064Tests.java
  12. 57
      spring-context/src/test/java/org/springframework/aop/framework/AbstractAopProxyTests.java
  13. 14
      spring-context/src/test/java/org/springframework/aop/framework/ProxyFactoryBeanTests.java
  14. 8
      spring-context/src/test/java/org/springframework/cache/config/CustomInterceptorTests.java
  15. 6
      spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java
  16. 5
      spring-context/src/test/java/org/springframework/ejb/access/LocalSlsbInvokerInterceptorTests.java
  17. 14
      spring-context/src/test/java/org/springframework/ejb/access/SimpleRemoteSlsbInvokerInterceptorTests.java
  18. 7
      spring-context/src/test/java/org/springframework/jmx/export/MBeanExporterTests.java
  19. 41
      spring-context/src/test/java/org/springframework/scripting/groovy/GroovyAspectIntegrationTests.java
  20. 5
      spring-context/src/test/java/org/springframework/scripting/groovy/GroovyScriptFactoryTests.java
  21. 26
      spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java
  22. 10
      spring-expression/src/test/java/org/springframework/expression/spel/ConstructorInvocationTests.java
  23. 11
      spring-expression/src/test/java/org/springframework/expression/spel/MethodInvocationTests.java
  24. 23
      spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java
  25. 62
      spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java
  26. 6
      spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateTests.java
  27. 5
      spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java
  28. 419
      spring-jdbc/src/test/java/org/springframework/jdbc/support/JdbcTransactionManagerTests.java
  29. 24
      spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethodTests.java
  30. 23
      spring-orm/src/test/java/org/springframework/orm/jpa/AbstractContainerEntityManagerFactoryIntegrationTests.java
  31. 258
      spring-orm/src/test/java/org/springframework/orm/jpa/JpaTransactionManagerTests.java
  32. 10
      spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceXmlParsingTests.java
  33. 8
      spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerIntegrationTests.java
  34. 7
      spring-test/src/test/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerTests.java
  35. 27
      spring-tx/src/test/java/org/springframework/dao/annotation/PersistenceExceptionTranslationAdvisorTests.java
  36. 48
      spring-tx/src/test/java/org/springframework/transaction/TransactionSupportTests.java
  37. 12
      spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionInterceptorTests.java
  38. 6
      spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java
  39. 6
      spring-tx/src/test/java/org/springframework/transaction/event/TransactionalApplicationListenerAdapterTests.java
  40. 6
      spring-tx/src/test/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapterTests.java
  41. 18
      spring-web/src/test/java/org/springframework/remoting/jaxws/JaxWsSupportTests.java
  42. 32
      spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerTests.java
  43. 7
      spring-web/src/test/java/org/springframework/web/method/annotation/WebArgumentResolverAdapterTests.java
  44. 24
      spring-web/src/test/java/org/springframework/web/method/support/InvocableHandlerMethodTests.java
  45. 16
      spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolutionIntegrationTests.java

8
integration-tests/src/test/java/org/springframework/aop/config/AopNamespaceHandlerAdviceOrderIntegrationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,7 +28,7 @@ import org.springframework.test.annotation.DirtiesContext; @@ -28,7 +28,7 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Integration tests for advice invocation order for advice configured via the
@ -52,7 +52,7 @@ class AopNamespaceHandlerAdviceOrderIntegrationTests { @@ -52,7 +52,7 @@ class AopNamespaceHandlerAdviceOrderIntegrationTests {
assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after", "after returning");
aspect.invocations.clear();
assertThatExceptionOfType(Exception.class).isThrownBy(() -> echo.echo(new Exception()));
assertThatException().isThrownBy(() -> echo.echo(new Exception()));
assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after", "after throwing");
}
}
@ -69,7 +69,7 @@ class AopNamespaceHandlerAdviceOrderIntegrationTests { @@ -69,7 +69,7 @@ class AopNamespaceHandlerAdviceOrderIntegrationTests {
assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after returning", "after");
aspect.invocations.clear();
assertThatExceptionOfType(Exception.class).isThrownBy(() -> echo.echo(new Exception()));
assertThatException().isThrownBy(() -> echo.echo(new Exception()));
assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after throwing", "after");
}
}

10
integration-tests/src/test/java/org/springframework/aop/framework/autoproxy/AspectJAutoProxyAdviceOrderIntegrationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,7 +38,7 @@ import org.springframework.test.annotation.DirtiesContext; @@ -38,7 +38,7 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Integration tests for advice invocation order for advice configured via
@ -65,8 +65,7 @@ class AspectJAutoProxyAdviceOrderIntegrationTests { @@ -65,8 +65,7 @@ class AspectJAutoProxyAdviceOrderIntegrationTests {
assertThat(aspect.invocations).containsExactly("around - start", "before", "after returning", "after", "around - end");
aspect.invocations.clear();
assertThatExceptionOfType(Exception.class).isThrownBy(
() -> echo.echo(new Exception()));
assertThatException().isThrownBy(() -> echo.echo(new Exception()));
assertThat(aspect.invocations).containsExactly("around - start", "before", "after throwing", "after", "around - end");
}
}
@ -95,8 +94,7 @@ class AspectJAutoProxyAdviceOrderIntegrationTests { @@ -95,8 +94,7 @@ class AspectJAutoProxyAdviceOrderIntegrationTests {
assertThat(aspect.invocations).containsExactly("around - start", "before", "after returning", "after", "around - end");
aspect.invocations.clear();
assertThatExceptionOfType(Exception.class).isThrownBy(
() -> echo.echo(new Exception()));
assertThatException().isThrownBy(() -> echo.echo(new Exception()));
assertThat(aspect.invocations).containsExactly("around - start", "before", "after throwing", "after", "around - end");
}
}

7
integration-tests/src/test/java/org/springframework/cache/annotation/EnableCachingIntegrationTests.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,7 +34,7 @@ import org.springframework.context.annotation.Configuration; @@ -34,7 +34,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Repository;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Integration tests for the @EnableCaching annotation.
@ -62,8 +62,7 @@ class EnableCachingIntegrationTests { @@ -62,8 +62,7 @@ class EnableCachingIntegrationTests {
// this test is a bit fragile, but gets the job done, proving that an
// attempt was made to look up the AJ aspect. It's due to classpath issues
// in .integration-tests that it's not found.
assertThatExceptionOfType(Exception.class).isThrownBy(
ctx::refresh)
assertThatException().isThrownBy(ctx::refresh)
.withMessageContaining("AspectJCachingConfiguration");
}

6
integration-tests/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementIntegrationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -45,7 +45,7 @@ import org.springframework.transaction.interceptor.BeanFactoryTransactionAttribu @@ -45,7 +45,7 @@ import org.springframework.transaction.interceptor.BeanFactoryTransactionAttribu
import org.springframework.transaction.testfixture.CallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Integration tests for the @EnableTransactionManagement annotation.
@ -98,7 +98,7 @@ class EnableTransactionManagementIntegrationTests { @@ -98,7 +98,7 @@ class EnableTransactionManagementIntegrationTests {
// this test is a bit fragile, but gets the job done, proving that an
// attempt was made to look up the AJ aspect. It's due to classpath issues
// in .integration-tests that it's not found.
assertThatExceptionOfType(Exception.class)
assertThatException()
.isThrownBy(ctx::refresh)
.withMessageContaining("AspectJJtaTransactionManagementConfiguration");
}

6
spring-aop/src/test/java/org/springframework/aop/framework/ProxyFactoryTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -44,7 +44,7 @@ import org.springframework.core.annotation.Order; @@ -44,7 +44,7 @@ import org.springframework.core.annotation.Order;
import org.springframework.core.testfixture.TimeStamped;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Also tests AdvisedSupport and ProxyCreatorSupport superclasses.
@ -277,7 +277,7 @@ public class ProxyFactoryTests { @@ -277,7 +277,7 @@ public class ProxyFactoryTests {
assertThat(config.getAdvisors().length == oldCount).isTrue();
assertThatExceptionOfType(RuntimeException.class)
assertThatRuntimeException()
.as("Existing object won't implement this interface any more")
.isThrownBy(ts::getTimeStamp); // Existing reference will fail

7
spring-aop/src/test/java/org/springframework/aop/framework/adapter/ThrowsAdviceInterceptorTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test; @@ -26,6 +26,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.aop.testfixture.advice.MyThrowsHandler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
@ -63,9 +64,7 @@ public class ThrowsAdviceInterceptorTests { @@ -63,9 +64,7 @@ public class ThrowsAdviceInterceptorTests {
Exception ex = new Exception();
MethodInvocation mi = mock(MethodInvocation.class);
given(mi.proceed()).willThrow(ex);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
ti.invoke(mi))
.isSameAs(ex);
assertThatException().isThrownBy(() -> ti.invoke(mi)).isSameAs(ex);
assertThat(th.getCalls()).isEqualTo(0);
}

29
spring-aspects/src/test/java/org/springframework/transaction/aspectj/TransactionAspectTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,7 +22,9 @@ import org.junit.jupiter.api.Test; @@ -22,7 +22,9 @@ import org.junit.jupiter.api.Test;
import org.springframework.transaction.testfixture.CallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Rod Johnson
@ -104,44 +106,44 @@ public class TransactionAspectTests { @@ -104,44 +106,44 @@ public class TransactionAspectTests {
@Test
public void defaultCommitOnAnnotatedClass() throws Throwable {
Exception ex = new Exception();
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), false))
assertThatException()
.isThrownBy(() -> testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), false))
.isSameAs(ex);
}
@Test
public void defaultRollbackOnAnnotatedClass() throws Throwable {
RuntimeException ex = new RuntimeException();
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), true))
assertThatRuntimeException()
.isThrownBy(() -> testRollback(() -> annotationOnlyOnClassWithNoInterface.echo(ex), true))
.isSameAs(ex);
}
@Test
public void defaultCommitOnSubclassOfAnnotatedClass() throws Throwable {
Exception ex = new Exception();
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
testRollback(() -> new SubclassOfClassWithTransactionalAnnotation().echo(ex), false))
assertThatException()
.isThrownBy(() -> testRollback(() -> new SubclassOfClassWithTransactionalAnnotation().echo(ex), false))
.isSameAs(ex);
}
@Test
public void defaultCommitOnSubclassOfClassWithTransactionalMethodAnnotated() throws Throwable {
Exception ex = new Exception();
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
testRollback(() -> new SubclassOfClassWithTransactionalMethodAnnotation().echo(ex), false))
assertThatException()
.isThrownBy(() -> testRollback(() -> new SubclassOfClassWithTransactionalMethodAnnotation().echo(ex), false))
.isSameAs(ex);
}
@Test
public void noCommitOnImplementationOfAnnotatedInterface() throws Throwable {
final Exception ex = new Exception();
Exception ex = new Exception();
testNotTransactional(() -> new ImplementsAnnotatedInterface().echo(ex), ex);
}
@Test
public void noRollbackOnImplementationOfAnnotatedInterface() throws Throwable {
final Exception rollbackProvokingException = new RuntimeException();
Exception rollbackProvokingException = new RuntimeException();
testNotTransactional(() -> new ImplementsAnnotatedInterface().echo(rollbackProvokingException),
rollbackProvokingException);
}
@ -165,8 +167,9 @@ public class TransactionAspectTests { @@ -165,8 +167,9 @@ public class TransactionAspectTests {
protected void testNotTransactional(TransactionOperationCallback toc, Throwable expected) throws Throwable {
txManager.clear();
assertThat(txManager.begun).isEqualTo(0);
assertThatExceptionOfType(Throwable.class).isThrownBy(
toc::performTransactionalOperation).isSameAs(expected);
assertThatExceptionOfType(Throwable.class)
.isThrownBy(toc::performTransactionalOperation)
.isSameAs(expected);
assertThat(txManager.begun).isEqualTo(0);
}

5
spring-beans/src/test/java/org/springframework/beans/factory/support/security/CallbacksSecurityTests.java

@ -58,6 +58,7 @@ import org.springframework.core.io.Resource; @@ -58,6 +58,7 @@ import org.springframework.core.io.Resource;
import org.springframework.core.testfixture.security.TestPrincipal;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
@ -303,14 +304,14 @@ public class CallbacksSecurityTests { @@ -303,14 +304,14 @@ public class CallbacksSecurityTests {
Method method = bean.getClass().getMethod("destroy");
method.setAccessible(true);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
assertThatException().isThrownBy(() ->
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
method.invoke(bean);
return null;
}, acc));
Class<ConstructorBean> cl = ConstructorBean.class;
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
assertThatException().isThrownBy(() ->
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () ->
cl.newInstance(), acc));
}

14
spring-beans/src/test/java/org/springframework/beans/factory/xml/DuplicateBeanIdTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,8 +23,7 @@ import org.springframework.beans.testfixture.beans.TestBean; @@ -23,8 +23,7 @@ import org.springframework.beans.testfixture.beans.TestBean;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* With Spring 3.1, bean id attributes (and all other id attributes across the
@ -40,22 +39,23 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -40,22 +39,23 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @see org.springframework.beans.factory.xml.XmlBeanFactoryTests#withDuplicateName
* @see org.springframework.beans.factory.xml.XmlBeanFactoryTests#withDuplicateNameInAlias
*/
public class DuplicateBeanIdTests {
class DuplicateBeanIdTests {
@Test
public void duplicateBeanIdsWithinSameNestingLevelRaisesError() {
void duplicateBeanIdsWithinSameNestingLevelRaisesError() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
assertThatExceptionOfType(Exception.class).as("duplicate ids in same nesting level").isThrownBy(() ->
assertThatException().as("duplicate ids in same nesting level").isThrownBy(() ->
reader.loadBeanDefinitions(new ClassPathResource("DuplicateBeanIdTests-sameLevel-context.xml", this.getClass())));
}
@Test
public void duplicateBeanIdsAcrossNestingLevels() {
void duplicateBeanIdsAcrossNestingLevels() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
reader.loadBeanDefinitions(new ClassPathResource("DuplicateBeanIdTests-multiLevel-context.xml", this.getClass()));
TestBean testBean = bf.getBean(TestBean.class); // there should be only one
assertThat(testBean.getName()).isEqualTo("nested");
}
}

8
spring-context-support/src/test/java/org/springframework/cache/jcache/config/JCacheCustomInterceptorTests.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -40,7 +40,7 @@ import org.springframework.context.annotation.Configuration; @@ -40,7 +40,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.contextsupport.testfixture.jcache.JCacheableService;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Stephane Nicoll
@ -86,8 +86,8 @@ public class JCacheCustomInterceptorTests { @@ -86,8 +86,8 @@ public class JCacheCustomInterceptorTests {
@Test
public void customInterceptorAppliesWithCheckedException() {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
cs.cacheWithCheckedException("id", true))
assertThatRuntimeException()
.isThrownBy(() -> cs.cacheWithCheckedException("id", true))
.withCauseExactlyInstanceOf(IOException.class);
}

6
spring-context/src/test/java/org/springframework/aop/aspectj/autoproxy/spr3064/SPR3064Tests.java

@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test; @@ -26,7 +26,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Adrian Colyer
@ -35,12 +35,12 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -35,12 +35,12 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
class SPR3064Tests {
@Test
void testServiceIsAdvised() {
void serviceIsAdvised() {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());
Service service = ctx.getBean(Service.class);
assertThatExceptionOfType(RuntimeException.class)
assertThatRuntimeException()
.isThrownBy(service::serveMe)
.withMessage("advice invoked");

57
spring-context/src/test/java/org/springframework/aop/framework/AbstractAopProxyTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -74,9 +74,11 @@ import org.springframework.core.testfixture.io.SerializationTestUtils; @@ -74,9 +74,11 @@ import org.springframework.core.testfixture.io.SerializationTestUtils;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Rod Johnson
@ -431,7 +433,7 @@ public abstract class AbstractAopProxyTests { @@ -431,7 +433,7 @@ public abstract class AbstractAopProxyTests {
pc.setTargetSource(mockTargetSource);
AopProxy aop = createAopProxy(pc);
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
assertThatException().isThrownBy(() -> {
ITestBean tb = (ITestBean) aop.getProxy();
// Note: exception param below isn't used
tb.exceptional(expectedException);
@ -459,8 +461,7 @@ public abstract class AbstractAopProxyTests { @@ -459,8 +461,7 @@ public abstract class AbstractAopProxyTests {
AopProxy aop = createAopProxy(pc);
ITestBean tb = (ITestBean) aop.getProxy();
assertThatExceptionOfType(UndeclaredThrowableException.class).isThrownBy(
tb::getAge)
assertThatExceptionOfType(UndeclaredThrowableException.class).isThrownBy(tb::getAge)
.satisfies(ex -> assertThat(ex.getUndeclaredThrowable()).isEqualTo(unexpectedException));
}
@ -480,8 +481,8 @@ public abstract class AbstractAopProxyTests { @@ -480,8 +481,8 @@ public abstract class AbstractAopProxyTests {
AopProxy aop = createAopProxy(pc);
ITestBean tb = (ITestBean) aop.getProxy();
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
tb::getAge)
assertThatRuntimeException()
.isThrownBy(tb::getAge)
.matches(unexpectedException::equals);
}
@ -571,8 +572,7 @@ public abstract class AbstractAopProxyTests { @@ -571,8 +572,7 @@ public abstract class AbstractAopProxyTests {
lockable.lock();
assertThat(itb.getAge()).isEqualTo(newAge);
assertThatExceptionOfType(LockedException.class).isThrownBy(() ->
itb.setAge(1));
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> itb.setAge(1));
assertThat(itb.getAge()).isEqualTo(newAge);
// Unlock
@ -675,7 +675,7 @@ public abstract class AbstractAopProxyTests { @@ -675,7 +675,7 @@ public abstract class AbstractAopProxyTests {
target.setAge(21);
ProxyFactory pc = new ProxyFactory(target);
pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class));
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
assertThatException().isThrownBy(() -> {
// TODO May fail on either call: may want to tighten up definition
ITestBean proxied = (ITestBean) createProxy(pc);
proxied.getName();
@ -725,8 +725,7 @@ public abstract class AbstractAopProxyTests { @@ -725,8 +725,7 @@ public abstract class AbstractAopProxyTests {
pc.addAdvisor(new DefaultIntroductionAdvisor(new MyDi()));
TimeStamped ts = (TimeStamped) createProxy(pc);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(
ts::getTimeStamp);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(ts::getTimeStamp);
}
/**
@ -754,8 +753,8 @@ public abstract class AbstractAopProxyTests { @@ -754,8 +753,8 @@ public abstract class AbstractAopProxyTests {
pc.addAdvice(new NopInterceptor());
ITestBean proxied = (ITestBean) createProxy(pc);
pc.setFrozen(true);
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add interceptor when frozen").isThrownBy(() ->
pc.addAdvice(0, new NopInterceptor()))
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add interceptor when frozen")
.isThrownBy(() -> pc.addAdvice(0, new NopInterceptor()))
.withMessageContaining("frozen");
// Check it still works: proxy factory state shouldn't have been corrupted
assertThat(proxied.getAge()).isEqualTo(target.getAge());
@ -777,8 +776,8 @@ public abstract class AbstractAopProxyTests { @@ -777,8 +776,8 @@ public abstract class AbstractAopProxyTests {
Advised advised = (Advised) proxied;
assertThat(pc.isFrozen()).isTrue();
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add Advisor when frozen").isThrownBy(() ->
advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor())))
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to add Advisor when frozen")
.isThrownBy(() -> advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor())))
.withMessageContaining("frozen");
// Check it still works: proxy factory state shouldn't have been corrupted
assertThat(proxied.getAge()).isEqualTo(target.getAge());
@ -797,8 +796,8 @@ public abstract class AbstractAopProxyTests { @@ -797,8 +796,8 @@ public abstract class AbstractAopProxyTests {
Advised advised = (Advised) proxied;
assertThat(pc.isFrozen()).isTrue();
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to remove Advisor when frozen").isThrownBy(() ->
advised.removeAdvisor(0))
assertThatExceptionOfType(AopConfigException.class).as("Shouldn't be able to remove Advisor when frozen")
.isThrownBy(() -> advised.removeAdvisor(0))
.withMessageContaining("frozen");
// Didn't get removed
assertThat(advised.getAdvisors().length).isEqualTo(1);
@ -1311,8 +1310,8 @@ public abstract class AbstractAopProxyTests { @@ -1311,8 +1310,8 @@ public abstract class AbstractAopProxyTests {
assertThat(cca.getCalls()).isEqualTo(2);
assertThat(proxied.getAge()).isEqualTo(26);
assertThat(cca.getCalls()).isEqualTo(4);
assertThatExceptionOfType(SpecializedUncheckedException.class).as("Should have thrown CannotGetJdbcConnectionException").isThrownBy(() ->
proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException)null)));
assertThatExceptionOfType(SpecializedUncheckedException.class).as("Should have thrown CannotGetJdbcConnectionException")
.isThrownBy(() -> proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException)null)));
assertThat(cca.getCalls()).isEqualTo(6);
}
@ -1346,8 +1345,8 @@ public abstract class AbstractAopProxyTests { @@ -1346,8 +1345,8 @@ public abstract class AbstractAopProxyTests {
assertThat(nop1.getCount()).isEqualTo(1);
assertThat(nop2.getCount()).isEqualTo(1);
// Will fail, after invoking Nop1
assertThatExceptionOfType(RuntimeException.class).as("before advice should have ended chain").isThrownBy(() ->
proxied.setAge(26))
assertThatRuntimeException().as("before advice should have ended chain")
.isThrownBy(() -> proxied.setAge(26))
.matches(rex::equals);
assertThat(ba.getCalls()).isEqualTo(2);
assertThat(nop1.getCount()).isEqualTo(2);
@ -1411,8 +1410,7 @@ public abstract class AbstractAopProxyTests { @@ -1411,8 +1410,7 @@ public abstract class AbstractAopProxyTests {
assertThat(car.getCalls()).isEqualTo(2);
Exception exc = new Exception();
// On exception it won't be invoked
assertThatExceptionOfType(Throwable.class).isThrownBy(() ->
proxied.exceptional(exc))
assertThatExceptionOfType(Throwable.class).isThrownBy(() -> proxied.exceptional(exc))
.satisfies(ex -> assertThat(ex).isSameAs(exc));
assertThat(car.getCalls()).isEqualTo(2);
}
@ -1442,12 +1440,11 @@ public abstract class AbstractAopProxyTests { @@ -1442,12 +1440,11 @@ public abstract class AbstractAopProxyTests {
assertThat(th.getCalls()).isEqualTo(0);
Exception ex = new Exception();
// Will be advised but doesn't match
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
proxied.echoException(1, ex))
assertThatException().isThrownBy(() -> proxied.echoException(1, ex))
.matches(ex::equals);
FileNotFoundException fex = new FileNotFoundException();
assertThatExceptionOfType(FileNotFoundException.class).isThrownBy(() ->
proxied.echoException(1, fex))
assertThatExceptionOfType(FileNotFoundException.class)
.isThrownBy(() -> proxied.echoException(1, fex))
.matches(fex::equals);
assertThat(th.getCalls("ioException")).isEqualTo(1);
}
@ -1468,14 +1465,12 @@ public abstract class AbstractAopProxyTests { @@ -1468,14 +1465,12 @@ public abstract class AbstractAopProxyTests {
assertThat(th.getCalls()).isEqualTo(0);
Exception ex = new Exception();
// Will be advised but doesn't match
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
proxied.echoException(1, ex))
assertThatException().isThrownBy(() -> proxied.echoException(1, ex))
.matches(ex::equals);
// Subclass of RemoteException
MarshalException mex = new MarshalException("");
assertThatExceptionOfType(MarshalException.class).isThrownBy(() ->
proxied.echoException(1, mex))
assertThatExceptionOfType(MarshalException.class).isThrownBy(() -> proxied.echoException(1, mex))
.matches(mex::equals);
assertThat(th.getCalls("remoteException")).isEqualTo(1);

14
spring-context/src/test/java/org/springframework/aop/framework/ProxyFactoryBeanTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -61,6 +61,7 @@ import org.springframework.core.testfixture.io.SerializationTestUtils; @@ -61,6 +61,7 @@ import org.springframework.core.testfixture.io.SerializationTestUtils;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIOException;
@ -310,7 +311,7 @@ public class ProxyFactoryBeanTests { @@ -310,7 +311,7 @@ public class ProxyFactoryBeanTests {
assertThat(config.getAdvisors().length).as("Have correct advisor count").isEqualTo(2);
ITestBean tb1 = (ITestBean) factory.getBean("test1");
assertThatExceptionOfType(Exception.class)
assertThatException()
.isThrownBy(tb1::toString)
.isSameAs(ex);
}
@ -438,8 +439,7 @@ public class ProxyFactoryBeanTests { @@ -438,8 +439,7 @@ public class ProxyFactoryBeanTests {
assertThat(cba.getCalls()).isEqualTo(2);
assertThat(th.getCalls()).isEqualTo(0);
Exception expected = new Exception();
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
echo.echoException(1, expected))
assertThatException().isThrownBy(() -> echo.echoException(1, expected))
.matches(expected::equals);
// No throws handler method: count should still be 0
assertThat(th.getCalls()).isEqualTo(0);
@ -586,8 +586,7 @@ public class ProxyFactoryBeanTests { @@ -586,8 +586,7 @@ public class ProxyFactoryBeanTests {
((Lockable) bean1).lock();
assertThatExceptionOfType(LockedException.class).isThrownBy(() ->
bean1.setAge(5));
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> bean1.setAge(5));
bean2.setAge(6); //do not expect LockedException"
}
@ -607,8 +606,7 @@ public class ProxyFactoryBeanTests { @@ -607,8 +606,7 @@ public class ProxyFactoryBeanTests {
((Lockable) bean1).lock();
assertThatExceptionOfType(LockedException.class).isThrownBy(() ->
bean1.setAge(5));
assertThatExceptionOfType(LockedException.class).isThrownBy(() -> bean1.setAge(5));
// do not expect LockedException
bean2.setAge(6);

8
spring-context/src/test/java/org/springframework/cache/config/CustomInterceptorTests.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -37,7 +37,7 @@ import org.springframework.context.testfixture.cache.beans.CacheableService; @@ -37,7 +37,7 @@ import org.springframework.context.testfixture.cache.beans.CacheableService;
import org.springframework.context.testfixture.cache.beans.DefaultCacheableService;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Stephane Nicoll
@ -76,8 +76,8 @@ public class CustomInterceptorTests { @@ -76,8 +76,8 @@ public class CustomInterceptorTests {
@Test
public void customInterceptorAppliesWithCheckedException() {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
this.cs.throwChecked(0L))
assertThatRuntimeException()
.isThrownBy(() -> this.cs.throwChecked(0L))
.withCauseExactlyInstanceOf(IOException.class);
}

6
spring-context/src/test/java/org/springframework/context/event/ApplicationContextEventTests.java

@ -52,7 +52,7 @@ import org.springframework.scheduling.support.TaskUtils; @@ -52,7 +52,7 @@ import org.springframework.scheduling.support.TaskUtils;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
@ -161,8 +161,8 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen @@ -161,8 +161,8 @@ public class ApplicationContextEventTests extends AbstractApplicationEventListen
RuntimeException thrown = new RuntimeException();
willThrow(thrown).given(listener).onApplicationEvent(evt);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
smc.multicastEvent(evt))
assertThatRuntimeException()
.isThrownBy(() -> smc.multicastEvent(evt))
.satisfies(ex -> assertThat(ex).isSameAs(thrown));
}

5
spring-context/src/test/java/org/springframework/ejb/access/LocalSlsbInvokerInterceptorTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import org.springframework.aop.framework.ProxyFactory; @@ -28,6 +28,7 @@ import org.springframework.aop.framework.ProxyFactory;
import org.springframework.jndi.JndiTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@ -132,7 +133,7 @@ public class LocalSlsbInvokerInterceptorTests { @@ -132,7 +133,7 @@ public class LocalSlsbInvokerInterceptorTests {
pf.addAdvice(si);
LocalInterfaceWithBusinessMethods target = (LocalInterfaceWithBusinessMethods) pf.getProxy();
assertThatExceptionOfType(Exception.class)
assertThatException()
.isThrownBy(target::targetMethod)
.isSameAs(expected);

14
spring-context/src/test/java/org/springframework/ejb/access/SimpleRemoteSlsbInvokerInterceptorTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,6 +32,7 @@ import org.springframework.jndi.JndiTemplate; @@ -32,6 +32,7 @@ import org.springframework.jndi.JndiTemplate;
import org.springframework.remoting.RemoteAccessException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@ -195,8 +196,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests { @@ -195,8 +196,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
assertThatExceptionOfType(RemoteException.class).isThrownBy(
target::targetMethod);
assertThatExceptionOfType(RemoteException.class).isThrownBy(target::targetMethod);
verify(mockContext).close();
verify(ejb, times(2)).remove();
@ -245,8 +245,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests { @@ -245,8 +245,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
si.setCacheHome(cacheHome);
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
assertThatExceptionOfType(ConnectException.class).isThrownBy(
target::targetMethod);
assertThatExceptionOfType(ConnectException.class).isThrownBy(target::targetMethod);
verify(mockContext, times(lookupCount)).close();
verify(ejb, times(2)).remove();
@ -281,8 +280,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests { @@ -281,8 +280,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
BusinessInterface target = (BusinessInterface) configuredProxy(si, BusinessInterface.class);
assertThatExceptionOfType(RemoteAccessException.class).isThrownBy(
target::targetMethod);
assertThatExceptionOfType(RemoteAccessException.class).isThrownBy(target::targetMethod);
verify(mockContext).close();
verify(ejb).remove();
@ -308,7 +306,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests { @@ -308,7 +306,7 @@ public class SimpleRemoteSlsbInvokerInterceptorTests {
SimpleRemoteSlsbInvokerInterceptor si = configuredInterceptor(mockContext, jndiName);
RemoteInterface target = (RemoteInterface) configuredProxy(si, RemoteInterface.class);
assertThatExceptionOfType(Exception.class)
assertThatException()
.isThrownBy(target::targetMethod)
.isSameAs(expected);
verify(mockContext).close();

7
spring-context/src/test/java/org/springframework/jmx/export/MBeanExporterTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -59,6 +59,7 @@ import org.springframework.jmx.support.RegistrationPolicy; @@ -59,6 +59,7 @@ import org.springframework.jmx.support.RegistrationPolicy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Integration tests for the {@link MBeanExporter} class.
@ -589,8 +590,8 @@ public class MBeanExporterTests extends AbstractMBeanServerTests { @@ -589,8 +590,8 @@ public class MBeanExporterTests extends AbstractMBeanServerTests {
exporter.setBeans(beansToExport);
exporter.setBeanFactory(factory);
assertThatExceptionOfType(RuntimeException.class).as("failed during creation of RuntimeExceptionThrowingConstructorBean").isThrownBy(() ->
start(exporter));
assertThatRuntimeException().as("failed during creation of RuntimeExceptionThrowingConstructorBean")
.isThrownBy(() -> start(exporter));
assertIsNotRegistered("Must have unregistered all previously registered MBeans due to RuntimeException",
ObjectNameManager.getInstance(objectName1));

41
spring-context/src/test/java/org/springframework/scripting/groovy/GroovyAspectIntegrationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,51 +22,50 @@ import org.junit.jupiter.api.Test; @@ -22,51 +22,50 @@ import org.junit.jupiter.api.Test;
import org.springframework.context.support.GenericXmlApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Dave Syer
*/
public class GroovyAspectIntegrationTests {
class GroovyAspectIntegrationTests {
private GenericXmlApplicationContext context;
@Test
public void testJavaBean() {
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-java-context.xml");
void javaBean() {
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName() + "-java-context.xml");
TestService bean = context.getBean("javaBean", TestService.class);
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
assertThat(logAdvice.getCountThrows()).isEqualTo(0);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
bean::sayHello)
assertThatRuntimeException()
.isThrownBy(bean::sayHello)
.withMessage("TestServiceImpl");
assertThat(logAdvice.getCountThrows()).isEqualTo(1);
}
@Test
public void testGroovyBeanInterface() {
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-interface-context.xml");
void groovyBeanInterface() {
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName() + "-groovy-interface-context.xml");
TestService bean = context.getBean("groovyBean", TestService.class);
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
assertThat(logAdvice.getCountThrows()).isEqualTo(0);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
bean::sayHello)
assertThatRuntimeException()
.isThrownBy(bean::sayHello)
.withMessage("GroovyServiceImpl");
assertThat(logAdvice.getCountThrows()).isEqualTo(1);
}
@Test
public void testGroovyBeanDynamic() {
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-dynamic-context.xml");
void groovyBeanDynamic() {
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName() + "-groovy-dynamic-context.xml");
TestService bean = context.getBean("groovyBean", TestService.class);
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
assertThat(logAdvice.getCountThrows()).isEqualTo(0);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
bean::sayHello)
assertThatRuntimeException()
.isThrownBy(bean::sayHello)
.withMessage("GroovyServiceImpl");
// No proxy here because the pointcut only applies to the concrete class, not the interface
assertThat(logAdvice.getCountThrows()).isEqualTo(0);
@ -74,21 +73,21 @@ public class GroovyAspectIntegrationTests { @@ -74,21 +73,21 @@ public class GroovyAspectIntegrationTests {
}
@Test
public void testGroovyBeanProxyTargetClass() {
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName()+"-groovy-proxy-target-class-context.xml");
void groovyBeanProxyTargetClass() {
context = new GenericXmlApplicationContext(getClass(), getClass().getSimpleName() + "-groovy-proxy-target-class-context.xml");
TestService bean = context.getBean("groovyBean", TestService.class);
LogUserAdvice logAdvice = context.getBean(LogUserAdvice.class);
assertThat(logAdvice.getCountThrows()).isEqualTo(0);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
bean::sayHello)
assertThatRuntimeException()
.isThrownBy(bean::sayHello)
.withMessage("GroovyServiceImpl");
assertThat(logAdvice.getCountBefore()).isEqualTo(1);
assertThat(logAdvice.getCountThrows()).isEqualTo(1);
}
@AfterEach
public void close() {
void close() {
if (context != null) {
context.close();
}

5
spring-context/src/test/java/org/springframework/scripting/groovy/GroovyScriptFactoryTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -46,6 +46,7 @@ import org.springframework.stereotype.Component; @@ -46,6 +46,7 @@ import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@ -318,7 +319,7 @@ public class GroovyScriptFactoryTests { @@ -318,7 +319,7 @@ public class GroovyScriptFactoryTests {
@Test
public void testWithTwoClassesDefinedInTheOneGroovyFile_WrongClassFirst() throws Exception {
assertThatExceptionOfType(Exception.class).as("two classes defined in GroovyScriptFactory source, non-Messenger class defined first").isThrownBy(() -> {
assertThatException().as("two classes defined in GroovyScriptFactory source, non-Messenger class defined first").isThrownBy(() -> {
ApplicationContext ctx = new ClassPathXmlApplicationContext("twoClassesWrongOneFirst.xml", getClass());
ctx.getBean("messenger", Messenger.class);
});

26
spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferTests.java

@ -25,6 +25,7 @@ import java.util.Arrays; @@ -25,6 +25,7 @@ import java.util.Arrays;
import org.springframework.core.testfixture.io.buffer.AbstractDataBufferAllocatingTests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@ -83,8 +84,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { @@ -83,8 +84,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests {
DataBuffer buffer = createDataBuffer(1);
try {
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() ->
buffer.readPosition(-1));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> buffer.readPosition(-1));
}
finally {
release(buffer);
@ -97,8 +97,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { @@ -97,8 +97,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests {
DataBuffer buffer = createDataBuffer(1);
try {
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() ->
buffer.readPosition(1));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> buffer.readPosition(1));
}
finally {
release(buffer);
@ -113,8 +112,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { @@ -113,8 +112,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests {
try {
buffer.write((byte) 'a');
buffer.read();
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() ->
buffer.writePosition(0));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> buffer.writePosition(0));
}
finally {
release(buffer);
@ -127,8 +125,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { @@ -127,8 +125,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests {
DataBuffer buffer = createDataBuffer(1);
try {
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() ->
buffer.writePosition(2));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> buffer.writePosition(2));
}
finally {
release(buffer);
@ -641,8 +638,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { @@ -641,8 +638,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests {
DataBuffer slice = buffer.slice(1, 2);
assertThat(slice.readableByteCount()).isEqualTo(2);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
slice.write((byte) 0));
assertThatException().isThrownBy(() -> slice.write((byte) 0));
buffer.write((byte) 'c');
assertThat(buffer.readableByteCount()).isEqualTo(3);
@ -670,8 +666,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { @@ -670,8 +666,7 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests {
DataBuffer slice = buffer.retainedSlice(1, 2);
assertThat(slice.readableByteCount()).isEqualTo(2);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
slice.write((byte) 0));
assertThatException().isThrownBy(() -> slice.write((byte) 0));
buffer.write((byte) 'c');
assertThat(buffer.readableByteCount()).isEqualTo(3);
@ -734,11 +729,8 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests { @@ -734,11 +729,8 @@ class DataBufferTests extends AbstractDataBufferAllocatingTests {
assertThat(buffer.getByte(0)).isEqualTo((byte) 'a');
assertThat(buffer.getByte(1)).isEqualTo((byte) 'b');
assertThat(buffer.getByte(2)).isEqualTo((byte) 'c');
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() ->
buffer.getByte(-1));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() ->
buffer.getByte(3));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> buffer.getByte(-1));
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> buffer.getByte(3));
release(buffer);
}

10
spring-expression/src/test/java/org/springframework/expression/spel/ConstructorInvocationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -33,7 +33,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext; @@ -33,7 +33,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.testresources.PlaceOfBirth;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Tests invocation of constructors.
@ -127,8 +127,8 @@ public class ConstructorInvocationTests extends AbstractExpressionTests { @@ -127,8 +127,8 @@ public class ConstructorInvocationTests extends AbstractExpressionTests {
// 4 will make it throw a checked exception - this will be wrapped by spel on the
// way out
eContext.setVariable("bar", 4);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
expr.getValue(eContext))
assertThatException()
.isThrownBy(() -> expr.getValue(eContext))
.withMessageContaining("Tester");
// A problem occurred whilst attempting to construct an object of type
// 'org.springframework.expression.spel.ConstructorInvocationTests$Tester'
@ -139,7 +139,7 @@ public class ConstructorInvocationTests extends AbstractExpressionTests { @@ -139,7 +139,7 @@ public class ConstructorInvocationTests extends AbstractExpressionTests {
// 1 will make it throw a RuntimeException - SpEL will let this through
eContext.setVariable("bar", 1);
assertThatExceptionOfType(Exception.class)
assertThatException()
.isThrownBy(() -> expr.getValue(eContext))
.isNotInstanceOf(SpelEvaluationException.class);
// A problem occurred whilst attempting to construct an object of type

11
spring-expression/src/test/java/org/springframework/expression/spel/MethodInvocationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,6 +39,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext; @@ -39,6 +39,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.testresources.PlaceOfBirth;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
@ -121,7 +122,8 @@ public class MethodInvocationTests extends AbstractExpressionTests { @@ -121,7 +122,8 @@ public class MethodInvocationTests extends AbstractExpressionTests {
// Now cause it to throw an exception:
eContext.setVariable("bar", 1);
assertThatExceptionOfType(Exception.class).isThrownBy(() -> expr.getValue(eContext))
assertThatException()
.isThrownBy(() -> expr.getValue(eContext))
.isNotInstanceOf(SpelEvaluationException.class);
// If counter is 4 then the method got called twice!
@ -149,7 +151,7 @@ public class MethodInvocationTests extends AbstractExpressionTests { @@ -149,7 +151,7 @@ public class MethodInvocationTests extends AbstractExpressionTests {
Expression expr = parser.parseExpression("throwException(#bar)");
context.setVariable("bar", 2);
assertThatExceptionOfType(Exception.class)
assertThatException()
.isThrownBy(() -> expr.getValue(context))
.isNotInstanceOf(SpelEvaluationException.class);
}
@ -166,7 +168,8 @@ public class MethodInvocationTests extends AbstractExpressionTests { @@ -166,7 +168,8 @@ public class MethodInvocationTests extends AbstractExpressionTests {
Expression expr = parser.parseExpression("throwException(#bar)");
context.setVariable("bar", 4);
assertThatExceptionOfType(ExpressionInvocationTargetException.class).isThrownBy(() -> expr.getValue(context))
assertThatExceptionOfType(ExpressionInvocationTargetException.class)
.isThrownBy(() -> expr.getValue(context))
.satisfies(ex -> assertThat(ex.getCause().getClass().getName()).isEqualTo(
"org.springframework.expression.spel.testresources.Inventor$TestException"));
}

23
spring-expression/src/test/java/org/springframework/expression/spel/SpelCompilationCoverageTests.java

@ -46,6 +46,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext; @@ -46,6 +46,7 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.testdata.PersonInOtherPackage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.within;
import static org.assertj.core.api.InstanceOfAssertFactories.BOOLEAN;
@ -1252,8 +1253,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests { @@ -1252,8 +1253,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
ctx.setVariable("target", "123");
assertThat(expression.getValue(ctx)).isEqualTo("123");
ctx.setVariable("target", 42);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(ctx))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(ctx))
.withCauseInstanceOf(ClassCastException.class);
ctx.setVariable("target", "abc");
@ -1264,8 +1265,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests { @@ -1264,8 +1265,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
ctx.setVariable("target", "1");
assertThat(expression.getValue(ctx)).isEqualTo('1');
ctx.setVariable("target", 42);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(ctx))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(ctx))
.withCauseInstanceOf(ClassCastException.class);
}
@ -3998,8 +3999,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests { @@ -3998,8 +3999,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
assertThat(expression.getValue(is)).isEqualTo(2);
assertCanCompile(expression);
assertThat(expression.getValue(is)).isEqualTo(2);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(strings))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(strings))
.withCauseInstanceOf(ClassCastException.class);
SpelCompiler.revertToInterpreted(expression);
assertThat(expression.getValue(strings)).isEqualTo("b");
@ -4026,8 +4027,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests { @@ -4026,8 +4027,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
assertCanCompile(expression);
tc.reset();
tc.obj=42;
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(tc))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(tc))
.withCauseInstanceOf(ClassCastException.class);
@ -4035,8 +4036,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests { @@ -4035,8 +4036,8 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
expression = parser.parseExpression("#root.charAt(0)");
assertThat(expression.getValue("abc")).isEqualTo('a');
assertCanCompile(expression);
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(42))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(42))
.withCauseInstanceOf(ClassCastException.class);
}
@ -5148,7 +5149,7 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests { @@ -5148,7 +5149,7 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
}
private void assertGetValueFail(Expression expression) {
assertThatExceptionOfType(Exception.class).isThrownBy(expression::getValue);
assertThatException().isThrownBy(expression::getValue);
}
public static void assertIsCompiled(Expression expression) {

62
spring-expression/src/test/java/org/springframework/expression/spel/SpelReproTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -62,6 +62,7 @@ import org.springframework.lang.Nullable; @@ -62,6 +62,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@ -104,8 +105,7 @@ class SpelReproTests extends AbstractExpressionTests { @@ -104,8 +105,7 @@ class SpelReproTests extends AbstractExpressionTests {
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(null)");
assertThat(expr.getValue(context)).isNull();
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
assertThatExceptionOfType(EvaluationException.class).isThrownBy(
expr::getValue);
assertThatExceptionOfType(EvaluationException.class).isThrownBy(expr::getValue);
context.setTypeLocator(new MyTypeLocator());
// varargs
@ -393,15 +393,15 @@ class SpelReproTests extends AbstractExpressionTests { @@ -393,15 +393,15 @@ class SpelReproTests extends AbstractExpressionTests {
private void checkTemplateParsingError(String expression, ParserContext context, String expectedMessage) {
SpelExpressionParser parser = new SpelExpressionParser();
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
parser.parseExpression(expression, context))
assertThatException()
.isThrownBy(() -> parser.parseExpression(expression, context))
.satisfies(ex -> {
String message = ex.getMessage();
if (ex instanceof ExpressionException) {
message = ((ExpressionException) ex).getSimpleMessage();
}
assertThat(message).isEqualTo(expectedMessage);
});
String message = ex.getMessage();
if (ex instanceof ExpressionException) {
message = ((ExpressionException) ex).getSimpleMessage();
}
assertThat(message).isEqualTo(expectedMessage);
});
}
@ -485,15 +485,15 @@ class SpelReproTests extends AbstractExpressionTests { @@ -485,15 +485,15 @@ class SpelReproTests extends AbstractExpressionTests {
assertThat(expr.getValue()).isNull();
// Different parts of ternary expression are null
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
new SpelExpressionParser().parseRaw("(?'abc':'default')").getValue(context))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> new SpelExpressionParser().parseRaw("(?'abc':'default')").getValue(context))
.satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.TYPE_CONVERSION_ERROR));
expr = new SpelExpressionParser().parseRaw("(false?'abc':null)");
assertThat(expr.getValue()).isNull();
// Assignment
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
new SpelExpressionParser().parseRaw("(='default')").getValue(context))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> new SpelExpressionParser().parseRaw("(='default')").getValue(context))
.satisfies(ex -> assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.SETVALUE_NOT_SUPPORTED));
}
@ -1232,8 +1232,8 @@ class SpelReproTests extends AbstractExpressionTests { @@ -1232,8 +1232,8 @@ class SpelReproTests extends AbstractExpressionTests {
void SPR16123() {
ExpressionParser parser = new SpelExpressionParser();
parser.parseExpression("simpleProperty").setValue(new BooleanHolder(), null);
assertThatExceptionOfType(EvaluationException.class).isThrownBy(() ->
parser.parseExpression("primitiveProperty").setValue(new BooleanHolder(), null));
assertThatExceptionOfType(EvaluationException.class)
.isThrownBy(() -> parser.parseExpression("primitiveProperty").setValue(new BooleanHolder(), null));
}
@Test
@ -1249,8 +1249,8 @@ class SpelReproTests extends AbstractExpressionTests { @@ -1249,8 +1249,8 @@ class SpelReproTests extends AbstractExpressionTests {
}
private void doTestSpr10146(String expression, String expectedMessage) {
assertThatExceptionOfType(SpelParseException.class).isThrownBy(() ->
new SpelExpressionParser().parseExpression(expression))
assertThatExceptionOfType(SpelParseException.class)
.isThrownBy(() -> new SpelExpressionParser().parseExpression(expression))
.withMessageContaining(expectedMessage);
}
@ -1275,8 +1275,8 @@ class SpelReproTests extends AbstractExpressionTests { @@ -1275,8 +1275,8 @@ class SpelReproTests extends AbstractExpressionTests {
@Test
void SPR10328() {
assertThatExceptionOfType(SpelParseException.class).isThrownBy(() ->
parser.parseExpression("$[]"))
assertThatExceptionOfType(SpelParseException.class)
.isThrownBy(() -> parser.parseExpression("$[]"))
.withMessageContaining("EL1071E: A required selection expression has not been specified");
}
@ -1363,8 +1363,8 @@ class SpelReproTests extends AbstractExpressionTests { @@ -1363,8 +1363,8 @@ class SpelReproTests extends AbstractExpressionTests {
StandardEvaluationContext context = new StandardEvaluationContext();
Spr11142 rootObject = new Spr11142();
Expression expression = parser.parseExpression("something");
assertThatExceptionOfType(SpelEvaluationException.class).isThrownBy(() ->
expression.getValue(context, rootObject))
assertThatExceptionOfType(SpelEvaluationException.class)
.isThrownBy(() -> expression.getValue(context, rootObject))
.withMessageContaining("'something' cannot be found");
}
@ -1535,19 +1535,19 @@ class SpelReproTests extends AbstractExpressionTests { @@ -1535,19 +1535,19 @@ class SpelReproTests extends AbstractExpressionTests {
expr = new SpelExpressionParser().parseRaw("&foo");
assertThat(expr.getValue(context)).isEqualTo("foo factory");
assertThatExceptionOfType(SpelParseException.class).isThrownBy(() ->
new SpelExpressionParser().parseRaw("&@foo"))
assertThatExceptionOfType(SpelParseException.class)
.isThrownBy(() -> new SpelExpressionParser().parseRaw("&@foo"))
.satisfies(ex -> {
assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.INVALID_BEAN_REFERENCE);
assertThat(ex.getPosition()).isEqualTo(0);
});
assertThatExceptionOfType(SpelParseException.class).isThrownBy(() ->
new SpelExpressionParser().parseRaw("@&foo"))
.satisfies(ex -> {
assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.INVALID_BEAN_REFERENCE);
assertThat(ex.getPosition()).isEqualTo(0);
});
assertThatExceptionOfType(SpelParseException.class)
.isThrownBy(() -> new SpelExpressionParser().parseRaw("@&foo"))
.satisfies(ex -> {
assertThat(ex.getMessageCode()).isEqualTo(SpelMessage.INVALID_BEAN_REFERENCE);
assertThat(ex.getPosition()).isEqualTo(0);
});
}
@Test

6
spring-jdbc/src/test/java/org/springframework/jdbc/core/JdbcTemplateTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -54,6 +54,7 @@ import org.springframework.util.StringUtils; @@ -54,6 +54,7 @@ import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
@ -320,7 +321,8 @@ public class JdbcTemplateTests { @@ -320,7 +321,8 @@ public class JdbcTemplateTests {
given(this.connection.createStatement()).willReturn(this.preparedStatement);
try {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
assertThatRuntimeException()
.isThrownBy(() ->
this.template.query(sql, (RowCallbackHandler) rs -> {
throw runtimeException;
}))

5
spring-jdbc/src/test/java/org/springframework/jdbc/datasource/DataSourceTransactionManagerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -52,6 +52,7 @@ import org.springframework.transaction.support.TransactionTemplate; @@ -52,6 +52,7 @@ import org.springframework.transaction.support.TransactionTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
@ -237,7 +238,7 @@ public class DataSourceTransactionManagerTests { @@ -237,7 +238,7 @@ public class DataSourceTransactionManagerTests {
assertThat(condition2).as("Synchronization not active").isTrue();
final RuntimeException ex = new RuntimeException("Application exception");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
assertThatRuntimeException().isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {

419
spring-jdbc/src/test/java/org/springframework/jdbc/support/JdbcTransactionManagerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -58,6 +58,7 @@ import org.springframework.transaction.support.TransactionTemplate; @@ -58,6 +58,7 @@ import org.springframework.transaction.support.TransactionTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
@ -144,10 +145,8 @@ public class JdbcTransactionManagerTests { @@ -144,10 +145,8 @@ public class JdbcTransactionManagerTests {
final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
tm = new JdbcTransactionManager(dsToUse);
TransactionTemplate tt = new TransactionTemplate(tm);
boolean condition3 = !TransactionSynchronizationManager.hasResource(dsToUse);
assertThat(condition3).as("Hasn't thread connection").isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(dsToUse)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -173,10 +172,8 @@ public class JdbcTransactionManagerTests { @@ -173,10 +172,8 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition1 = !TransactionSynchronizationManager.hasResource(dsToUse);
assertThat(condition1).as("Hasn't thread connection").isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(dsToUse)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
if (autoCommit && (!lazyConnection || createStatement)) {
InOrder ordered = inOrder(con);
@ -237,13 +234,11 @@ public class JdbcTransactionManagerTests { @@ -237,13 +234,11 @@ public class JdbcTransactionManagerTests {
final DataSource dsToUse = (lazyConnection ? new LazyConnectionDataSourceProxy(ds) : ds);
tm = new JdbcTransactionManager(dsToUse);
TransactionTemplate tt = new TransactionTemplate(tm);
boolean condition3 = !TransactionSynchronizationManager.hasResource(dsToUse);
assertThat(condition3).as("Hasn't thread connection").isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(dsToUse)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
final RuntimeException ex = new RuntimeException("Application exception");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
assertThatRuntimeException().isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
@ -264,10 +259,8 @@ public class JdbcTransactionManagerTests { @@ -264,10 +259,8 @@ public class JdbcTransactionManagerTests {
}))
.isEqualTo(ex);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
if (autoCommit && (!lazyConnection || createStatement)) {
InOrder ordered = inOrder(con);
@ -287,10 +280,8 @@ public class JdbcTransactionManagerTests { @@ -287,10 +280,8 @@ public class JdbcTransactionManagerTests {
public void testTransactionRollbackOnly() throws Exception {
tm.setTransactionSynchronization(JdbcTransactionManager.SYNCHRONIZATION_NEVER);
TransactionTemplate tt = new TransactionTemplate(tm);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
ConnectionHolder conHolder = new ConnectionHolder(con, true);
TransactionSynchronizationManager.bindResource(ds, conHolder);
@ -300,10 +291,8 @@ public class JdbcTransactionManagerTests { @@ -300,10 +291,8 @@ public class JdbcTransactionManagerTests {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Is existing transaction").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
assertThat(!status.isNewTransaction()).as("Is existing transaction").isTrue();
throw ex;
}
});
@ -311,16 +300,14 @@ public class JdbcTransactionManagerTests { @@ -311,16 +300,14 @@ public class JdbcTransactionManagerTests {
}
catch (RuntimeException ex2) {
// expected
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
assertThat(ex2).as("Correct exception thrown").isEqualTo(ex);
}
finally {
TransactionSynchronizationManager.unbindResource(ds);
}
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
}
@Test
@ -338,10 +325,8 @@ public class JdbcTransactionManagerTests { @@ -338,10 +325,8 @@ public class JdbcTransactionManagerTests {
if (failEarly) {
tm.setFailEarlyOnGlobalRollbackOnly(true);
}
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
TestTransactionSynchronization synch =
@ -356,21 +341,18 @@ public class JdbcTransactionManagerTests { @@ -356,21 +341,18 @@ public class JdbcTransactionManagerTests {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
boolean condition1 = !status.isNewTransaction();
assertThat(condition1).as("Is existing transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Is existing transaction").isTrue();
assertThat(status.isRollbackOnly()).as("Is not rollback-only").isFalse();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Is existing transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Is existing transaction").isTrue();
status.setRollbackOnly();
}
});
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Is existing transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Is existing transaction").isTrue();
assertThat(status.isRollbackOnly()).as("Is rollback-only").isTrue();
}
});
@ -393,8 +375,7 @@ public class JdbcTransactionManagerTests { @@ -393,8 +375,7 @@ public class JdbcTransactionManagerTests {
}
}
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(synch.beforeCommitCalled).isFalse();
assertThat(synch.beforeCompletionCalled).isTrue();
assertThat(synch.afterCommitCalled).isFalse();
@ -407,10 +388,8 @@ public class JdbcTransactionManagerTests { @@ -407,10 +388,8 @@ public class JdbcTransactionManagerTests {
public void testParticipatingTransactionWithIncompatibleIsolationLevel() throws Exception {
tm.setValidateExistingTransaction(true);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> {
final TransactionTemplate tt = new TransactionTemplate(tm);
@ -432,8 +411,7 @@ public class JdbcTransactionManagerTests { @@ -432,8 +411,7 @@ public class JdbcTransactionManagerTests {
});
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).rollback();
verify(con).close();
}
@ -443,10 +421,8 @@ public class JdbcTransactionManagerTests { @@ -443,10 +421,8 @@ public class JdbcTransactionManagerTests {
willThrow(new SQLException("read-only not supported")).given(con).setReadOnly(true);
tm.setValidateExistingTransaction(true);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() -> {
final TransactionTemplate tt = new TransactionTemplate(tm);
@ -469,18 +445,15 @@ public class JdbcTransactionManagerTests { @@ -469,18 +445,15 @@ public class JdbcTransactionManagerTests {
});
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).rollback();
verify(con).close();
}
@Test
public void testParticipatingTransactionWithTransactionStartedFromSynch() throws Exception {
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
@ -506,14 +479,12 @@ public class JdbcTransactionManagerTests { @@ -506,14 +479,12 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(synch.beforeCommitCalled).isTrue();
assertThat(synch.beforeCompletionCalled).isTrue();
assertThat(synch.afterCommitCalled).isTrue();
assertThat(synch.afterCompletionCalled).isTrue();
boolean condition3 = synch.afterCompletionException instanceof IllegalStateException;
assertThat(condition3).isTrue();
assertThat(synch.afterCompletionException instanceof IllegalStateException).isTrue();
verify(con, times(2)).commit();
verify(con, times(2)).close();
}
@ -524,10 +495,8 @@ public class JdbcTransactionManagerTests { @@ -524,10 +495,8 @@ public class JdbcTransactionManagerTests {
final Connection con2 = mock(Connection.class);
given(ds2.getConnection()).willReturn(con2);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
final TransactionTemplate tt = new TransactionTemplate(tm);
@ -548,8 +517,7 @@ public class JdbcTransactionManagerTests { @@ -548,8 +517,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(synch.beforeCommitCalled).isTrue();
assertThat(synch.beforeCompletionCalled).isTrue();
assertThat(synch.afterCommitCalled).isTrue();
@ -566,10 +534,8 @@ public class JdbcTransactionManagerTests { @@ -566,10 +534,8 @@ public class JdbcTransactionManagerTests {
JdbcTransactionManager tm2 = new JdbcTransactionManager(ds);
// tm has no synch enabled (used at outer level), tm2 has synch enabled (inner level)
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
final TestTransactionSynchronization synch =
@ -581,21 +547,18 @@ public class JdbcTransactionManagerTests { @@ -581,21 +547,18 @@ public class JdbcTransactionManagerTests {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
boolean condition1 = !status.isNewTransaction();
assertThat(condition1).as("Is existing transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Is existing transaction").isTrue();
assertThat(status.isRollbackOnly()).as("Is not rollback-only").isFalse();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Is existing transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Is existing transaction").isTrue();
status.setRollbackOnly();
}
});
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Is existing transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Is existing transaction").isTrue();
assertThat(status.isRollbackOnly()).as("Is rollback-only").isTrue();
TransactionSynchronizationManager.registerSynchronization(synch);
}
@ -604,8 +567,7 @@ public class JdbcTransactionManagerTests { @@ -604,8 +567,7 @@ public class JdbcTransactionManagerTests {
tm.commit(ts);
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(synch.beforeCommitCalled).isFalse();
assertThat(synch.beforeCompletionCalled).isTrue();
assertThat(synch.afterCommitCalled).isFalse();
@ -618,10 +580,8 @@ public class JdbcTransactionManagerTests { @@ -618,10 +580,8 @@ public class JdbcTransactionManagerTests {
public void testPropagationRequiresNewWithExistingTransaction() throws Exception {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -647,8 +607,7 @@ public class JdbcTransactionManagerTests { @@ -647,8 +607,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).rollback();
verify(con).commit();
verify(con, times(2)).close();
@ -667,12 +626,9 @@ public class JdbcTransactionManagerTests { @@ -667,12 +626,9 @@ public class JdbcTransactionManagerTests {
final TransactionTemplate tt2 = new TransactionTemplate(tm2);
tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
boolean condition4 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition4).as("Hasn't thread connection").isTrue();
boolean condition3 = !TransactionSynchronizationManager.hasResource(ds2);
assertThat(condition3).as("Hasn't thread connection").isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds2)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -698,10 +654,8 @@ public class JdbcTransactionManagerTests { @@ -698,10 +654,8 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
boolean condition = !TransactionSynchronizationManager.hasResource(ds2);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds2)).as("Hasn't thread connection").isTrue();
verify(con).commit();
verify(con).close();
verify(con2).rollback();
@ -722,12 +676,9 @@ public class JdbcTransactionManagerTests { @@ -722,12 +676,9 @@ public class JdbcTransactionManagerTests {
final TransactionTemplate tt2 = new TransactionTemplate(tm2);
tt2.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
boolean condition4 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition4).as("Hasn't thread connection").isTrue();
boolean condition3 = !TransactionSynchronizationManager.hasResource(ds2);
assertThat(condition3).as("Hasn't thread connection").isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds2)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
assertThatExceptionOfType(CannotCreateTransactionException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@ -746,10 +697,8 @@ public class JdbcTransactionManagerTests { @@ -746,10 +697,8 @@ public class JdbcTransactionManagerTests {
}
})).withCause(failure);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
boolean condition = !TransactionSynchronizationManager.hasResource(ds2);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds2)).as("Hasn't thread connection").isTrue();
verify(con).rollback();
verify(con).close();
}
@ -758,10 +707,8 @@ public class JdbcTransactionManagerTests { @@ -758,10 +707,8 @@ public class JdbcTransactionManagerTests {
public void testPropagationNotSupportedWithExistingTransaction() throws Exception {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -773,11 +720,9 @@ public class JdbcTransactionManagerTests { @@ -773,11 +720,9 @@ public class JdbcTransactionManagerTests {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Isn't new transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Isn't new transaction").isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isFalse();
status.setRollbackOnly();
@ -789,8 +734,7 @@ public class JdbcTransactionManagerTests { @@ -789,8 +734,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).commit();
verify(con).close();
}
@ -799,10 +743,8 @@ public class JdbcTransactionManagerTests { @@ -799,10 +743,8 @@ public class JdbcTransactionManagerTests {
public void testPropagationNeverWithExistingTransaction() throws Exception {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
assertThatExceptionOfType(IllegalTransactionStateException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@ -820,8 +762,7 @@ public class JdbcTransactionManagerTests { @@ -820,8 +762,7 @@ public class JdbcTransactionManagerTests {
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).rollback();
verify(con).close();
}
@ -830,10 +771,8 @@ public class JdbcTransactionManagerTests { @@ -830,10 +771,8 @@ public class JdbcTransactionManagerTests {
public void testPropagationSupportsAndRequiresNew() throws Exception {
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -854,8 +793,7 @@ public class JdbcTransactionManagerTests { @@ -854,8 +793,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).commit();
verify(con).close();
}
@ -869,10 +807,8 @@ public class JdbcTransactionManagerTests { @@ -869,10 +807,8 @@ public class JdbcTransactionManagerTests {
final
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -896,8 +832,7 @@ public class JdbcTransactionManagerTests { @@ -896,8 +832,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con1).close();
verify(con2).commit();
verify(con2).close();
@ -912,8 +847,7 @@ public class JdbcTransactionManagerTests { @@ -912,8 +847,7 @@ public class JdbcTransactionManagerTests {
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
tt.setReadOnly(true);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
@ -923,8 +857,7 @@ public class JdbcTransactionManagerTests { @@ -923,8 +857,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
InOrder ordered = inOrder(con);
ordered.verify(con).setReadOnly(true);
ordered.verify(con).setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
@ -947,8 +880,7 @@ public class JdbcTransactionManagerTests { @@ -947,8 +880,7 @@ public class JdbcTransactionManagerTests {
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
tt.setReadOnly(true);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
@ -958,8 +890,7 @@ public class JdbcTransactionManagerTests { @@ -958,8 +890,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
InOrder ordered = inOrder(con, stmt);
ordered.verify(con).setReadOnly(true);
ordered.verify(con).setAutoCommit(false);
@ -981,8 +912,7 @@ public class JdbcTransactionManagerTests { @@ -981,8 +912,7 @@ public class JdbcTransactionManagerTests {
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setTimeout(timeout);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
try {
tt.execute(new TransactionCallbackWithoutResult() {
@ -1016,8 +946,7 @@ public class JdbcTransactionManagerTests { @@ -1016,8 +946,7 @@ public class JdbcTransactionManagerTests {
}
}
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
if (timeout > 1) {
verify(ps).setQueryTimeout(timeout - 1);
verify(con).commit();
@ -1037,8 +966,7 @@ public class JdbcTransactionManagerTests { @@ -1037,8 +966,7 @@ public class JdbcTransactionManagerTests {
given(con.getWarnings()).willThrow(new SQLException());
TransactionTemplate tt = new TransactionTemplate(tm);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
@ -1059,8 +987,7 @@ public class JdbcTransactionManagerTests { @@ -1059,8 +987,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).commit();
@ -1074,8 +1001,7 @@ public class JdbcTransactionManagerTests { @@ -1074,8 +1001,7 @@ public class JdbcTransactionManagerTests {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -1119,8 +1045,7 @@ public class JdbcTransactionManagerTests { @@ -1119,8 +1045,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).commit();
@ -1134,8 +1059,7 @@ public class JdbcTransactionManagerTests { @@ -1134,8 +1059,7 @@ public class JdbcTransactionManagerTests {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRES_NEW);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -1180,8 +1104,7 @@ public class JdbcTransactionManagerTests { @@ -1180,8 +1104,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).commit();
@ -1205,8 +1128,7 @@ public class JdbcTransactionManagerTests { @@ -1205,8 +1128,7 @@ public class JdbcTransactionManagerTests {
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).close();
}
@ -1223,8 +1145,7 @@ public class JdbcTransactionManagerTests { @@ -1223,8 +1145,7 @@ public class JdbcTransactionManagerTests {
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).close();
}
@ -1242,8 +1163,7 @@ public class JdbcTransactionManagerTests { @@ -1242,8 +1163,7 @@ public class JdbcTransactionManagerTests {
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).close();
}
@ -1260,8 +1180,7 @@ public class JdbcTransactionManagerTests { @@ -1260,8 +1180,7 @@ public class JdbcTransactionManagerTests {
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).close();
}
@ -1279,8 +1198,7 @@ public class JdbcTransactionManagerTests { @@ -1279,8 +1198,7 @@ public class JdbcTransactionManagerTests {
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).rollback();
verify(con).close();
}
@ -1299,8 +1217,7 @@ public class JdbcTransactionManagerTests { @@ -1299,8 +1217,7 @@ public class JdbcTransactionManagerTests {
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).rollback();
@ -1323,8 +1240,7 @@ public class JdbcTransactionManagerTests { @@ -1323,8 +1240,7 @@ public class JdbcTransactionManagerTests {
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).rollback();
@ -1346,8 +1262,7 @@ public class JdbcTransactionManagerTests { @@ -1346,8 +1262,7 @@ public class JdbcTransactionManagerTests {
}
}));
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
InOrder ordered = inOrder(con);
ordered.verify(con).setAutoCommit(false);
ordered.verify(con).rollback();
@ -1359,65 +1274,53 @@ public class JdbcTransactionManagerTests { @@ -1359,65 +1274,53 @@ public class JdbcTransactionManagerTests {
public void testTransactionWithPropagationSupports() throws Exception {
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Is not new transaction").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!status.isNewTransaction()).as("Is not new transaction").isTrue();
assertThat(TransactionSynchronizationManager.isCurrentTransactionReadOnly()).isFalse();
assertThat(TransactionSynchronizationManager.isActualTransactionActive()).isFalse();
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
}
@Test
public void testTransactionWithPropagationNotSupported() throws Exception {
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Is not new transaction").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!status.isNewTransaction()).as("Is not new transaction").isTrue();
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
}
@Test
public void testTransactionWithPropagationNever() throws Exception {
TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NEVER);
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
boolean condition1 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition1).as("Hasn't thread connection").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Is not new transaction").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!status.isNewTransaction()).as("Is not new transaction").isTrue();
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
}
@Test
@ -1442,37 +1345,31 @@ public class JdbcTransactionManagerTests { @@ -1442,37 +1345,31 @@ public class JdbcTransactionManagerTests {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
boolean condition1 = !status.hasSavepoint();
assertThat(condition1).as("Isn't nested transaction").isTrue();
assertThat(!status.hasSavepoint()).as("Isn't nested transaction").isTrue();
for (int i = 0; i < count; i++) {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Isn't new transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Isn't new transaction").isTrue();
assertThat(status.hasSavepoint()).as("Is nested transaction").isTrue();
}
});
}
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
boolean condition = !status.hasSavepoint();
assertThat(condition).as("Isn't nested transaction").isTrue();
assertThat(!status.hasSavepoint()).as("Isn't nested transaction").isTrue();
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con, times(count)).releaseSavepoint(sp);
verify(con).commit();
verify(con).close();
@ -1489,36 +1386,30 @@ public class JdbcTransactionManagerTests { @@ -1489,36 +1386,30 @@ public class JdbcTransactionManagerTests {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
boolean condition1 = !status.hasSavepoint();
assertThat(condition1).as("Isn't nested transaction").isTrue();
assertThat(!status.hasSavepoint()).as("Isn't nested transaction").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Isn't new transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Isn't new transaction").isTrue();
assertThat(status.hasSavepoint()).as("Is nested transaction").isTrue();
status.setRollbackOnly();
}
});
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
boolean condition = !status.hasSavepoint();
assertThat(condition).as("Isn't nested transaction").isTrue();
assertThat(!status.hasSavepoint()).as("Isn't nested transaction").isTrue();
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).rollback(sp);
verify(con).releaseSavepoint(sp);
verify(con).commit();
@ -1536,25 +1427,21 @@ public class JdbcTransactionManagerTests { @@ -1536,25 +1427,21 @@ public class JdbcTransactionManagerTests {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
boolean condition1 = !status.hasSavepoint();
assertThat(condition1).as("Isn't nested transaction").isTrue();
assertThat(!status.hasSavepoint()).as("Isn't nested transaction").isTrue();
assertThatIllegalStateException().isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Isn't new transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Isn't new transaction").isTrue();
assertThat(status.hasSavepoint()).as("Is nested transaction").isTrue();
TransactionTemplate ntt = new TransactionTemplate(tm);
ntt.execute(new TransactionCallbackWithoutResult() {
@ -1562,23 +1449,19 @@ public class JdbcTransactionManagerTests { @@ -1562,23 +1449,19 @@ public class JdbcTransactionManagerTests {
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
boolean condition1 = !status.isNewTransaction();
assertThat(condition1).as("Isn't new transaction").isTrue();
boolean condition = !status.hasSavepoint();
assertThat(condition).as("Is regular transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Isn't new transaction").isTrue();
assertThat(!status.hasSavepoint()).as("Is regular transaction").isTrue();
throw new IllegalStateException();
}
});
}
}));
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
boolean condition = !status.hasSavepoint();
assertThat(condition).as("Isn't nested transaction").isTrue();
assertThat(!status.hasSavepoint()).as("Isn't nested transaction").isTrue();
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).rollback(sp);
verify(con).releaseSavepoint(sp);
verify(con).commit();
@ -1596,25 +1479,21 @@ public class JdbcTransactionManagerTests { @@ -1596,25 +1479,21 @@ public class JdbcTransactionManagerTests {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
boolean condition1 = !status.hasSavepoint();
assertThat(condition1).as("Isn't nested transaction").isTrue();
assertThat(!status.hasSavepoint()).as("Isn't nested transaction").isTrue();
assertThatExceptionOfType(UnexpectedRollbackException.class).isThrownBy(() ->
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).as("Isn't new transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Isn't new transaction").isTrue();
assertThat(status.hasSavepoint()).as("Is nested transaction").isTrue();
TransactionTemplate ntt = new TransactionTemplate(tm);
ntt.execute(new TransactionCallbackWithoutResult() {
@ -1622,23 +1501,19 @@ public class JdbcTransactionManagerTests { @@ -1622,23 +1501,19 @@ public class JdbcTransactionManagerTests {
protected void doInTransactionWithoutResult(TransactionStatus status) throws RuntimeException {
assertThat(TransactionSynchronizationManager.hasResource(ds)).as("Has thread connection").isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization active").isTrue();
boolean condition1 = !status.isNewTransaction();
assertThat(condition1).as("Isn't new transaction").isTrue();
boolean condition = !status.hasSavepoint();
assertThat(condition).as("Is regular transaction").isTrue();
assertThat(!status.isNewTransaction()).as("Isn't new transaction").isTrue();
assertThat(!status.hasSavepoint()).as("Is regular transaction").isTrue();
status.setRollbackOnly();
}
});
}
}));
assertThat(status.isNewTransaction()).as("Is new transaction").isTrue();
boolean condition = !status.hasSavepoint();
assertThat(condition).as("Isn't nested transaction").isTrue();
assertThat(!status.hasSavepoint()).as("Isn't nested transaction").isTrue();
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).rollback(sp);
verify(con).releaseSavepoint(sp);
verify(con).commit();
@ -1656,10 +1531,8 @@ public class JdbcTransactionManagerTests { @@ -1656,10 +1531,8 @@ public class JdbcTransactionManagerTests {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -1671,8 +1544,7 @@ public class JdbcTransactionManagerTests { @@ -1671,8 +1544,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).releaseSavepoint(sp);
verify(con).commit();
verify(con).close();
@ -1690,10 +1562,8 @@ public class JdbcTransactionManagerTests { @@ -1690,10 +1562,8 @@ public class JdbcTransactionManagerTests {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -1705,8 +1575,7 @@ public class JdbcTransactionManagerTests { @@ -1705,8 +1575,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).rollback(sp);
verify(con).commit();
verify(con).close();
@ -1716,10 +1585,8 @@ public class JdbcTransactionManagerTests { @@ -1716,10 +1585,8 @@ public class JdbcTransactionManagerTests {
public void testTransactionWithPropagationNested() throws Exception {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -1728,8 +1595,7 @@ public class JdbcTransactionManagerTests { @@ -1728,8 +1595,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).commit();
verify(con).close();
}
@ -1738,10 +1604,8 @@ public class JdbcTransactionManagerTests { @@ -1738,10 +1604,8 @@ public class JdbcTransactionManagerTests {
public void testTransactionWithPropagationNestedAndRollback() throws Exception {
final TransactionTemplate tt = new TransactionTemplate(tm);
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
boolean condition2 = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition2).as("Hasn't thread connection").isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).as("Synchronization not active").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).as("Synchronization not active").isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -1751,8 +1615,7 @@ public class JdbcTransactionManagerTests { @@ -1751,8 +1615,7 @@ public class JdbcTransactionManagerTests {
}
});
boolean condition = !TransactionSynchronizationManager.hasResource(ds);
assertThat(condition).as("Hasn't thread connection").isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(ds)).as("Hasn't thread connection").isTrue();
verify(con).rollback();
verify(con).close();
}

24
spring-messaging/src/test/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethodTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,9 +25,11 @@ import org.springframework.lang.Nullable; @@ -25,9 +25,11 @@ import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.mockito.Mockito.mock;
/**
@ -71,8 +73,8 @@ public class InvocableHandlerMethodTests { @@ -71,8 +73,8 @@ public class InvocableHandlerMethodTests {
@Test
public void cannotResolveArg() throws Exception {
Method method = ResolvableMethod.on(Handler.class).mockCall(c -> c.handle(0, "")).method();
assertThatExceptionOfType(MethodArgumentResolutionException.class).isThrownBy(() ->
invoke(new Handler(), method))
assertThatExceptionOfType(MethodArgumentResolutionException.class)
.isThrownBy(() -> invoke(new Handler(), method))
.withMessageContaining("Could not resolve parameter [0]");
}
@ -125,20 +127,20 @@ public class InvocableHandlerMethodTests { @@ -125,20 +127,20 @@ public class InvocableHandlerMethodTests {
Handler handler = new Handler();
Method method = ResolvableMethod.on(Handler.class).argTypes(Throwable.class).resolveMethod();
RuntimeException runtimeException = new RuntimeException("error");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
invoke(handler, method, runtimeException))
assertThatRuntimeException()
.isThrownBy(() -> invoke(handler, method, runtimeException))
.isSameAs(runtimeException);
Error error = new Error("error");
assertThatExceptionOfType(Error.class).isThrownBy(() ->
invoke(handler, method, error))
assertThatExceptionOfType(Error.class)
.isThrownBy(() -> invoke(handler, method, error))
.isSameAs(error);
Exception exception = new Exception("error");
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
invoke(handler, method, exception))
assertThatException()
.isThrownBy(() -> invoke(handler, method, exception))
.isSameAs(exception);
Throwable throwable = new Throwable("error", exception);
assertThatIllegalStateException().isThrownBy(() ->
invoke(handler, method, throwable))
assertThatIllegalStateException()
.isThrownBy(() -> invoke(handler, method, throwable))
.withCause(throwable)
.withMessageContaining("Invocation failure");
}

23
spring-orm/src/test/java/org/springframework/orm/jpa/AbstractContainerEntityManagerFactoryIntegrationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -31,8 +31,10 @@ import org.springframework.orm.jpa.domain.DriversLicense; @@ -31,8 +31,10 @@ import org.springframework.orm.jpa.domain.DriversLicense;
import org.springframework.orm.jpa.domain.Person;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Integration tests for LocalContainerEntityManagerFactoryBean.
@ -93,7 +95,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests @@ -93,7 +95,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
@Test
public void testBogusQuery() {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> {
assertThatRuntimeException().isThrownBy(() -> {
Query query = sharedEntityManager.createQuery("It's raining toads");
// required in OpenJPA case
query.executeUpdate();
@ -102,7 +104,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests @@ -102,7 +104,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
@Test
public void testGetReferenceWhenNoRow() {
assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
assertThatException().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.
@ -186,8 +188,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests @@ -186,8 +188,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertThat(people.size()).isEqualTo(0);
assertThatExceptionOfType(NoResultException.class).isThrownBy(
q::getSingleResult);
assertThatExceptionOfType(NoResultException.class).isThrownBy(q::getSingleResult);
}
@Test
@ -199,8 +200,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests @@ -199,8 +200,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertThat(people.size()).isEqualTo(0);
assertThatExceptionOfType(NoResultException.class).isThrownBy(
q::getSingleResult);
assertThatExceptionOfType(NoResultException.class).isThrownBy(q::getSingleResult);
}
@Test
@ -210,8 +210,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests @@ -210,8 +210,7 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
q.setFlushMode(FlushModeType.AUTO);
List<Person> people = q.getResultList();
assertThat(people.size()).isEqualTo(0);
assertThatExceptionOfType(NoResultException.class).isThrownBy(
q::getSingleResult);
assertThatExceptionOfType(NoResultException.class).isThrownBy(q::getSingleResult);
}
@Test
@ -224,15 +223,15 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests @@ -224,15 +223,15 @@ public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
q.setFlushMode(FlushModeType.AUTO);
List<Person> people = q.getResultList();
assertThat(people.size()).isEqualTo(0);
assertThatExceptionOfType(Exception.class).isThrownBy(q::getSingleResult)
assertThatException()
.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);
assertThatExceptionOfType(NoResultException.class).isThrownBy(q2::getSingleResult);
}
@Test

258
spring-orm/src/test/java/org/springframework/orm/jpa/JpaTransactionManagerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -39,6 +39,7 @@ import org.springframework.transaction.support.TransactionTemplate; @@ -39,6 +39,7 @@ import org.springframework.transaction.support.TransactionTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.mock;
@ -94,10 +95,8 @@ public class JpaTransactionManagerTests { @@ -94,10 +95,8 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
Object result = tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
@ -106,10 +105,8 @@ public class JpaTransactionManagerTests { @@ -106,10 +105,8 @@ public class JpaTransactionManagerTests {
});
assertThat(result).isSameAs(l);
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(tx).commit();
verify(manager).flush();
@ -125,10 +122,8 @@ public class JpaTransactionManagerTests { @@ -125,10 +122,8 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
try {
Object result = tt.execute(status -> {
@ -140,14 +135,11 @@ public class JpaTransactionManagerTests { @@ -140,14 +135,11 @@ public class JpaTransactionManagerTests {
}
catch (TransactionSystemException tse) {
// expected
boolean condition = tse.getCause() instanceof RollbackException;
assertThat(condition).isTrue();
assertThat(tse.getCause() instanceof RollbackException).isTrue();
}
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(manager).flush();
verify(manager).close();
@ -161,22 +153,18 @@ public class JpaTransactionManagerTests { @@ -161,22 +153,18 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
assertThatRuntimeException().isThrownBy(() ->
tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
throw new RuntimeException("some exception");
})).withMessage("some exception");
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(tx).rollback();
verify(manager).close();
@ -189,22 +177,18 @@ public class JpaTransactionManagerTests { @@ -189,22 +177,18 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
assertThatRuntimeException().isThrownBy(() ->
tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
throw new RuntimeException("some exception");
}));
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(manager).close();
}
@ -217,10 +201,8 @@ public class JpaTransactionManagerTests { @@ -217,10 +201,8 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
@ -231,10 +213,8 @@ public class JpaTransactionManagerTests { @@ -231,10 +213,8 @@ public class JpaTransactionManagerTests {
return l;
});
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(manager).flush();
verify(tx).rollback();
@ -248,10 +228,8 @@ public class JpaTransactionManagerTests { @@ -248,10 +228,8 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
@ -262,10 +240,8 @@ public class JpaTransactionManagerTests { @@ -262,10 +240,8 @@ public class JpaTransactionManagerTests {
});
});
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(manager).flush();
verify(tx).commit();
@ -280,12 +256,10 @@ public class JpaTransactionManagerTests { @@ -280,12 +256,10 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
assertThatRuntimeException().isThrownBy(() ->
tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
return tt.execute(status1 -> {
@ -294,10 +268,8 @@ public class JpaTransactionManagerTests { @@ -294,10 +268,8 @@ public class JpaTransactionManagerTests {
});
}));
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(tx).setRollbackOnly();
verify(tx).rollback();
@ -314,10 +286,8 @@ public class JpaTransactionManagerTests { @@ -314,10 +286,8 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
assertThatExceptionOfType(TransactionSystemException.class).isThrownBy(() ->
tt.execute(status -> {
@ -331,10 +301,8 @@ public class JpaTransactionManagerTests { @@ -331,10 +301,8 @@ public class JpaTransactionManagerTests {
}))
.withCauseInstanceOf(RollbackException.class);
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(manager).flush();
verify(tx).setRollbackOnly();
@ -352,10 +320,8 @@ public class JpaTransactionManagerTests { @@ -352,10 +320,8 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
Object result = tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
@ -366,10 +332,8 @@ public class JpaTransactionManagerTests { @@ -366,10 +332,8 @@ public class JpaTransactionManagerTests {
});
assertThat(result).isSameAs(l);
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(manager).flush();
verify(manager, times(2)).close();
@ -385,10 +349,8 @@ public class JpaTransactionManagerTests { @@ -385,10 +349,8 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
@ -408,10 +370,8 @@ public class JpaTransactionManagerTests { @@ -408,10 +370,8 @@ public class JpaTransactionManagerTests {
TransactionSynchronizationManager.unbindResource(factory);
}
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(tx, times(2)).begin();
verify(tx, times(2)).commit();
@ -428,10 +388,8 @@ public class JpaTransactionManagerTests { @@ -428,10 +388,8 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
Object result = tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isFalse();
@ -444,10 +402,8 @@ public class JpaTransactionManagerTests { @@ -444,10 +402,8 @@ public class JpaTransactionManagerTests {
});
assertThat(result).isSameAs(l);
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(tx).commit();
verify(manager).flush();
@ -465,10 +421,8 @@ public class JpaTransactionManagerTests { @@ -465,10 +421,8 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
Object result = tt.execute(status -> {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory);
@ -483,10 +437,8 @@ public class JpaTransactionManagerTests { @@ -483,10 +437,8 @@ public class JpaTransactionManagerTests {
});
assertThat(result).isSameAs(l);
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(tx).commit();
verify(manager).flush();
@ -505,10 +457,8 @@ public class JpaTransactionManagerTests { @@ -505,10 +457,8 @@ public class JpaTransactionManagerTests {
given(manager2.getTransaction()).willReturn(tx2);
given(manager2.isOpen()).willReturn(true);
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
tt.execute(status -> {
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
@ -524,10 +474,8 @@ public class JpaTransactionManagerTests { @@ -524,10 +474,8 @@ public class JpaTransactionManagerTests {
return null;
});
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(tx).commit();
verify(tx2).begin();
@ -547,26 +495,20 @@ public class JpaTransactionManagerTests { @@ -547,26 +495,20 @@ public class JpaTransactionManagerTests {
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
Object result = tt.execute(status -> {
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).isTrue();
assertThat(!status.isNewTransaction()).isTrue();
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
});
assertThat(result).isSameAs(l);
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(manager).flush();
verify(manager).close();
@ -578,26 +520,20 @@ public class JpaTransactionManagerTests { @@ -578,26 +520,20 @@ public class JpaTransactionManagerTests {
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
tt.execute(status -> {
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).isTrue();
assertThat(!status.isNewTransaction()).isTrue();
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status.setRollbackOnly();
return null;
});
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(manager).flush();
verify(manager).close();
@ -610,10 +546,8 @@ public class JpaTransactionManagerTests { @@ -610,10 +546,8 @@ public class JpaTransactionManagerTests {
final List<String> l = new ArrayList<>();
l.add("test");
boolean condition2 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition2).isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
try {
@ -626,8 +560,7 @@ public class JpaTransactionManagerTests { @@ -626,8 +560,7 @@ public class JpaTransactionManagerTests {
assertThat(result).isSameAs(l);
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
}
finally {
TransactionSynchronizationManager.unbindResource(factory);
@ -642,10 +575,8 @@ public class JpaTransactionManagerTests { @@ -642,10 +575,8 @@ public class JpaTransactionManagerTests {
given(manager.getTransaction()).willReturn(tx);
given(tx.isActive()).willReturn(true);
boolean condition2 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition2).isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
try {
@ -658,8 +589,7 @@ public class JpaTransactionManagerTests { @@ -658,8 +589,7 @@ public class JpaTransactionManagerTests {
});
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
}
finally {
TransactionSynchronizationManager.unbindResource(factory);
@ -677,26 +607,22 @@ public class JpaTransactionManagerTests { @@ -677,26 +607,22 @@ public class JpaTransactionManagerTests {
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
boolean condition2 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition2).isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
try {
Object result = tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).isTrue();
assertThat(!status.isNewTransaction()).isTrue();
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
return l;
});
assertThat(result).isSameAs(l);
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
}
finally {
TransactionSynchronizationManager.unbindResource(factory);
@ -709,26 +635,22 @@ public class JpaTransactionManagerTests { @@ -709,26 +635,22 @@ public class JpaTransactionManagerTests {
public void testTransactionRollbackWithPreboundAndPropagationSupports() {
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
boolean condition2 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition2).isTrue();
boolean condition1 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition1).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
TransactionSynchronizationManager.bindResource(factory, new EntityManagerHolder(manager));
try {
tt.execute(status -> {
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
boolean condition = !status.isNewTransaction();
assertThat(condition).isTrue();
assertThat(!status.isNewTransaction()).isTrue();
EntityManagerFactoryUtils.getTransactionalEntityManager(factory).flush();
status.setRollbackOnly();
return null;
});
assertThat(TransactionSynchronizationManager.hasResource(factory)).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
}
finally {
TransactionSynchronizationManager.unbindResource(factory);
@ -758,10 +680,8 @@ public class JpaTransactionManagerTests { @@ -758,10 +680,8 @@ public class JpaTransactionManagerTests {
public void testTransactionFlush() {
given(manager.getTransaction()).willReturn(tx);
boolean condition3 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition3).isTrue();
boolean condition2 = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition2).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
tt.execute(new TransactionCallbackWithoutResult() {
@Override
@ -771,10 +691,8 @@ public class JpaTransactionManagerTests { @@ -771,10 +691,8 @@ public class JpaTransactionManagerTests {
}
});
boolean condition1 = !TransactionSynchronizationManager.hasResource(factory);
assertThat(condition1).isTrue();
boolean condition = !TransactionSynchronizationManager.isSynchronizationActive();
assertThat(condition).isTrue();
assertThat(!TransactionSynchronizationManager.hasResource(factory)).isTrue();
assertThat(!TransactionSynchronizationManager.isSynchronizationActive()).isTrue();
verify(tx).commit();
verify(manager).flush();

10
spring-orm/src/test/java/org/springframework/orm/jpa/persistenceunit/PersistenceXmlParsingTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,7 +38,7 @@ import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup; @@ -38,7 +38,7 @@ import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.jdbc.datasource.lookup.MapDataSourceLookup;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Unit and integration tests for the JPA XML resource parsing support.
@ -260,8 +260,7 @@ public class PersistenceXmlParsingTests { @@ -260,8 +260,7 @@ public class PersistenceXmlParsingTests {
PersistenceUnitReader reader = new PersistenceUnitReader(
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
String resource = "/org/springframework/orm/jpa/persistence-invalid.xml";
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
reader.readPersistenceUnitInfos(resource));
assertThatRuntimeException().isThrownBy(() -> reader.readPersistenceUnitInfos(resource));
}
@Disabled("not doing schema parsing anymore for JPA 2.0 compatibility")
@ -270,8 +269,7 @@ public class PersistenceXmlParsingTests { @@ -270,8 +269,7 @@ public class PersistenceXmlParsingTests {
PersistenceUnitReader reader = new PersistenceUnitReader(
new PathMatchingResourcePatternResolver(), new JndiDataSourceLookup());
String resource = "/org/springframework/orm/jpa/persistence-no-schema.xml";
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
reader.readPersistenceUnitInfos(resource));
assertThatRuntimeException().isThrownBy(() -> reader.readPersistenceUnitInfos(resource));
}
@Test

8
spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerIntegrationTests.java

@ -52,7 +52,7 @@ import org.springframework.util.ReflectionUtils; @@ -52,7 +52,7 @@ import org.springframework.util.ReflectionUtils;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.only;
@ -130,9 +130,9 @@ public class EventPublishingTestExecutionListenerIntegrationTests { @@ -130,9 +130,9 @@ public class EventPublishingTestExecutionListenerIntegrationTests {
@Test
public void beforeTestMethodAnnotationWithFailingEventListener() throws Exception {
Method method = ReflectionUtils.findMethod(ExampleTestCase.class, "testWithFailingEventListener");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
testContextManager.beforeTestMethod(testInstance, method))
.withMessageContaining("Boom!");
assertThatRuntimeException()
.isThrownBy(() -> testContextManager.beforeTestMethod(testInstance, method))
.withMessageContaining("Boom!");
verify(listener, only()).beforeTestMethod(testContext);
}

7
spring-test/src/test/java/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,7 +26,7 @@ import org.springframework.test.annotation.Timed; @@ -26,7 +26,7 @@ import org.springframework.test.annotation.Timed;
import org.springframework.test.context.TestContextManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Unit tests for {@link SpringJUnit4ClassRunner}.
@ -53,8 +53,7 @@ public class SpringJUnit4ClassRunnerTests { @@ -53,8 +53,7 @@ public class SpringJUnit4ClassRunnerTests {
};
}
};
assertThatExceptionOfType(Exception.class).isThrownBy(
runner::createTest);
assertThatException().isThrownBy(runner::createTest);
}
@Test

27
spring-tx/src/test/java/org/springframework/dao/annotation/PersistenceExceptionTranslationAdvisorTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -33,6 +33,7 @@ import org.springframework.dao.support.PersistenceExceptionTranslator; @@ -33,6 +33,7 @@ import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.stereotype.Repository;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Tests for PersistenceExceptionTranslationAdvisor's exception translation, as applied by
@ -69,11 +70,11 @@ public class PersistenceExceptionTranslationAdvisorTests { @@ -69,11 +70,11 @@ public class PersistenceExceptionTranslationAdvisorTests {
ri.throwsPersistenceException();
target.setBehavior(persistenceException1);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::noThrowsClause)
assertThatRuntimeException()
.isThrownBy(ri::noThrowsClause)
.isSameAs(persistenceException1);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::throwsPersistenceException)
assertThatRuntimeException()
.isThrownBy(ri::throwsPersistenceException)
.isSameAs(persistenceException1);
}
@ -86,11 +87,11 @@ public class PersistenceExceptionTranslationAdvisorTests { @@ -86,11 +87,11 @@ public class PersistenceExceptionTranslationAdvisorTests {
ri.throwsPersistenceException();
target.setBehavior(doNotTranslate);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::noThrowsClause)
assertThatRuntimeException()
.isThrownBy(ri::noThrowsClause)
.isSameAs(doNotTranslate);
assertThatExceptionOfType(RuntimeException.class).isThrownBy(
ri::throwsPersistenceException)
assertThatRuntimeException()
.isThrownBy(ri::throwsPersistenceException)
.isSameAs(doNotTranslate);
}
@ -123,12 +124,12 @@ public class PersistenceExceptionTranslationAdvisorTests { @@ -123,12 +124,12 @@ public class PersistenceExceptionTranslationAdvisorTests {
RepositoryInterface ri = createProxy(target);
target.setBehavior(persistenceException1);
assertThatExceptionOfType(DataAccessException.class).isThrownBy(
ri::noThrowsClause)
assertThatExceptionOfType(DataAccessException.class)
.isThrownBy(ri::noThrowsClause)
.withCause(persistenceException1);
assertThatExceptionOfType(PersistenceException.class).isThrownBy(
ri::throwsPersistenceException)
assertThatExceptionOfType(PersistenceException.class)
.isThrownBy(ri::throwsPersistenceException)
.isSameAs(persistenceException1);
}

48
spring-tx/src/test/java/org/springframework/transaction/TransactionSupportTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import org.springframework.transaction.support.TransactionTemplate; @@ -28,6 +28,7 @@ import org.springframework.transaction.support.TransactionTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Juergen Hoeller
@ -57,20 +58,17 @@ public class TransactionSupportTests { @@ -57,20 +58,17 @@ public class TransactionSupportTests {
DefaultTransactionStatus status1 = (DefaultTransactionStatus)
tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
assertThat(status1.getTransaction() != null).as("Must have transaction").isTrue();
boolean condition2 = !status1.isNewTransaction();
assertThat(condition2).as("Must not be new transaction").isTrue();
assertThat(!status1.isNewTransaction()).as("Must not be new transaction").isTrue();
DefaultTransactionStatus status2 = (DefaultTransactionStatus)
tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED));
assertThat(status2.getTransaction() != null).as("Must have transaction").isTrue();
boolean condition1 = !status2.isNewTransaction();
assertThat(condition1).as("Must not be new transaction").isTrue();
assertThat(!status2.isNewTransaction()).as("Must not be new transaction").isTrue();
DefaultTransactionStatus status3 = (DefaultTransactionStatus)
tm.getTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY));
assertThat(status3.getTransaction() != null).as("Must have transaction").isTrue();
boolean condition = !status3.isNewTransaction();
assertThat(condition).as("Must not be new transaction").isTrue();
assertThat(!status3.isNewTransaction()).as("Must not be new transaction").isTrue();
}
@Test
@ -181,8 +179,8 @@ public class TransactionSupportTests { @@ -181,8 +179,8 @@ public class TransactionSupportTests {
public void transactionTemplateWithException() {
TestTransactionManager tm = new TestTransactionManager(false, true);
TransactionTemplate template = new TransactionTemplate(tm);
final RuntimeException ex = new RuntimeException("Some application exception");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
RuntimeException ex = new RuntimeException("Some application exception");
assertThatRuntimeException().isThrownBy(() ->
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
@ -208,14 +206,9 @@ public class TransactionSupportTests { @@ -208,14 +206,9 @@ public class TransactionSupportTests {
}
};
TransactionTemplate template = new TransactionTemplate(tm);
final RuntimeException ex = new RuntimeException("Some application exception");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw ex;
}
}))
RuntimeException ex = new RuntimeException("Some application exception");
assertThatRuntimeException()
.isThrownBy(() -> template.executeWithoutResult(status -> { throw ex; }))
.isSameAs(tex);
assertThat(tm.begin).as("triggered begin").isTrue();
assertThat(tm.commit).as("no commit").isFalse();
@ -227,13 +220,8 @@ public class TransactionSupportTests { @@ -227,13 +220,8 @@ public class TransactionSupportTests {
public void transactionTemplateWithError() {
TestTransactionManager tm = new TestTransactionManager(false, true);
TransactionTemplate template = new TransactionTemplate(tm);
assertThatExceptionOfType(Error.class).isThrownBy(() ->
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus status) {
throw new Error("Some application error");
}
}));
assertThatExceptionOfType(Error.class)
.isThrownBy(() -> template.executeWithoutResult(status -> { throw new Error("Some application error"); }));
assertThat(tm.begin).as("triggered begin").isTrue();
assertThat(tm.commit).as("no commit").isFalse();
assertThat(tm.rollback).as("triggered rollback").isTrue();
@ -247,23 +235,19 @@ public class TransactionSupportTests { @@ -247,23 +235,19 @@ public class TransactionSupportTests {
template.setTransactionManager(tm);
assertThat(template.getTransactionManager() == tm).as("correct transaction manager set").isTrue();
assertThatIllegalArgumentException().isThrownBy(() ->
template.setPropagationBehaviorName("TIMEOUT_DEFAULT"));
assertThatIllegalArgumentException().isThrownBy(() -> template.setPropagationBehaviorName("TIMEOUT_DEFAULT"));
template.setPropagationBehaviorName("PROPAGATION_SUPPORTS");
assertThat(template.getPropagationBehavior() == TransactionDefinition.PROPAGATION_SUPPORTS).as("Correct propagation behavior set").isTrue();
assertThatIllegalArgumentException().isThrownBy(() ->
template.setPropagationBehavior(999));
assertThatIllegalArgumentException().isThrownBy(() -> template.setPropagationBehavior(999));
template.setPropagationBehavior(TransactionDefinition.PROPAGATION_MANDATORY);
assertThat(template.getPropagationBehavior() == TransactionDefinition.PROPAGATION_MANDATORY).as("Correct propagation behavior set").isTrue();
assertThatIllegalArgumentException().isThrownBy(() ->
template.setIsolationLevelName("TIMEOUT_DEFAULT"));
assertThatIllegalArgumentException().isThrownBy(() -> template.setIsolationLevelName("TIMEOUT_DEFAULT"));
template.setIsolationLevelName("ISOLATION_SERIALIZABLE");
assertThat(template.getIsolationLevel() == TransactionDefinition.ISOLATION_SERIALIZABLE).as("Correct isolation level set").isTrue();
assertThatIllegalArgumentException().isThrownBy(() ->
template.setIsolationLevel(999));
assertThatIllegalArgumentException().isThrownBy(() -> template.setIsolationLevel(999));
template.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
assertThat(template.getIsolationLevel() == TransactionDefinition.ISOLATION_REPEATABLE_READ).as("Correct isolation level set").isTrue();

12
spring-tx/src/test/java/org/springframework/transaction/annotation/AnnotationTransactionInterceptorTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,7 +32,7 @@ import org.springframework.transaction.testfixture.CallCountingTransactionManage @@ -32,7 +32,7 @@ import org.springframework.transaction.testfixture.CallCountingTransactionManage
import org.springframework.transaction.testfixture.ReactiveCallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@ -161,8 +161,8 @@ public class AnnotationTransactionInterceptorTests { @@ -161,8 +161,8 @@ public class AnnotationTransactionInterceptorTests {
TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy();
assertThatExceptionOfType(Exception.class).isThrownBy(
proxy::doSomethingElseWithCheckedException)
assertThatException()
.isThrownBy(proxy::doSomethingElseWithCheckedException)
.satisfies(ex -> assertGetTransactionAndCommitCount(1));
}
@ -174,8 +174,8 @@ public class AnnotationTransactionInterceptorTests { @@ -174,8 +174,8 @@ public class AnnotationTransactionInterceptorTests {
TestWithExceptions proxy = (TestWithExceptions) proxyFactory.getProxy();
assertThatExceptionOfType(Exception.class).isThrownBy(
proxy::doSomethingElseWithCheckedExceptionAndRollbackRule)
assertThatException()
.isThrownBy(proxy::doSomethingElseWithCheckedExceptionAndRollbackRule)
.satisfies(ex -> assertGetTransactionAndRollbackCount(1));
}

6
spring-tx/src/test/java/org/springframework/transaction/annotation/EnableTransactionManagementTests.java

@ -45,7 +45,7 @@ import org.springframework.transaction.interceptor.TransactionAttribute; @@ -45,7 +45,7 @@ import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.testfixture.CallCountingTransactionManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* Tests demonstrating use of @EnableTransactionManagement @Configuration classes.
@ -207,8 +207,8 @@ public class EnableTransactionManagementTests { @@ -207,8 +207,8 @@ public class EnableTransactionManagementTests {
public void proxyTypeAspectJCausesRegistrationOfAnnotationTransactionAspect() {
// should throw CNFE when trying to load AnnotationTransactionAspect.
// Do you actually have org.springframework.aspects on the classpath?
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
new AnnotationConfigApplicationContext(EnableAspectjTxConfig.class, TxManagerConfig.class))
assertThatException()
.isThrownBy(() -> new AnnotationConfigApplicationContext(EnableAspectjTxConfig.class, TxManagerConfig.class))
.withMessageContaining("AspectJJtaTransactionManagementConfiguration");
}

6
spring-tx/src/test/java/org/springframework/transaction/event/TransactionalApplicationListenerAdapterTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,7 +23,7 @@ import org.springframework.transaction.support.TransactionSynchronization; @@ -23,7 +23,7 @@ import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Juergen Hoeller
@ -58,7 +58,7 @@ public class TransactionalApplicationListenerAdapterTests { @@ -58,7 +58,7 @@ public class TransactionalApplicationListenerAdapterTests {
TransactionPhase.BEFORE_COMMIT, p -> {throw ex;});
adapter.addCallback(callback);
assertThatExceptionOfType(RuntimeException.class)
assertThatRuntimeException()
.isThrownBy(() -> runInTransaction(() -> adapter.onApplicationEvent(event)))
.withMessage("event");

6
spring-tx/src/test/java/org/springframework/transaction/event/TransactionalApplicationListenerMethodAdapterTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,7 +30,7 @@ import org.springframework.util.ClassUtils; @@ -30,7 +30,7 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* @author Stephane Nicoll
@ -94,7 +94,7 @@ public class TransactionalApplicationListenerMethodAdapterTests { @@ -94,7 +94,7 @@ public class TransactionalApplicationListenerMethodAdapterTests {
TransactionalApplicationListenerMethodAdapter adapter = createTestInstance(m);
adapter.addCallback(callback);
assertThatExceptionOfType(RuntimeException.class)
assertThatRuntimeException()
.isThrownBy(() -> runInTransaction(() -> adapter.onApplicationEvent(event)))
.withMessage("event");

18
spring-web/src/test/java/org/springframework/remoting/jaxws/JaxWsSupportTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,7 +38,7 @@ import org.springframework.context.support.GenericApplicationContext; @@ -38,7 +38,7 @@ import org.springframework.context.support.GenericApplicationContext;
import org.springframework.remoting.RemoteAccessException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
/**
* @author Juergen Hoeller
@ -102,19 +102,17 @@ public class JaxWsSupportTests { @@ -102,19 +102,17 @@ public class JaxWsSupportTests {
String order = orderService.getOrder(1000);
assertThat(order).isEqualTo("order 1000");
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
orderService.getOrder(0))
.matches(ex -> ex instanceof OrderNotFoundException ||
ex instanceof RemoteAccessException);
assertThatException()
.isThrownBy(() -> orderService.getOrder(0))
.matches(ex -> ex instanceof OrderNotFoundException || ex instanceof RemoteAccessException);
// ignore RemoteAccessException as probably setup issue with JAX-WS provider vs JAXB
ServiceAccessor serviceAccessor = ac.getBean("accessor", ServiceAccessor.class);
order = serviceAccessor.orderService.getOrder(1000);
assertThat(order).isEqualTo("order 1000");
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
serviceAccessor.orderService.getOrder(0))
.matches(ex -> ex instanceof OrderNotFoundException ||
ex instanceof WebServiceException);
assertThatException()
.isThrownBy(() -> serviceAccessor.orderService.getOrder(0))
.matches(ex -> ex instanceof OrderNotFoundException || ex instanceof WebServiceException);
// ignore WebServiceException as probably setup issue with JAX-WS provider vs JAXB
}
catch (BeanCreationException ex) {

32
spring-web/src/test/java/org/springframework/web/context/request/async/WebAsyncManagerTests.java

@ -29,7 +29,7 @@ import org.springframework.core.task.SimpleAsyncTaskExecutor; @@ -29,7 +29,7 @@ import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.any;
@ -70,12 +70,12 @@ public class WebAsyncManagerTests { @@ -70,12 +70,12 @@ public class WebAsyncManagerTests {
public void startAsyncProcessingWithoutAsyncWebRequest() throws Exception {
WebAsyncManager manager = WebAsyncUtils.getAsyncManager(new MockHttpServletRequest());
assertThatIllegalStateException().isThrownBy(() ->
manager.startCallableProcessing(new StubCallable(1)))
assertThatIllegalStateException()
.isThrownBy(() -> manager.startCallableProcessing(new StubCallable(1)))
.withMessage("AsyncWebRequest must not be null");
assertThatIllegalStateException().isThrownBy(() ->
manager.startDeferredResultProcessing(new DeferredResult<String>()))
assertThatIllegalStateException()
.isThrownBy(() -> manager.startDeferredResultProcessing(new DeferredResult<String>()))
.withMessage("AsyncWebRequest must not be null");
}
@ -94,13 +94,11 @@ public class WebAsyncManagerTests { @@ -94,13 +94,11 @@ public class WebAsyncManagerTests {
@Test
public void setAsyncWebRequestAfterAsyncStarted() {
this.asyncWebRequest.startAsync();
assertThatIllegalArgumentException().isThrownBy(() ->
this.asyncManager.setAsyncWebRequest(null));
assertThatIllegalArgumentException().isThrownBy(() -> this.asyncManager.setAsyncWebRequest(null));
}
@Test
public void startCallableProcessing() throws Exception {
int concurrentResult = 21;
Callable<Object> task = new StubCallable(concurrentResult);
@ -122,7 +120,6 @@ public class WebAsyncManagerTests { @@ -122,7 +120,6 @@ public class WebAsyncManagerTests {
@Test
public void startCallableProcessingCallableException() throws Exception {
Exception concurrentResult = new Exception();
Callable<Object> task = new StubCallable(concurrentResult);
@ -152,8 +149,8 @@ public class WebAsyncManagerTests { @@ -152,8 +149,8 @@ public class WebAsyncManagerTests {
this.asyncManager.registerCallableInterceptor("interceptor", interceptor);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
this.asyncManager.startCallableProcessing(task))
assertThatException()
.isThrownBy(() -> this.asyncManager.startCallableProcessing(task))
.isEqualTo(exception);
assertThat(this.asyncManager.hasConcurrentResult()).isFalse();
@ -248,8 +245,8 @@ public class WebAsyncManagerTests { @@ -248,8 +245,8 @@ public class WebAsyncManagerTests {
@Test
public void startCallableProcessingNullInput() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() ->
this.asyncManager.startCallableProcessing((Callable<?>) null))
assertThatIllegalArgumentException()
.isThrownBy(() -> this.asyncManager.startCallableProcessing((Callable<?>) null))
.withMessage("Callable must not be null");
}
@ -285,8 +282,8 @@ public class WebAsyncManagerTests { @@ -285,8 +282,8 @@ public class WebAsyncManagerTests {
this.asyncManager.registerDeferredResultInterceptor("interceptor", interceptor);
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
this.asyncManager.startDeferredResultProcessing(deferredResult))
assertThatException()
.isThrownBy(() -> this.asyncManager.startDeferredResultProcessing(deferredResult))
.isEqualTo(exception);
assertThat(this.asyncManager.hasConcurrentResult()).isFalse();
@ -298,7 +295,6 @@ public class WebAsyncManagerTests { @@ -298,7 +295,6 @@ public class WebAsyncManagerTests {
@Test
public void startDeferredResultProcessingPreProcessException() throws Exception {
DeferredResult<Integer> deferredResult = new DeferredResult<>();
Exception exception = new Exception();
@ -340,8 +336,8 @@ public class WebAsyncManagerTests { @@ -340,8 +336,8 @@ public class WebAsyncManagerTests {
@Test
public void startDeferredResultProcessingNullInput() throws Exception {
assertThatIllegalArgumentException().isThrownBy(() ->
this.asyncManager.startDeferredResultProcessing(null))
assertThatIllegalArgumentException()
.isThrownBy(() -> this.asyncManager.startDeferredResultProcessing(null))
.withMessage("DeferredResult must not be null");
}

7
spring-web/src/test/java/org/springframework/web/method/annotation/WebArgumentResolverAdapterTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -28,7 +28,7 @@ import org.springframework.web.context.request.ServletWebRequest; @@ -28,7 +28,7 @@ import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@ -131,8 +131,7 @@ public class WebArgumentResolverAdapterTests { @@ -131,8 +131,7 @@ public class WebArgumentResolverAdapterTests {
public void resolveArgumentThrowsException() throws Exception {
given(adaptee.resolveArgument(parameter, webRequest)).willThrow(new Exception());
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
adapter.resolveArgument(parameter, null, webRequest, null));
assertThatException().isThrownBy(() -> adapter.resolveArgument(parameter, null, webRequest, null));
}
public void handle(int param) {

24
spring-web/src/test/java/org/springframework/web/method/support/InvocableHandlerMethodTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -30,9 +30,11 @@ import org.springframework.web.testfixture.servlet.MockHttpServletRequest; @@ -30,9 +30,11 @@ import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Unit tests for {@link InvocableHandlerMethod}.
@ -127,23 +129,23 @@ public class InvocableHandlerMethodTests { @@ -127,23 +129,23 @@ public class InvocableHandlerMethodTests {
@Test
public void invocationTargetException() throws Exception {
RuntimeException runtimeException = new RuntimeException("error");
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
getInvocable(Throwable.class).invokeForRequest(this.request, null, runtimeException))
assertThatRuntimeException()
.isThrownBy(() -> getInvocable(Throwable.class).invokeForRequest(this.request, null, runtimeException))
.isSameAs(runtimeException);
Error error = new Error("error");
assertThatExceptionOfType(Error.class).isThrownBy(() ->
getInvocable(Throwable.class).invokeForRequest(this.request, null, error))
assertThatExceptionOfType(Error.class)
.isThrownBy(() -> getInvocable(Throwable.class).invokeForRequest(this.request, null, error))
.isSameAs(error);
Exception exception = new Exception("error");
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
getInvocable(Throwable.class).invokeForRequest(this.request, null, exception))
assertThatException()
.isThrownBy(() -> getInvocable(Throwable.class).invokeForRequest(this.request, null, exception))
.isSameAs(exception);
Throwable throwable = new Throwable("error");
assertThatIllegalStateException().isThrownBy(() ->
getInvocable(Throwable.class).invokeForRequest(this.request, null, throwable))
assertThatIllegalStateException()
.isThrownBy(() -> getInvocable(Throwable.class).invokeForRequest(this.request, null, throwable))
.withCause(throwable)
.withMessageContaining("Invocation failure");
}
@ -151,8 +153,8 @@ public class InvocableHandlerMethodTests { @@ -151,8 +153,8 @@ public class InvocableHandlerMethodTests {
@Test // SPR-13917
public void invocationErrorMessage() throws Exception {
this.composite.addResolver(new StubArgumentResolver(double.class));
assertThatIllegalStateException().isThrownBy(() ->
getInvocable(double.class).invokeForRequest(this.request, null))
assertThatIllegalStateException()
.isThrownBy(() -> getInvocable(double.class).invokeForRequest(this.request, null))
.withMessageContaining("Illegal argument");
}

16
spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolutionIntegrationTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -40,7 +40,7 @@ import org.springframework.web.testfixture.servlet.MockServletConfig; @@ -40,7 +40,7 @@ import org.springframework.web.testfixture.servlet.MockServletConfig;
import org.springframework.web.testfixture.servlet.MockServletContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Integration tests for view resolution with {@code @EnableWebMvc}.
@ -70,22 +70,22 @@ public class ViewResolutionIntegrationTests { @@ -70,22 +70,22 @@ public class ViewResolutionIntegrationTests {
@Test
public void freemarkerInvalidConfig() throws Exception {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
runTest(InvalidFreeMarkerWebConfig.class))
assertThatRuntimeException()
.isThrownBy(() -> runTest(InvalidFreeMarkerWebConfig.class))
.withMessageContaining("In addition to a FreeMarker view resolver ");
}
@Test
public void tilesInvalidConfig() throws Exception {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
runTest(InvalidTilesWebConfig.class))
assertThatRuntimeException()
.isThrownBy(() -> runTest(InvalidTilesWebConfig.class))
.withMessageContaining("In addition to a Tiles view resolver ");
}
@Test
public void groovyMarkupInvalidConfig() throws Exception {
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() ->
runTest(InvalidGroovyMarkupWebConfig.class))
assertThatRuntimeException()
.isThrownBy(() -> runTest(InvalidGroovyMarkupWebConfig.class))
.withMessageContaining("In addition to a Groovy markup view resolver ");
}

Loading…
Cancel
Save