Use AssertJ’s exception assertions rather than fail

Closes gh-15761
This commit is contained in:
Andy Wilkinson
2019-02-04 11:48:14 +00:00
parent 9357a92503
commit 82bc87560c
26 changed files with 264 additions and 428 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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,6 @@ import org.springframework.format.support.DefaultFormattingConversionService;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -61,15 +60,14 @@ public class ConversionServiceParameterValueMapperTests {
given(conversionService.convert(any(), any())).willThrow(error);
ConversionServiceParameterValueMapper mapper = new ConversionServiceParameterValueMapper(
conversionService);
try {
mapper.mapParameterValue(new TestOperationParameter(Integer.class), "123");
fail("Did not throw");
}
catch (ParameterMappingException ex) {
assertThat(ex.getValue()).isEqualTo("123");
assertThat(ex.getParameter().getType()).isEqualTo(Integer.class);
assertThat(ex.getCause()).isEqualTo(error);
}
assertThatExceptionOfType(ParameterMappingException.class)
.isThrownBy(() -> mapper.mapParameterValue(
new TestOperationParameter(Integer.class), "123"))
.satisfies((ex) -> {
assertThat(ex.getValue()).isEqualTo("123");
assertThat(ex.getParameter().getType()).isEqualTo(Integer.class);
assertThat(ex.getCause()).isEqualTo(error);
});
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -36,7 +36,7 @@ import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchangeDecorator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -123,19 +123,14 @@ public class HttpTraceWebFilterTests {
@Test
public void statusIsAssumedToBe500WhenChainFails()
throws ServletException, IOException {
try {
this.filter
.filter(MockServerWebExchange
.from(MockServerHttpRequest.get("https://api.example.com")),
(exchange) -> Mono.error(new RuntimeException()))
.block(Duration.ofSeconds(30));
fail();
}
catch (Exception ex) {
assertThat(this.repository.findAll()).hasSize(1);
assertThat(this.repository.findAll().get(0).getResponse().getStatus())
.isEqualTo(500);
}
assertThatExceptionOfType(Exception.class).isThrownBy(() -> this.filter
.filter(MockServerWebExchange
.from(MockServerHttpRequest.get("https://api.example.com")),
(exchange) -> Mono.error(new RuntimeException()))
.block(Duration.ofSeconds(30)));
assertThat(this.repository.findAll()).hasSize(1);
assertThat(this.repository.findAll().get(0).getResponse().getStatus())
.isEqualTo(500);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -106,25 +106,24 @@ public class HttpTraceFilterTests {
@Test
public void statusIsAssumedToBe500WhenChainFails()
throws ServletException, IOException {
try {
this.filter.doFilter(new MockHttpServletRequest(),
new MockHttpServletResponse(), new MockFilterChain(new HttpServlet() {
assertThatIOException()
.isThrownBy(() -> this.filter.doFilter(new MockHttpServletRequest(),
new MockHttpServletResponse(),
new MockFilterChain(new HttpServlet() {
@Override
protected void service(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
throw new IOException();
}
@Override
protected void service(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
throw new IOException();
}
}));
fail("Filter swallowed IOException");
}
catch (IOException ex) {
assertThat(this.repository.findAll()).hasSize(1);
assertThat(this.repository.findAll().get(0).getResponse().getStatus())
.isEqualTo(500);
}
})))
.satisfies((ex) -> {
assertThat(this.repository.findAll()).hasSize(1);
assertThat(this.repository.findAll().get(0).getResponse().getStatus())
.isEqualTo(500);
});
}
@Test