Simplify AssertJ assertions and also make them more readable

See gh-33653
This commit is contained in:
Krzysztof Krason
2022-12-29 17:52:45 +01:00
committed by Moritz Halbritter
parent c9a2b2ab66
commit cf6493f65c
345 changed files with 1258 additions and 1292 deletions

View File

@@ -68,7 +68,7 @@ abstract class AbstractSpringBootTestEmbeddedReactiveWebEnvironmentTests {
@Test
void runAndTestHttpEndpoint() {
assertThat(this.port).isNotEqualTo(8080).isNotEqualTo(0);
assertThat(this.port).isNotEqualTo(8080).isNotZero();
WebTestClient.bindToServer().baseUrl("http://localhost:" + this.port).responseTimeout(Duration.ofMinutes(5))
.build().get().uri("/").exchange().expectBody(String.class).isEqualTo("Hello World");
}

View File

@@ -70,7 +70,7 @@ abstract class AbstractSpringBootTestWebServerWebEnvironmentTests {
@Test
void runAndTestHttpEndpoint() {
assertThat(this.port).isNotEqualTo(8080).isNotEqualTo(0);
assertThat(this.port).isNotEqualTo(8080).isNotZero();
String body = new RestTemplate().getForObject("http://localhost:" + this.port + "/", String.class);
assertThat(body).isEqualTo("Hello World");
}

View File

@@ -68,8 +68,8 @@ class ImportsContextCustomizerFactoryTests {
ContextCustomizer customizer3 = this.factory.createContextCustomizer(TestWithImportAndMetaImport.class, null);
ContextCustomizer customizer4 = this.factory.createContextCustomizer(TestWithSameImportAndMetaImport.class,
null);
assertThat(customizer1.hashCode()).isEqualTo(customizer1.hashCode());
assertThat(customizer1.hashCode()).isEqualTo(customizer2.hashCode());
assertThat(customizer1).hasSameHashCodeAs(customizer1);
assertThat(customizer1).hasSameHashCodeAs(customizer2);
assertThat(customizer1).isEqualTo(customizer1).isEqualTo(customizer2).isNotEqualTo(customizer3);
assertThat(customizer3).isEqualTo(customizer4);
}

View File

@@ -256,8 +256,8 @@ class SpringBootContextLoaderTests {
}
private void assertKey(Map<String, Object> actual, String key, Object value) {
assertThat(actual.containsKey(key)).as("Key '" + key + "' not found").isTrue();
assertThat(actual.get(key)).isEqualTo(value);
assertThat(actual).as("Key '" + key + "' not found").containsKey(key);
assertThat(actual).containsEntry(key, value);
}
@SpringBootTest(properties = { "key=myValue", "anotherKey:anotherValue" }, classes = Config.class)

View File

@@ -35,14 +35,14 @@ class TestConfigurationTests {
void proxyBeanMethodsIsEnabledByDefault() {
AnnotationAttributes attributes = AnnotatedElementUtils
.getMergedAnnotationAttributes(DefaultTestConfiguration.class, Configuration.class);
assertThat(attributes.get("proxyBeanMethods")).isEqualTo(true);
assertThat(attributes).containsEntry("proxyBeanMethods", true);
}
@Test
void proxyBeanMethodsCanBeDisabled() {
AnnotationAttributes attributes = AnnotatedElementUtils
.getMergedAnnotationAttributes(NoBeanMethodProxyingTestConfiguration.class, Configuration.class);
assertThat(attributes.get("proxyBeanMethods")).isEqualTo(false);
assertThat(attributes).containsEntry("proxyBeanMethods", false);
}
@TestConfiguration

View File

@@ -178,7 +178,7 @@ class ApplicationContextAssertProviderTests {
@Test
void toStringWhenContextFailsToStartShouldReturnSimpleString() {
ApplicationContextAssertProvider<ApplicationContext> context = get(this.startupFailureSupplier);
assertThat(context.toString()).isEqualTo("Unstarted application context "
assertThat(context).hasToString("Unstarted application context "
+ "org.springframework.context.ApplicationContext[startupFailure=java.lang.RuntimeException]");
}

View File

@@ -71,19 +71,19 @@ abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicati
@Test
void runWithSystemPropertiesShouldSetAndRemoveProperties() {
String key = "test." + UUID.randomUUID();
assertThat(System.getProperties().containsKey(key)).isFalse();
assertThat(System.getProperties()).doesNotContainKey(key);
get().withSystemProperties(key + "=value")
.run((context) -> assertThat(System.getProperties()).containsEntry(key, "value"));
assertThat(System.getProperties().containsKey(key)).isFalse();
assertThat(System.getProperties()).doesNotContainKey(key);
}
@Test
void runWithSystemPropertiesWhenContextFailsShouldRemoveProperties() {
String key = "test." + UUID.randomUUID();
assertThat(System.getProperties().containsKey(key)).isFalse();
assertThat(System.getProperties()).doesNotContainKey(key);
get().withSystemProperties(key + "=value").withUserConfiguration(FailingConfig.class)
.run((context) -> assertThat(context).hasFailed());
assertThat(System.getProperties().containsKey(key)).isFalse();
assertThat(System.getProperties()).doesNotContainKey(key);
}
@Test

View File

@@ -117,7 +117,7 @@ class JacksonTesterIntegrationTests {
ObjectContent<ExampleObjectWithView> content = this.jsonWithView.forView(ExampleObjectWithView.TestView.class)
.read(resource);
assertThat(content.getObject().getName()).isEqualTo("Spring");
assertThat(content.getObject().getAge()).isEqualTo(0);
assertThat(content.getObject().getAge()).isZero();
}
@Test
@@ -127,7 +127,7 @@ class JacksonTesterIntegrationTests {
ObjectContent<ExampleObjectWithView> content = this.jsonWithView.forView(ExampleObjectWithView.TestView.class)
.read(reader);
assertThat(content.getObject().getName()).isEqualTo("Spring");
assertThat(content.getObject().getAge()).isEqualTo(0);
assertThat(content.getObject().getAge()).isZero();
}
}

View File

@@ -66,7 +66,7 @@ class MockBeanContextCachingTests {
@Test
void whenThereIsANormalBeanAndAMockBeanThenTwoContextsAreCreated() {
bootstrapContext(TestClass.class);
assertThat(this.contextCache.size()).isEqualTo(1);
assertThat(this.contextCache.size()).isOne();
bootstrapContext(MockedBeanTestClass.class);
assertThat(this.contextCache.size()).isEqualTo(2);
}
@@ -74,9 +74,9 @@ class MockBeanContextCachingTests {
@Test
void whenThereIsTheSameMockedBeanInEachTestClassThenOneContextIsCreated() {
bootstrapContext(MockedBeanTestClass.class);
assertThat(this.contextCache.size()).isEqualTo(1);
assertThat(this.contextCache.size()).isOne();
bootstrapContext(AnotherMockedBeanTestClass.class);
assertThat(this.contextCache.size()).isEqualTo(1);
assertThat(this.contextCache.size()).isOne();
}
@SuppressWarnings("rawtypes")

View File

@@ -52,8 +52,8 @@ class MockBeanOnContextHierarchyIntegrationTests {
ApplicationContext context = this.childConfig.getContext();
ApplicationContext parentContext = context.getParent();
assertThat(parentContext.getBeanNamesForType(ExampleService.class)).hasSize(1);
assertThat(parentContext.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(0);
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(0);
assertThat(parentContext.getBeanNamesForType(ExampleServiceCaller.class)).isEmpty();
assertThat(context.getBeanNamesForType(ExampleService.class)).isEmpty();
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1);
assertThat(context.getBean(ExampleService.class)).isNotNull();
assertThat(context.getBean(ExampleServiceCaller.class)).isNotNull();

View File

@@ -56,7 +56,7 @@ class MockBeanWithAopProxyTests {
void verifyShouldUseProxyTarget() {
given(this.dateService.getDate(false)).willReturn(1L);
Long d1 = this.dateService.getDate(false);
assertThat(d1).isEqualTo(1L);
assertThat(d1).isOne();
given(this.dateService.getDate(false)).willReturn(2L);
Long d2 = this.dateService.getDate(false);
assertThat(d2).isEqualTo(2L);

View File

@@ -82,7 +82,7 @@ class MockDefinitionTests {
MockCreationSettings<?> settings = Mockito.mockingDetails(mock).getMockCreationSettings();
assertThat(mock).isInstanceOf(ExampleService.class);
assertThat(mock).isInstanceOf(ExampleExtraInterface.class);
assertThat(settings.getMockName().toString()).isEqualTo("name");
assertThat(settings.getMockName()).hasToString("name");
assertThat(settings.getDefaultAnswer()).isEqualTo(Answers.RETURNS_SMART_NULLS);
assertThat(settings.isSerializable()).isTrue();
assertThat(MockReset.get(mock)).isEqualTo(MockReset.BEFORE);

View File

@@ -51,11 +51,9 @@ class MockitoContextCustomizerFactoryTests {
assertThat(customizer).isNotNull();
ContextCustomizer different = this.factory.createContextCustomizer(WithDifferentMockBeanAnnotation.class, null);
assertThat(different).isNotNull();
assertThat(customizer.hashCode()).isEqualTo(same.hashCode());
assertThat(customizer).hasSameHashCodeAs(same);
assertThat(customizer.hashCode()).isNotEqualTo(different.hashCode());
assertThat(customizer).isEqualTo(customizer);
assertThat(customizer).isEqualTo(same);
assertThat(customizer).isNotEqualTo(different);
assertThat(customizer).isEqualTo(customizer).isEqualTo(same).isNotEqualTo(different);
}
static class NoMockBeanAnnotation {

View File

@@ -45,7 +45,7 @@ class MockitoContextCustomizerTests {
MockitoContextCustomizer c1 = new MockitoContextCustomizer(NO_DEFINITIONS);
MockitoContextCustomizer c2 = new MockitoContextCustomizer(new LinkedHashSet<>(Arrays.asList(d1, d2)));
MockitoContextCustomizer c3 = new MockitoContextCustomizer(new LinkedHashSet<>(Arrays.asList(d2, d1)));
assertThat(c2.hashCode()).isEqualTo(c3.hashCode());
assertThat(c2).hasSameHashCodeAs(c3);
assertThat(c1).isEqualTo(c1).isNotEqualTo(c2);
assertThat(c2).isEqualTo(c2).isEqualTo(c3).isNotEqualTo(c1);
}

View File

@@ -108,9 +108,9 @@ class QualifierDefinitionTests {
.forElement(ReflectionUtils.findField(ConfigA.class, "customQualifier"));
QualifierDefinition customQualifier2 = QualifierDefinition
.forElement(ReflectionUtils.findField(ConfigB.class, "customQualifier"));
assertThat(directQualifier1.hashCode()).isEqualTo(directQualifier2.hashCode());
assertThat(differentDirectQualifier1.hashCode()).isEqualTo(differentDirectQualifier2.hashCode());
assertThat(customQualifier1.hashCode()).isEqualTo(customQualifier2.hashCode());
assertThat(directQualifier1).hasSameHashCodeAs(directQualifier2);
assertThat(differentDirectQualifier1).hasSameHashCodeAs(differentDirectQualifier2);
assertThat(customQualifier1).hasSameHashCodeAs(customQualifier2);
assertThat(differentDirectQualifier1).isEqualTo(differentDirectQualifier1).isEqualTo(differentDirectQualifier2)
.isNotEqualTo(directQualifier2);
assertThat(directQualifier1).isEqualTo(directQualifier1).isEqualTo(directQualifier2)

View File

@@ -53,8 +53,8 @@ class SpyBeanOnContextHierarchyIntegrationTests {
ApplicationContext context = this.childConfig.getContext();
ApplicationContext parentContext = context.getParent();
assertThat(parentContext.getBeanNamesForType(ExampleService.class)).hasSize(1);
assertThat(parentContext.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(0);
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(0);
assertThat(parentContext.getBeanNamesForType(ExampleServiceCaller.class)).isEmpty();
assertThat(context.getBeanNamesForType(ExampleService.class)).isEmpty();
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1);
assertThat(context.getBean(ExampleService.class)).isNotNull();
assertThat(context.getBean(ExampleServiceCaller.class)).isNotNull();

View File

@@ -50,8 +50,7 @@ class SpyBeanOnTestFieldForMultipleExistingBeansWithOnePrimaryIntegrationTests {
@Test
void testSpying() {
assertThat(this.caller.sayGreeting()).isEqualTo("I say two");
assertThat(Mockito.mockingDetails(this.spy).getMockCreationSettings().getMockName().toString())
.isEqualTo("two");
assertThat(Mockito.mockingDetails(this.spy).getMockCreationSettings().getMockName()).hasToString("two");
then(this.spy).should().greeting();
}

View File

@@ -45,7 +45,7 @@ class SpyBeanWithNameOnTestFieldForMultipleExistingBeansTests {
void testSpying() {
MockingDetails mockingDetails = Mockito.mockingDetails(this.spy);
assertThat(mockingDetails.isSpy()).isTrue();
assertThat(mockingDetails.getMockCreationSettings().getMockName().toString()).isEqualTo("two");
assertThat(mockingDetails.getMockCreationSettings().getMockName()).hasToString("two");
}
@Configuration(proxyBeanMethods = false)

View File

@@ -72,7 +72,7 @@ class SpyDefinitionTests {
RealExampleService spy = definition.createSpy(new RealExampleService("hello"));
MockCreationSettings<?> settings = Mockito.mockingDetails(spy).getMockCreationSettings();
assertThat(spy).isInstanceOf(ExampleService.class);
assertThat(settings.getMockName().toString()).isEqualTo("name");
assertThat(settings.getMockName()).hasToString("name");
assertThat(settings.getDefaultAnswer()).isEqualTo(Answers.CALLS_REAL_METHODS);
assertThat(MockReset.get(spy)).isEqualTo(MockReset.BEFORE);
}

View File

@@ -90,28 +90,28 @@ class OutputCaptureTests {
System.out.print("A");
this.output.pop();
System.out.print("B");
assertThat(this.systemOut.toString()).isEqualTo("AB");
assertThat(this.systemOut).hasToString("AB");
}
@Test
void captureAlsoWritesToSystemOut() {
this.output.push();
System.out.print("A");
assertThat(this.systemOut.toString()).isEqualTo("A");
assertThat(this.systemOut).hasToString("A");
}
@Test
void captureAlsoWritesToSystemErr() {
this.output.push();
System.err.print("A");
assertThat(this.systemErr.toString()).isEqualTo("A");
assertThat(this.systemErr).hasToString("A");
}
@Test
void lengthReturnsCapturedLength() {
this.output.push();
System.out.print("ABC");
assertThat(this.output.length()).isEqualTo(3);
assertThat(this.output).hasSize(3);
}
@Test
@@ -137,7 +137,7 @@ class OutputCaptureTests {
@Test
void toStringReturnsAllCapturedOutput() {
pushAndPrint();
assertThat(this.output.toString()).isEqualTo("ABC");
assertThat(this.output).hasToString("ABC");
}
@Test
@@ -158,7 +158,7 @@ class OutputCaptureTests {
for (int i = 0; i < 10; i++) {
assertThat(this.output.getAll()).isEqualTo("ABC");
}
assertThat(this.output.buildCount).isEqualTo(1);
assertThat(this.output.buildCount).isOne();
System.out.print("X");
assertThat(this.output.getAll()).isEqualTo("ABCX");
assertThat(this.output.buildCount).isEqualTo(2);
@@ -170,7 +170,7 @@ class OutputCaptureTests {
for (int i = 0; i < 10; i++) {
assertThat(this.output.getOut()).isEqualTo("AC");
}
assertThat(this.output.buildCount).isEqualTo(1);
assertThat(this.output.buildCount).isOne();
System.out.print("X");
assertThat(this.output.getOut()).isEqualTo("ACX");
assertThat(this.output.buildCount).isEqualTo(2);
@@ -182,7 +182,7 @@ class OutputCaptureTests {
for (int i = 0; i < 10; i++) {
assertThat(this.output.getErr()).isEqualTo("B");
}
assertThat(this.output.buildCount).isEqualTo(1);
assertThat(this.output.buildCount).isOne();
System.err.print("X");
assertThat(this.output.getErr()).isEqualTo("BX");
assertThat(this.output.buildCount).isEqualTo(2);

View File

@@ -106,7 +106,7 @@ class SpringBootTestRandomPortEnvironmentPostProcessorTests {
this.propertySources.addLast(otherSource);
this.postProcessor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("");
assertThat(this.environment.getProperty("management.server.port")).isEmpty();
}
@Test
@@ -118,7 +118,7 @@ class SpringBootTestRandomPortEnvironmentPostProcessorTests {
.addLast(new MapPropertySource("other", Collections.singletonMap("management.server.port", "8080")));
this.postProcessor.postProcessEnvironment(this.environment, null);
assertThat(this.environment.getProperty("server.port")).isEqualTo("0");
assertThat(this.environment.getProperty("management.server.port")).isEqualTo("");
assertThat(this.environment.getProperty("management.server.port")).isEmpty();
}
@Test

View File

@@ -120,7 +120,7 @@ class TestRestTemplateTests {
@Test
void getRootUriRootUriNotSet() {
assertThat(new TestRestTemplate().getRootUri()).isEqualTo("");
assertThat(new TestRestTemplate().getRootUri()).isEmpty();
}
@Test