Polish tests and test fixtures in spring-test

This commit is contained in:
Sam Brannen
2021-10-14 22:12:23 +02:00
parent 9b4f3880b3
commit b8fc79543d
11 changed files with 19 additions and 18 deletions

View File

@@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
*/ */
class MockHttpSessionTests { class MockHttpSessionTests {
private MockHttpSession session = new MockHttpSession(); private final MockHttpSession session = new MockHttpSession();
@Test @Test

View File

@@ -118,7 +118,7 @@ class TestContextConcurrencyTests {
private static class TrackingListener implements TestExecutionListener { private static class TrackingListener implements TestExecutionListener {
private ThreadLocal<String> methodName = new ThreadLocal<>(); private final ThreadLocal<String> methodName = new ThreadLocal<>();
@Override @Override

View File

@@ -47,7 +47,7 @@ import static org.springframework.test.context.cache.ContextCacheTestUtils.asser
*/ */
class ContextCacheTests { class ContextCacheTests {
private ContextCache contextCache = new DefaultContextCache(); private final ContextCache contextCache = new DefaultContextCache();
@BeforeEach @BeforeEach

View File

@@ -38,7 +38,7 @@ import static org.mockito.Mockito.mock;
*/ */
public class SpringFailOnTimeoutTests { public class SpringFailOnTimeoutTests {
private Statement statement = mock(Statement.class); private final Statement statement = mock(Statement.class);
@Test @Test

View File

@@ -68,16 +68,16 @@ public class Component {
int add(int... args) { int add(int... args) {
int sum = 0; int sum = 0;
for (int i = 0; i < args.length; i++) { for (int arg : args) {
sum += args[i]; sum += arg;
} }
return sum; return sum;
} }
int multiply(Integer... args) { int multiply(Integer... args) {
int product = 1; int product = 1;
for (int i = 0; i < args.length; i++) { for (Integer arg : args) {
product *= args[i]; product *= arg;
} }
return product; return product;
} }

View File

@@ -70,10 +70,9 @@ public class Person {
if (this == other) { if (this == other) {
return true; return true;
} }
if (!(other instanceof Person)) { if (!(other instanceof Person otherPerson)) {
return false; return false;
} }
Person otherPerson = (Person) other;
return (ObjectUtils.nullSafeEquals(this.name, otherPerson.name) && return (ObjectUtils.nullSafeEquals(this.name, otherPerson.name) &&
ObjectUtils.nullSafeEquals(this.someDouble, otherPerson.someDouble) && ObjectUtils.nullSafeEquals(this.someDouble, otherPerson.someDouble) &&
ObjectUtils.nullSafeEquals(this.someBoolean, otherPerson.someBoolean)); ObjectUtils.nullSafeEquals(this.someBoolean, otherPerson.someBoolean));

View File

@@ -45,8 +45,12 @@ class Person {
@Override @Override
public boolean equals(Object other) { public boolean equals(Object other) {
if (this == other) return true; if (this == other) {
if (other == null || getClass() != other.getClass()) return false; return true;
}
if (other == null || getClass() != other.getClass()) {
return false;
}
Person person = (Person) other; Person person = (Person) other;
return getName().equals(person.getName()); return getName().equals(person.getName());
} }

View File

@@ -347,7 +347,7 @@ public class PrintingResultHandlerTests {
private String printedHeading; private String printedHeading;
private Map<String, Map<String, Object>> printedValues = new HashMap<>(); private final Map<String, Map<String, Object>> printedValues = new HashMap<>();
@Override @Override
public void printHeading(String heading) { public void printHeading(String heading) {

View File

@@ -117,9 +117,7 @@ public class EncodedUriTests {
@Override @Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RequestMappingHandlerMapping) { if (bean instanceof RequestMappingHandlerMapping requestMappingHandlerMapping) {
RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) bean;
// URL decode after request mapping, not before. // URL decode after request mapping, not before.
requestMappingHandlerMapping.setUrlDecode(false); requestMappingHandlerMapping.setUrlDecode(false);
} }

View File

@@ -86,7 +86,7 @@ public class HttpOptionsTests {
@Controller @Controller
private static class MyController { private static class MyController {
private AtomicInteger counter = new AtomicInteger(); private final AtomicInteger counter = new AtomicInteger();
@RequestMapping(value = "/myUrl", method = RequestMethod.OPTIONS) @RequestMapping(value = "/myUrl", method = RequestMethod.OPTIONS)

View File

@@ -85,7 +85,7 @@ public class FrameworkExtensionTests {
*/ */
private static class TestRequestPostProcessor implements RequestPostProcessor { private static class TestRequestPostProcessor implements RequestPostProcessor {
private HttpHeaders headers = new HttpHeaders(); private final HttpHeaders headers = new HttpHeaders();
public TestRequestPostProcessor foo(String value) { public TestRequestPostProcessor foo(String value) {