GH-288: More AssertJ Changes

Missed a few in the last commit.
This commit is contained in:
Gary Russell
2022-04-20 13:06:26 -04:00
committed by Artem Bilan
parent b577e29e4c
commit 39880e4eb1
7 changed files with 81 additions and 180 deletions

View File

@@ -33,7 +33,7 @@ import org.springframework.retry.policy.CircuitBreakerRetryPolicy;
import org.springframework.retry.support.RetrySynchronizationManager;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Dave Syer
@@ -47,34 +47,14 @@ public class CircuitBreakerTests {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
Service service = context.getBean(Service.class);
assertThat(AopUtils.isAopProxy(service)).isTrue();
try {
service.service();
fail("Expected exception");
}
catch (Exception e) {
}
assertThatExceptionOfType(Exception.class).isThrownBy(() -> service.service());
assertThat((Boolean) service.getContext().getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN)).isFalse();
try {
service.service();
fail("Expected exception");
}
catch (Exception e) {
}
assertThatExceptionOfType(Exception.class).isThrownBy(() -> service.service());
assertThat((Boolean) service.getContext().getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN)).isFalse();
try {
service.service();
fail("Expected exception");
}
catch (Exception e) {
}
assertThatExceptionOfType(Exception.class).isThrownBy(() -> service.service());
assertThat((Boolean) service.getContext().getAttribute(CircuitBreakerRetryPolicy.CIRCUIT_OPEN)).isTrue();
assertThat(service.getCount()).isEqualTo(3);
try {
service.service();
fail("Expected exception");
}
catch (Exception e) {
}
assertThatExceptionOfType(Exception.class).isThrownBy(() -> service.service());
// Not called again once circuit is open
assertThat(service.getCount()).isEqualTo(3);
service.expressionService();

View File

@@ -44,8 +44,9 @@ import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
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.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
/**
* @author Dave Syer
@@ -127,12 +128,7 @@ public class EnableRetryTests {
public void excludes() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
ExcludesService service = context.getBean(ExcludesService.class);
try {
service.service();
fail("Expected IllegalStateException");
}
catch (IllegalStateException e) {
}
assertThatIllegalStateException().isThrownBy(() -> service.service());
assertThat(service.getCount()).isEqualTo(1);
context.close();
}
@@ -142,12 +138,7 @@ public class EnableRetryTests {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
ExcludesOnlyService service = context.getBean(ExcludesOnlyService.class);
service.setExceptionToThrow(new IllegalStateException());
try {
service.service();
fail("Expected IllegalStateException");
}
catch (IllegalStateException e) {
}
assertThatExceptionOfType(Exception.class).isThrownBy(() -> service.service());
assertThat(service.getCount()).isEqualTo(1);
service.setExceptionToThrow(new IllegalArgumentException());
@@ -217,13 +208,7 @@ public class EnableRetryTests {
ExpressionService service = context.getBean(ExpressionService.class);
service.service1();
assertThat(service.getCount()).isEqualTo(3);
try {
service.service2();
fail("expected exception");
}
catch (RuntimeException e) {
assertThat(e.getMessage()).isEqualTo("this cannot be retried");
}
assertThatExceptionOfType(Exception.class).isThrownBy(() -> service.service2());
assertThat(service.getCount()).isEqualTo(4);
service.service3();
assertThat(service.getCount()).isEqualTo(9);

View File

@@ -40,12 +40,12 @@ import org.springframework.retry.listener.RetryListenerSupport;
import org.springframework.retry.policy.NeverRetryPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
public class RetryOperationsInterceptorTests {
@@ -194,13 +194,7 @@ public class RetryOperationsInterceptorTests {
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(new NeverRetryPolicy());
this.interceptor.setRetryOperations(template);
try {
this.service.service();
fail("Expected Exception.");
}
catch (Exception e) {
assertThat(e.getMessage()).startsWith("Not enough calls");
}
assertThatExceptionOfType(Exception.class).isThrownBy(() -> service.service());
assertThat(count).isEqualTo(1);
}
@@ -220,39 +214,32 @@ public class RetryOperationsInterceptorTests {
@Test
public void testIllegalMethodInvocationType() throws Throwable {
try {
this.interceptor.invoke(new MethodInvocation() {
@Override
public Method getMethod() {
return ClassUtils.getMethod(RetryOperationsInterceptorTests.class,
"testIllegalMethodInvocationType");
}
assertThatIllegalStateException().isThrownBy(() -> this.interceptor.invoke(new MethodInvocation() {
@Override
public Method getMethod() {
return ClassUtils.getMethod(RetryOperationsInterceptorTests.class, "testIllegalMethodInvocationType");
}
@Override
public Object[] getArguments() {
return null;
}
@Override
public Object[] getArguments() {
return null;
}
@Override
public AccessibleObject getStaticPart() {
return null;
}
@Override
public AccessibleObject getStaticPart() {
return null;
}
@Override
public Object getThis() {
return null;
}
@Override
public Object getThis() {
return null;
}
@Override
public Object proceed() {
return null;
}
});
fail("IllegalStateException expected");
}
catch (IllegalStateException e) {
assertThat(e.getMessage()).contains("MethodInvocation");
}
@Override
public Object proceed() {
return null;
}
})).withMessageContaining("MethodInvocation");
}
public static interface Service {
@@ -275,16 +262,18 @@ public class RetryOperationsInterceptorTests {
}
}
@SuppressWarnings("deprecation")
@Override
public void doTansactional() {
if (TransactionSynchronizationManager.isActualTransactionActive() && !this.enteredTransaction) {
transactionCount++;
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void beforeCompletion() {
ServiceImpl.this.enteredTransaction = false;
}
});
TransactionSynchronizationManager.registerSynchronization(
new org.springframework.transaction.support.TransactionSynchronizationAdapter() {
@Override
public void beforeCompletion() {
ServiceImpl.this.enteredTransaction = false;
}
});
this.enteredTransaction = true;
}
count++;

View File

@@ -42,7 +42,6 @@ import org.springframework.retry.support.RetryTemplate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
@@ -191,7 +190,7 @@ public class StatefulRetryOperationsInterceptorTests {
this.interceptor.invoke(invocation);
ArgumentCaptor<DefaultRetryState> captor = ArgumentCaptor.forClass(DefaultRetryState.class);
verify(template).execute(any(RetryCallback.class), eq(null), captor.capture());
assertNull(captor.getValue().getKey());
assertThat(captor.getValue().getKey()).isNull();
}
@SuppressWarnings("unchecked")

View File

@@ -26,7 +26,6 @@ import org.springframework.retry.interceptor.MethodInvocationRetryCallback;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
public class MethodInvocationRetryListenerSupportTests {
@@ -73,12 +72,7 @@ public class MethodInvocationRetryListenerSupportTests {
@Test
public void testOnError() {
MethodInvocationRetryListenerSupport support = new MethodInvocationRetryListenerSupport();
try {
support.onError(null, null, null);
}
catch (Exception e) {
fail("Unexpected exception");
}
assertThatNoException().isThrownBy(() -> support.onError(null, null, null));
}
@Test

View File

@@ -29,9 +29,8 @@ import org.springframework.retry.policy.NeverRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
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.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
public class RetryListenerTests {
@@ -70,16 +69,10 @@ public class RetryListenerTests {
return false;
}
});
try {
template.execute(context -> {
count++;
return null;
});
fail("Expected TerminatedRetryException");
}
catch (TerminatedRetryException e) {
// expected
}
assertThatExceptionOfType(TerminatedRetryException.class).isThrownBy(() -> template.execute(context -> {
count++;
return null;
}));
assertThat(count).isEqualTo(0);
assertThat(list).hasSize(1);
assertThat(list.get(0)).isEqualTo("1");
@@ -140,7 +133,7 @@ public class RetryListenerTests {
Throwable t) {
list.add("" + count);
// The last attempt should have been successful:
assertNull(t);
assertThat(t).isNull();
}
});
template.execute(context -> {

View File

@@ -35,6 +35,9 @@ import org.springframework.retry.policy.NeverRetryPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
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.assertThatIllegalStateException;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
@@ -115,16 +118,8 @@ public class RetryTemplateTests {
RetryTemplate retryTemplate = new RetryTemplate();
int retryAttempts = 2;
retryTemplate.setRetryPolicy(new SimpleRetryPolicy(retryAttempts));
try {
retryTemplate.execute(callback);
fail("Expected IllegalArgumentException");
}
catch (IllegalArgumentException e) {
assertThat(e).isNotNull();
assertThat(callback.attempts).isEqualTo(retryAttempts);
return;
}
fail("Expected IllegalArgumentException");
assertThatIllegalArgumentException().isThrownBy(() -> retryTemplate.execute(callback));
assertThat(callback.attempts).isEqualTo(retryAttempts);
}
@Test
@@ -199,37 +194,21 @@ public class RetryTemplateTests {
@Test
public void testEarlyTermination() {
try {
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.execute(status -> {
status.setExhaustedOnly();
throw new IllegalStateException("Retry this operation");
});
fail("Expected ExhaustedRetryException");
}
catch (IllegalStateException ex) {
// Expected for internal retry policy (external would recover
// gracefully)
assertThat(ex.getMessage()).isEqualTo("Retry this operation");
}
RetryTemplate retryTemplate = new RetryTemplate();
assertThatIllegalStateException().isThrownBy(() -> retryTemplate.execute(status -> {
status.setExhaustedOnly();
throw new IllegalStateException("Retry this operation");
})).withMessage("Retry this operation");
}
@Test
public void testEarlyTerminationWithOriginalException() {
try {
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setThrowLastExceptionOnExhausted(true);
retryTemplate.execute(status -> {
status.setExhaustedOnly();
throw new IllegalStateException("Retry this operation");
});
fail("Expected ExhaustedRetryException");
}
catch (IllegalStateException ex) {
// Expected for internal retry policy (external would recover
// gracefully)
assertThat(ex.getMessage()).isEqualTo("Retry this operation");
}
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setThrowLastExceptionOnExhausted(true);
assertThatIllegalStateException().isThrownBy(() -> retryTemplate.execute(status -> {
status.setExhaustedOnly();
throw new IllegalStateException("Retry this operation");
})).withMessage("Retry this operation");
}
@Test
@@ -280,15 +259,9 @@ public class RetryTemplateTests {
throw new RuntimeException("Planned");
}
});
try {
retryTemplate.execute(context -> {
throw new RuntimeException("Realllly bad!");
});
fail("Expected Error");
}
catch (TerminatedRetryException e) {
assertThat(e.getCause().getMessage()).isEqualTo("Planned");
}
assertThatExceptionOfType(TerminatedRetryException.class).isThrownBy(() -> retryTemplate.execute(context -> {
throw new RuntimeException("Realllly bad!");
})).withCauseInstanceOf(RuntimeException.class).withMessageContaining("Planned");
}
@Test
@@ -300,15 +273,9 @@ public class RetryTemplateTests {
throw new BackOffInterruptedException("foo");
}
});
try {
retryTemplate.execute(context -> {
throw new RuntimeException("Bad!");
});
fail("Expected RuntimeException");
}
catch (BackOffInterruptedException e) {
assertThat(e.getMessage()).isEqualTo("foo");
}
assertThatExceptionOfType(BackOffInterruptedException.class).isThrownBy(() -> retryTemplate.execute(context -> {
throw new RuntimeException("Bad!");
})).withMessage("foo");
}
/**
@@ -328,22 +295,16 @@ public class RetryTemplateTests {
given(bop.start(any())).willReturn(backOffContext);
try {
tested.execute(context -> {
throw new Exception("maybe next time!");
}, null, new DefaultRetryState(tested) {
assertThatExceptionOfType(Exception.class).isThrownBy(() -> tested.execute(context -> {
throw new Exception("maybe next time!");
}, null, new DefaultRetryState(tested) {
@Override
public boolean rollbackFor(Throwable exception) {
return true;
}
@Override
public boolean rollbackFor(Throwable exception) {
return true;
}
});
fail();
}
catch (Exception expected) {
assertThat(expected.getMessage()).isEqualTo("maybe next time!");
}
})).withMessage("maybe next time!");
verify(bop).start(any());
}