Refactor AssertJ assertions into more idiomatic ones

This commit refactors some AssertJ assertions into more idiomatic and
readable ones. Using the dedicated assertion instead of a generic one
will produce more meaningful error messages. 

For instance, consider collection size:
```
// expected: 5 but was: 2
assertThat(collection.size()).equals(5);
// Expected size: 5 but was: 2 in: [1, 2]
assertThat(collection).hasSize(5);
```

Closes gh-30104
This commit is contained in:
Krzysztof Krasoń
2023-04-04 17:34:07 +02:00
committed by GitHub
parent dd97ee4e99
commit 1734deca1e
371 changed files with 3177 additions and 3076 deletions

View File

@@ -93,11 +93,11 @@ public class CaffeineCacheManagerTests {
Cache cache1x = cm.getCache("c1");
boolean condition1 = cache1x instanceof CaffeineCache;
assertThat(condition1).isTrue();
assertThat(cache1x != cache1).isTrue();
assertThat(cache1x).isNotSameAs(cache1);
Cache cache2x = cm.getCache("c2");
boolean condition = cache2x instanceof CaffeineCache;
assertThat(condition).isTrue();
assertThat(cache2x != cache2).isTrue();
assertThat(cache2x).isNotSameAs(cache2);
Cache cache3x = cm.getCache("c3");
assertThat(cache3x).isNull();
@@ -123,7 +123,7 @@ public class CaffeineCacheManagerTests {
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().maximumSize(10);
cm.setCaffeine(caffeine);
Cache cache1x = cm.getCache("c1");
assertThat(cache1x != cache1).isTrue();
assertThat(cache1x).isNotSameAs(cache1);
cm.setCaffeine(caffeine); // Set same instance
Cache cache1xx = cm.getCache("c1");
@@ -137,7 +137,7 @@ public class CaffeineCacheManagerTests {
cm.setCaffeineSpec(CaffeineSpec.parse("maximumSize=10"));
Cache cache1x = cm.getCache("c1");
assertThat(cache1x != cache1).isTrue();
assertThat(cache1x).isNotSameAs(cache1);
}
@Test
@@ -147,7 +147,7 @@ public class CaffeineCacheManagerTests {
cm.setCacheSpecification("maximumSize=10");
Cache cache1x = cm.getCache("c1");
assertThat(cache1x != cache1).isTrue();
assertThat(cache1x).isNotSameAs(cache1);
}
@Test
@@ -160,7 +160,7 @@ public class CaffeineCacheManagerTests {
cm.setCacheLoader(loader);
Cache cache1x = cm.getCache("c1");
assertThat(cache1x != cache1).isTrue();
assertThat(cache1x).isNotSameAs(cache1);
cm.setCacheLoader(loader); // Set same instance
Cache cache1xx = cm.getCache("c1");
@@ -201,11 +201,11 @@ public class CaffeineCacheManagerTests {
Cache cache1 = cm.getCache("c1");
Cache cache2 = cm.getCache("c2");
assertThat(nc == cache2.getNativeCache()).isTrue();
assertThat(nc).isSameAs(cache2.getNativeCache());
cm.setCaffeine(Caffeine.newBuilder().maximumSize(10));
assertThat(cm.getCache("c1") != cache1).isTrue();
assertThat(cm.getCache("c2") == cache2).isTrue();
assertThat(cm.getCache("c1")).isNotSameAs(cache1);
assertThat(cm.getCache("c2")).isSameAs(cache2);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@@ -72,7 +72,7 @@ public class JCacheCustomInterceptorTests {
@Test
public void onlyOneInterceptorIsAvailable() {
Map<String, JCacheInterceptor> interceptors = ctx.getBeansOfType(JCacheInterceptor.class);
assertThat(interceptors.size()).as("Only one interceptor should be defined").isEqualTo(1);
assertThat(interceptors).as("Only one interceptor should be defined").hasSize(1);
JCacheInterceptor interceptor = interceptors.values().iterator().next();
assertThat(interceptor.getClass()).as("Custom interceptor not defined").isEqualTo(TestCacheInterceptor.class);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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,8 @@ public abstract class AbstractCacheOperationTests<O extends JCacheOperation<?>>
public void simple() {
O operation = createSimpleOperation();
assertThat(operation.getCacheName()).as("Wrong cache name").isEqualTo("simpleCache");
assertThat(operation.getAnnotations().size()).as("Unexpected number of annotation on " + operation.getMethod()).isEqualTo(1);
assertThat(operation.getAnnotations()).as("Unexpected number of annotation on " + operation.getMethod())
.hasSize(1);
assertThat(operation.getAnnotations().iterator().next()).as("Wrong method annotation").isEqualTo(operation.getCacheAnnotation());
assertThat(operation.getCacheResolver()).as("cache resolver should be set").isNotNull();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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,25 +37,25 @@ public class InternetAddressEditorTests {
@Test
public void uninitialized() {
assertThat(editor.getAsText()).as("Uninitialized editor did not return empty value string").isEqualTo(EMPTY);
assertThat(editor.getAsText()).as("Uninitialized editor did not return empty value string").isEmpty();
}
@Test
public void setNull() {
editor.setAsText(null);
assertThat(editor.getAsText()).as("Setting null did not result in empty value string").isEqualTo(EMPTY);
assertThat(editor.getAsText()).as("Setting null did not result in empty value string").isEmpty();
}
@Test
public void setEmpty() {
editor.setAsText(EMPTY);
assertThat(editor.getAsText()).as("Setting empty string did not result in empty value string").isEqualTo(EMPTY);
assertThat(editor.getAsText()).as("Setting empty string did not result in empty value string").isEmpty();
}
@Test
public void allWhitespace() {
editor.setAsText(" ");
assertThat(editor.getAsText()).as("All whitespace was not recognized").isEqualTo(EMPTY);
assertThat(editor.getAsText()).as("All whitespace was not recognized").isEmpty();
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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,8 +39,8 @@ public class QuartzSchedulerLifecycleTests {
sw.start("lazyScheduler");
context.close();
sw.stop();
assertThat(sw.getTotalTimeMillis() < 500).as("Quartz Scheduler with lazy-init is hanging on destruction: " +
sw.getTotalTimeMillis()).isTrue();
assertThat(sw.getTotalTimeMillis()).as("Quartz Scheduler with lazy-init is hanging on destruction: " +
sw.getTotalTimeMillis()).isLessThan(500);
}
@Test // SPR-6354
@@ -52,8 +52,8 @@ public class QuartzSchedulerLifecycleTests {
sw.start("lazyScheduler");
context.close();
sw.stop();
assertThat(sw.getTotalTimeMillis() < 500).as("Quartz Scheduler with lazy-init is hanging on destruction: " +
sw.getTotalTimeMillis()).isTrue();
assertThat(sw.getTotalTimeMillis()).as("Quartz Scheduler with lazy-init is hanging on destruction: " +
sw.getTotalTimeMillis()).isLessThan(500);
}
}

View File

@@ -126,7 +126,7 @@ class QuartzSupportTests {
bean.start();
Thread.sleep(500);
assertThat(DummyJob.count > 0).as("DummyJob should have been executed at least once.").isTrue();
assertThat(DummyJob.count).as("DummyJob should have been executed at least once.").isGreaterThan(0);
assertThat(taskExecutor.count).isEqualTo(DummyJob.count);
bean.destroy();
@@ -168,7 +168,7 @@ class QuartzSupportTests {
Thread.sleep(500);
assertThat(DummyJobBean.param).isEqualTo(10);
assertThat(DummyJobBean.count > 0).isTrue();
assertThat(DummyJobBean.count).isGreaterThan(0);
bean.destroy();
}
@@ -203,7 +203,7 @@ class QuartzSupportTests {
Thread.sleep(500);
assertThat(DummyJob.param).isEqualTo(10);
assertThat(DummyJob.count > 0).as("DummyJob should have been executed at least once.").isTrue();
assertThat(DummyJob.count).as("DummyJob should have been executed at least once.").isGreaterThan(0);
bean.destroy();
}
@@ -239,7 +239,7 @@ class QuartzSupportTests {
Thread.sleep(500);
assertThat(DummyJob.param).isEqualTo(0);
assertThat(DummyJob.count == 0).isTrue();
assertThat(DummyJob.count).isEqualTo(0);
bean.destroy();
}
@@ -273,7 +273,7 @@ class QuartzSupportTests {
Thread.sleep(500);
assertThat(DummyJobBean.param).isEqualTo(10);
assertThat(DummyJobBean.count > 0).isTrue();
assertThat(DummyJobBean.count).isGreaterThan(0);
bean.destroy();
}
@@ -292,7 +292,7 @@ class QuartzSupportTests {
Thread.sleep(500);
assertThat(DummyJob.param).isEqualTo(10);
assertThat(DummyJob.count > 0).as("DummyJob should have been executed at least once.").isTrue();
assertThat(DummyJob.count).as("DummyJob should have been executed at least once.").isGreaterThan(0);
bean.destroy();
}