Skip to content

Commit

Permalink
refactor: AssertJ best practices (#3637)
Browse files Browse the repository at this point in the history
Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.java.testing.assertj.Assertj

Co-authored-by: Moderne <[email protected]>
  • Loading branch information
timtebeek and TeamModerne authored Dec 13, 2023
1 parent 2f01596 commit 6d6ba01
Show file tree
Hide file tree
Showing 23 changed files with 111 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static zipkin2.TestObjects.LOTS_OF_SPANS;
import static zipkin2.TestObjects.UTF_8;
import static zipkin2.codec.SpanBytesEncoder.PROTO3;
Expand Down Expand Up @@ -97,8 +96,7 @@ public class ITActiveMQCollector {
collector = builder().connectionFactory(connectionFactory).build();
collector.start();
});
assertTrue(exception.getMessage()
.contains("Unable to establish connection to ActiveMQ broker: Connection refused"));
assertThat(exception.getMessage()).contains("Unable to establish connection to ActiveMQ broker: Connection refused");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.Percentage.withPercentage;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static zipkin2.TestObjects.LOTS_OF_SPANS;

public class CollectorSamplerTest {
Expand Down Expand Up @@ -77,12 +76,12 @@ public class CollectorSamplerTest {

@Test void rateCantBeNegative() {
Throwable exception = assertThrows(IllegalArgumentException.class, () -> CollectorSampler.create(-1.0f));
assertTrue(exception.getMessage().contains("rate should be between 0 and 1: was -1.0"));
assertThat(exception.getMessage()).contains("rate should be between 0 and 1: was -1.0");
}

@Test void rateCantBeOverOne() {
Throwable exception = assertThrows(IllegalArgumentException.class, () -> CollectorSampler.create(1.1f));
assertTrue(exception.getMessage().contains("rate should be between 0 and 1: was 1.1"));
assertThat(exception.getMessage()).contains("rate should be between 0 and 1: was 1.1");
}

static Stream<Span> lotsOfSpans() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import zipkin2.server.internal.scribe.ZipkinScribeCollectorConfiguration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

public class ZipkinScribeCollectorConfigurationTest {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
Expand All @@ -33,7 +33,7 @@ public class ZipkinScribeCollectorConfigurationTest {
}

@Test void doesntProvidesCollectorComponent_byDefault() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
refreshContext();

context.getBean(ScribeCollector.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

public class ZipkinHttpConfigurationTest {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
Expand All @@ -50,7 +50,7 @@ public class ZipkinHttpConfigurationTest {
}

@Test void httpCollector_canDisable() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
TestPropertyValues.of("zipkin.collector.http.enabled:false").applyTo(context);
registerBaseConfig(context);
context.register(ZipkinHttpCollector.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

public class ZipkinActiveMQCollectorConfigurationTest {

Expand All @@ -36,7 +36,7 @@ public class ZipkinActiveMQCollectorConfigurationTest {
}

@Test void doesNotProvideCollectorComponent_whenAddressAndUriNotSet() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
context.register(
PropertyPlaceholderAutoConfiguration.class,
ZipkinActiveMQCollectorConfiguration.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import zipkin2.elasticsearch.ElasticsearchStorage;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static zipkin2.server.internal.elasticsearch.ITElasticsearchDynamicCredentials.pathOfResource;

public class ZipkinElasticsearchStorageConfigurationTest {
Expand All @@ -45,7 +45,7 @@ public class ZipkinElasticsearchStorageConfigurationTest {
}

@Test void doesntProvideStorageComponent_whenStorageTypeNotElasticsearch() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
TestPropertyValues.of("zipkin.storage.type:cassandra").applyTo(context);
Access.registerElasticsearch(context);
context.refresh();
Expand Down Expand Up @@ -256,11 +256,11 @@ static class CustomizerConfiguration {
}

@Test void dailyIndexFormat_overridingDateSeparator_invalidToBeMultiChar() {
assertThrows(BeanCreationException.class, () -> {
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> {
TestPropertyValues.of(
"zipkin.storage.type:elasticsearch",
"zipkin.storage.elasticsearch.hosts:http://host1:9200",
"zipkin.storage.elasticsearch.date-separator:blagho")
"zipkin.storage.type:elasticsearch",
"zipkin.storage.elasticsearch.hosts:http://host1:9200",
"zipkin.storage.elasticsearch.date-separator:blagho")
.applyTo(context);
Access.registerElasticsearch(context);

Expand Down Expand Up @@ -337,26 +337,26 @@ static class CustomizerConfiguration {
}

@Test void providesBasicAuthInterceptor_whenInvalidDynamicCredentialsConfigured() {
assertThrows(BeanCreationException.class, () -> {
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> {
String credentialsFile = pathOfResource("es-credentials-invalid");
TestPropertyValues.of(
"zipkin.storage.type:elasticsearch",
"zipkin.storage.elasticsearch.hosts:127.0.0.1:1234",
"zipkin.storage.elasticsearch.credentials-file:" + credentialsFile,
"zipkin.storage.elasticsearch.credentials-refresh-interval:2")
"zipkin.storage.type:elasticsearch",
"zipkin.storage.elasticsearch.hosts:127.0.0.1:1234",
"zipkin.storage.elasticsearch.credentials-file:" + credentialsFile,
"zipkin.storage.elasticsearch.credentials-refresh-interval:2")
.applyTo(context);
Access.registerElasticsearch(context);
context.refresh();
});
}

@Test void providesBasicAuthInterceptor_whenDynamicCredentialsConfiguredButFileAbsent() {
assertThrows(BeanCreationException.class, () -> {
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> {
TestPropertyValues.of(
"zipkin.storage.type:elasticsearch",
"zipkin.storage.elasticsearch.hosts:127.0.0.1:1234",
"zipkin.storage.elasticsearch.credentials-file:no-this-file",
"zipkin.storage.elasticsearch.credentials-refresh-interval:2")
"zipkin.storage.type:elasticsearch",
"zipkin.storage.elasticsearch.hosts:127.0.0.1:1234",
"zipkin.storage.elasticsearch.credentials-file:no-this-file",
"zipkin.storage.elasticsearch.credentials-refresh-interval:2")
.applyTo(context);
Access.registerElasticsearch(context);
context.refresh();
Expand Down Expand Up @@ -436,10 +436,10 @@ static class CustomizerConfiguration {
}

@Test void templatePriority_Invalid() {
assertThrows(UnsatisfiedDependencyException.class, () -> {
assertThatExceptionOfType(UnsatisfiedDependencyException.class).isThrownBy(() -> {
TestPropertyValues.of(
"zipkin.storage.type:elasticsearch",
"zipkin.storage.elasticsearch.template-priority:string")
"zipkin.storage.type:elasticsearch",
"zipkin.storage.elasticsearch.template-priority:string")
.applyTo(context);
Access.registerElasticsearch(context);
context.refresh();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import zipkin2.server.internal.InMemoryConfiguration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

public class ZipkinKafkaCollectorConfigurationTest {

Expand All @@ -34,7 +34,7 @@ public class ZipkinKafkaCollectorConfigurationTest {
}

@Test void doesNotProvideCollectorComponent_whenBootstrapServersUnset() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
context.register(
PropertyPlaceholderAutoConfiguration.class,
ZipkinKafkaCollectorConfiguration.class,
Expand All @@ -45,7 +45,7 @@ public class ZipkinKafkaCollectorConfigurationTest {
}

@Test void providesCollectorComponent_whenBootstrapServersEmptyString() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
TestPropertyValues.of("zipkin.collector.kafka.bootstrap-servers:").applyTo(context);
context.register(
PropertyPlaceholderAutoConfiguration.class,
Expand All @@ -69,7 +69,7 @@ public class ZipkinKafkaCollectorConfigurationTest {
}

@Test void doesNotProvidesCollectorComponent_whenBootstrapServersSetAndDisabled() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
TestPropertyValues.of("zipkin.collector.kafka.bootstrap-servers:localhost:9092")
.applyTo(context);
TestPropertyValues.of("zipkin.collector.kafka.enabled:false").applyTo(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.failBecauseExceptionWasNotThrown;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

public class ZipkinRabbitMQCollectorConfigurationTest {

Expand All @@ -36,7 +36,7 @@ public class ZipkinRabbitMQCollectorConfigurationTest {
}

@Test void doesNotProvideCollectorComponent_whenAddressAndUriNotSet() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
context.register(
PropertyPlaceholderAutoConfiguration.class,
ZipkinRabbitMQCollectorConfiguration.class,
Expand All @@ -47,11 +47,11 @@ public class ZipkinRabbitMQCollectorConfigurationTest {
}

@Test void doesNotProvideCollectorComponent_whenAddressesAndUriIsEmptyString() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
context = new AnnotationConfigApplicationContext();
TestPropertyValues.of(
"zipkin.collector.rabbitmq.addresses:",
"zipkin.collector.rabbitmq.uri:")
"zipkin.collector.rabbitmq.addresses:",
"zipkin.collector.rabbitmq.uri:")
.applyTo(context);
context.register(
PropertyPlaceholderAutoConfiguration.class,
Expand Down Expand Up @@ -80,7 +80,7 @@ public class ZipkinRabbitMQCollectorConfigurationTest {
}

@Test void doesNotProvidesCollectorComponent_whenAddressesSetAndDisabled() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("zipkin.collector.rabbitmq.addresses:localhost:1234").applyTo(context);
TestPropertyValues.of("zipkin.collector.rabbitmq.enabled:false").applyTo(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import zipkin2.storage.StorageComponent;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand All @@ -48,7 +48,7 @@ public class ThrottledStorageComponentTest {
}

@Test void createComponent_withNegativeQueue() {
assertThrows(IllegalArgumentException.class, () -> {
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
int queueSize = -1;
new ThrottledStorageComponent(delegate, registry, tracing, 1, 2, queueSize);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@
import org.springframework.core.io.ClassPathResource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

public class ZipkinUiConfigurationTest {
class ZipkinUiConfigurationTest {

AnnotationConfigApplicationContext context;

Expand All @@ -60,8 +59,7 @@ public class ZipkinUiConfigurationTest {
ZipkinUiConfiguration ui = new ZipkinUiConfiguration();
ui.ui = new ZipkinUiProperties();
ui.lensIndexHtml = new ClassPathResource("does-not-exist.html");
assertThatThrownBy(ui::indexService)
.isInstanceOf(BeanCreationException.class);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(ui::indexService);
}

@Test void canOverridesProperty_defaultLookback() {
Expand Down Expand Up @@ -112,7 +110,7 @@ public class ZipkinUiConfigurationTest {
}

@Test void canOverridesProperty_disable() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
context = createContextWithOverridenProperty("zipkin.ui.enabled:false");

context.getBean(ZipkinUiProperties.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import zipkin2.server.internal.mysql.Access;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

public class ZipkinMySQLStorageConfigurationTest {

Expand All @@ -35,7 +35,7 @@ public class ZipkinMySQLStorageConfigurationTest {
}

@Test void doesntProvidesStorageComponent_whenStorageTypeNotMySQL() {
assertThrows(NoSuchBeanDefinitionException.class, () -> {
assertThatExceptionOfType(NoSuchBeanDefinitionException.class).isThrownBy(() -> {
context = new AnnotationConfigApplicationContext();
TestPropertyValues.of("zipkin.storage.type:cassandra").applyTo(context);
Access.registerMySQL(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import zipkin2.elasticsearch.internal.JsonReaders;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static zipkin2.elasticsearch.internal.JsonReaders.collectValuesNamed;
import static zipkin2.elasticsearch.internal.JsonSerializers.JSON_FACTORY;

Expand Down Expand Up @@ -74,7 +74,7 @@ public class JsonReadersTest {

// All elasticsearch results start with an object, not an array.
@Test void collectValuesNamed_exceptionOnWrongData() throws IOException {
assertThrows(IllegalArgumentException.class, () -> {
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
assertThat(collectValuesNamed(JSON_FACTORY.createParser("[]"), "key")).isEmpty();
});
}
Expand Down
3 changes: 1 addition & 2 deletions zipkin-tests/src/test/java/zipkin2/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AnnotationTest {

Expand All @@ -26,7 +25,7 @@ public class AnnotationTest {

Annotation.create(1L, null);
});
assertTrue(exception.getMessage().contains("value"));
assertThat(exception.getMessage()).contains("value");
}

@Test void toString_isNice() {
Expand Down
4 changes: 2 additions & 2 deletions zipkin-tests/src/test/java/zipkin2/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -249,7 +249,7 @@ void concurrent_executesOrSubmitsOnce() throws InterruptedException {
}

@Test void onErrorReturn_execute_onError() throws Exception {
assertThrows(IllegalArgumentException.class, () -> {
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
IllegalArgumentException exception = new IllegalArgumentException();
Call<String> errorCall = errorCall(exception);

Expand Down
Loading

0 comments on commit 6d6ba01

Please sign in to comment.