Skip to content

Commit 4d6a9c2

Browse files
authored
Merge pull request #96 from aiven/anatolii/assertj-migration
Migrating tests to AssertJ
2 parents a227738 + c56e821 commit 4d6a9c2

File tree

16 files changed

+306
-328
lines changed

16 files changed

+306
-328
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,10 @@ dependencies {
9797
implementation "org.slf4j:slf4j-api:1.7.36"
9898

9999
testImplementation "org.junit.jupiter:junit-jupiter:5.9.3"
100-
testImplementation "org.hamcrest:hamcrest:2.2"
101100
testImplementation "org.apache.kafka:connect-api:$kafkaVersion"
102101
testImplementation "org.testcontainers:junit-jupiter:$testcontainersVersion"
103102
testImplementation "io.debezium:debezium-api:$debeziumVersion"
103+
testImplementation "org.assertj:assertj-core:3.24.2"
104104

105105
testRuntimeOnly "org.apache.logging.log4j:log4j-slf4j-impl:2.20.0"
106106
testRuntimeOnly "org.apache.logging.log4j:log4j-api:2.20.0"

src/integration-test/java/io/aiven/kafka/connect/transforms/IntegrationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import org.testcontainers.junit.jupiter.Testcontainers;
4646
import org.testcontainers.utility.DockerImageName;
4747

48-
import static org.junit.jupiter.api.Assertions.assertEquals;
48+
import static org.assertj.core.api.Assertions.assertThat;
4949

5050
@Testcontainers
5151
final class IntegrationTest {
@@ -204,9 +204,9 @@ final void checkMessageTopics(final TopicPartition originalTopicPartition, final
204204
final Map<TopicPartition, Long> endOffsets = consumer.endOffsets(
205205
Arrays.asList(originalTopicPartition, newTopicPartition));
206206
// The original topic should be empty.
207-
assertEquals(0, endOffsets.get(originalTopicPartition));
207+
assertThat(endOffsets.get(originalTopicPartition)).isZero();
208208
// The new topic should be non-empty.
209-
assertEquals(TestSourceConnector.MESSAGES_TO_PRODUCE, endOffsets.get(newTopicPartition));
209+
assertThat(endOffsets).containsEntry(newTopicPartition, TestSourceConnector.MESSAGES_TO_PRODUCE);
210210
}
211211

212212
private void waitForCondition(final Supplier<Boolean> conditionChecker,

src/integration-test/java/io/aiven/kafka/connect/transforms/TestSourceConnector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* <p>It just produces a fixed number of struct records.
3737
*/
3838
public class TestSourceConnector extends SourceConnector {
39-
static final int MESSAGES_TO_PRODUCE = 10;
39+
static final long MESSAGES_TO_PRODUCE = 10L;
4040

4141
static final String ORIGINAL_TOPIC = "original-topic";
4242
static final String NEW_TOPIC = "new-topic";

src/test/java/io/aiven/kafka/connect/debezium/converters/MoneyConverterTest.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@
3030
import org.junit.jupiter.api.BeforeEach;
3131
import org.junit.jupiter.api.Test;
3232

33-
import static org.junit.jupiter.api.Assertions.assertEquals;
34-
import static org.junit.jupiter.api.Assertions.assertNull;
35-
import static org.junit.jupiter.api.Assertions.assertThrows;
33+
import static org.assertj.core.api.Assertions.assertThat;
34+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3635

3736
public class MoneyConverterTest {
3837

@@ -59,11 +58,11 @@ void teardown() {
5958
@Test
6059
void shouldRegisterCorrectSchema() {
6160
transform.configure(prop);
62-
assertNull(registration.currFieldSchema);
61+
assertThat(registration.currFieldSchema).isNull();
6362
transform.converterFor(new MoneyTestRelationalColumn(), registration);
6463

65-
assertEquals(registration.currFieldSchema.schema().name(), "price");
66-
assertEquals(registration.currFieldSchema.schema().type(), Schema.Type.STRING);
64+
assertThat(registration.currFieldSchema.schema().name()).isEqualTo("price");
65+
assertThat(registration.currFieldSchema.schema().type()).isEqualTo(Schema.Type.STRING);
6766
}
6867

6968
@Test
@@ -72,40 +71,40 @@ void shouldDoNothingIfColumnIsNotMoney() {
7271

7372
transform.converterFor(new DummyRelationalColumn(), registration);
7473

75-
assertNull(registration.currFieldSchema);
76-
assertNull(registration.currConverter);
74+
assertThat(registration.currFieldSchema).isNull();
75+
assertThat(registration.currConverter).isNull();
7776
}
7877

7978
@Test
8079
void shouldFormatDataToMoneyFormat() {
81-
assertNull(registration.currConverter);
80+
assertThat(registration.currConverter).isNull();
8281
transform.converterFor(new MoneyTestRelationalColumn(), registration);
8382

8483
final String result = (String) registration.currConverter.convert(BigDecimal.valueOf(103.6999));
85-
assertEquals(result, "103.70");
84+
assertThat(result).isEqualTo("103.70");
8685

8786
final String result2 = (String) registration.currConverter.convert((long) 103);
88-
assertEquals(result2, "103.00");
87+
assertThat(result2).isEqualTo("103.00");
8988
}
9089

9190
@Test
9291
void shouldFailIfDataIsNotBigDecimal() {
93-
assertNull(registration.currConverter);
92+
assertThat(registration.currConverter).isNull();
9493
transform.converterFor(new MoneyTestRelationalColumn(), registration);
9594

96-
final Throwable e = assertThrows(IllegalArgumentException.class,
97-
() -> registration.currConverter.convert("103.6999"));
98-
assertEquals(e.getMessage(), "Money type should have BigDecimal type");
95+
assertThatThrownBy(() -> registration.currConverter.convert("103.6999"))
96+
.isInstanceOf(IllegalArgumentException.class)
97+
.hasMessage("Money type should have BigDecimal type");
9998
}
10099

101100
@Test
102101
void shouldFailIfDataIsMissing() {
103-
assertNull(registration.currConverter);
102+
assertThat(registration.currConverter).isNull();
104103
transform.converterFor(new MoneyTestRelationalColumn(), registration);
105104

106-
final Throwable e = assertThrows(IllegalArgumentException.class,
107-
() -> registration.currConverter.convert(null));
108-
assertEquals(e.getMessage(), "Money column is not optional, but data is null");
105+
assertThatThrownBy(() -> registration.currConverter.convert(null))
106+
.isInstanceOf(IllegalArgumentException.class)
107+
.hasMessage("Money column is not optional, but data is null");
109108
}
110109

111110
@Test
@@ -117,7 +116,7 @@ void shouldDoNothingIfColumnIsOptional() {
117116
transform.converterFor(moneyColumn, registration);
118117

119118
final String result = (String) registration.currConverter.convert(null);
120-
assertNull(result);
119+
assertThat(result).isNull();
121120
}
122121

123122
class StubConverterRegistration implements CustomConverter.ConverterRegistration<SchemaBuilder> {

src/test/java/io/aiven/kafka/connect/transforms/ConcatFieldsConfigTest.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,25 @@
2828
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.FIELD_NAMES_CONFIG;
2929
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.FIELD_REPLACE_MISSING_CONFIG;
3030
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.OUTPUT_FIELD_NAME_CONFIG;
31-
import static org.junit.jupiter.api.Assertions.assertEquals;
32-
import static org.junit.jupiter.api.Assertions.assertThrows;
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3333

3434
class ConcatFieldsConfigTest {
3535
@Test
3636
void emptyConfig() {
3737
final Map<String, String> props = new HashMap<>();
38-
final Throwable e = assertThrows(ConfigException.class, () -> new ConcatFieldsConfig(props));
39-
assertEquals("Missing required configuration \"field.names\" which has no default value.",
40-
e.getMessage());
38+
assertThatThrownBy(() -> new ConcatFieldsConfig(props))
39+
.isInstanceOf(ConfigException.class)
40+
.hasMessage("Missing required configuration \"field.names\" which has no default value.");
4141
}
4242

4343
@Test
4444
void emptyFieldName() {
4545
final Map<String, String> props = new HashMap<>();
4646
props.put(FIELD_NAMES_CONFIG, "");
47-
final Throwable e = assertThrows(ConfigException.class, () -> new ConcatFieldsConfig(props));
48-
assertEquals("Missing required configuration \"output.field.name\" which has no default value.",
49-
e.getMessage());
47+
assertThatThrownBy(() -> new ConcatFieldsConfig(props))
48+
.isInstanceOf(ConfigException.class)
49+
.hasMessage("Missing required configuration \"output.field.name\" which has no default value.");
5050
}
5151

5252
@Test
@@ -57,9 +57,9 @@ void definedFieldName() {
5757
props.put(DELIMITER_CONFIG, "-");
5858
props.put(FIELD_REPLACE_MISSING_CONFIG, "*");
5959
final ConcatFieldsConfig config = new ConcatFieldsConfig(props);
60-
assertEquals(Arrays.asList("test", "foo", "bar"), config.fieldNames());
61-
assertEquals("combined", config.outputFieldName());
62-
assertEquals("-", config.delimiter());
63-
assertEquals("*", config.fieldReplaceMissing());
60+
assertThat(config.fieldNames()).isEqualTo(Arrays.asList("test", "foo", "bar"));
61+
assertThat(config.outputFieldName()).isEqualTo("combined");
62+
assertThat(config.delimiter()).isEqualTo("-");
63+
assertThat(config.fieldReplaceMissing()).isEqualTo("*");
6464
}
6565
}

src/test/java/io/aiven/kafka/connect/transforms/ConcatFieldsTest.java

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@
3333
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.FIELD_NAMES_CONFIG;
3434
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.FIELD_REPLACE_MISSING_CONFIG;
3535
import static io.aiven.kafka.connect.transforms.ConcatFieldsConfig.OUTPUT_FIELD_NAME_CONFIG;
36-
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
37-
import static org.junit.jupiter.api.Assertions.assertEquals;
38-
import static org.junit.jupiter.api.Assertions.assertThrows;
36+
import static org.assertj.core.api.Assertions.assertThatNoException;
37+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
38+
import static org.junit.Assert.assertEquals;
3939

4040
abstract class ConcatFieldsTest {
4141
private static final String FIELD = "combined";
@@ -65,30 +65,27 @@ abstract class ConcatFieldsTest {
6565

6666
@Test
6767
void recordNotStructOrMap() {
68-
final SinkRecord originalRecord = record(SchemaBuilder.INT8_SCHEMA, (byte) 123);
69-
final Throwable e = assertThrows(DataException.class,
70-
() -> transformation().apply(originalRecord));
71-
assertEquals("Value type must be STRUCT or MAP: " + originalRecord,
72-
e.getMessage());
68+
final SinkRecord originalRecord = record(Schema.INT8_SCHEMA, (byte) 123);
69+
assertThatThrownBy(() -> transformation().apply(originalRecord))
70+
.isInstanceOf(DataException.class)
71+
.hasMessage("Value type must be STRUCT or MAP: " + originalRecord);
7372
}
7473

7574
@Test
7675
void recordStructNull() {
7776
final Schema schema = SchemaBuilder.struct().schema();
7877
final SinkRecord originalRecord = record(schema, null);
79-
final Throwable e = assertThrows(DataException.class,
80-
() -> transformation().apply(originalRecord));
81-
assertEquals(dataPlace() + " Value can't be null: " + originalRecord,
82-
e.getMessage());
78+
assertThatThrownBy(() -> transformation().apply(originalRecord))
79+
.isInstanceOf(DataException.class)
80+
.hasMessage(dataPlace() + " Value can't be null: " + originalRecord);
8381
}
8482

8583
@Test
8684
void recordMapNull() {
8785
final SinkRecord originalRecord = record(null, null);
88-
final Throwable e = assertThrows(DataException.class,
89-
() -> transformation().apply(originalRecord));
90-
assertEquals(dataPlace() + " Value can't be null: " + originalRecord,
91-
e.getMessage());
86+
assertThatThrownBy(() -> transformation().apply(originalRecord))
87+
.isInstanceOf(DataException.class)
88+
.hasMessage(dataPlace() + " Value can't be null: " + originalRecord);
9289
}
9390

9491
@Test
@@ -100,9 +97,9 @@ void structWithSchemaMissingField() {
10097
.field(AGE_FIELD, Schema.OPTIONAL_INT64_SCHEMA)
10198
.build();
10299
final SinkRecord originalRecord = record(schema, new Struct(schema));
103-
final Throwable e = assertThrows(DataException.class,
104-
() -> transformation().apply(originalRecord));
105-
assertEquals("Invalid value: null used for required field: \"bar\", schema type: STRING", e.getMessage());
100+
assertThatThrownBy(() -> transformation().apply(originalRecord))
101+
.isInstanceOf(DataException.class)
102+
.hasMessage("Invalid value: null used for required field: \"bar\", schema type: STRING");
106103
}
107104

108105
@Test
@@ -114,26 +111,27 @@ void structWithMissingField() {
114111
.field(AGE_FIELD, Schema.OPTIONAL_INT64_SCHEMA)
115112
.build();
116113
final SinkRecord originalRecord = record(null, new Struct(schema));
117-
final Throwable e = assertThrows(DataException.class,
118-
() -> transformation().apply(originalRecord));
119-
assertEquals("Invalid value: null used for required field: \"bar\", schema type: STRING", e.getMessage());
114+
assertThatThrownBy(() -> transformation().apply(originalRecord))
115+
.isInstanceOf(DataException.class)
116+
.hasMessage("Invalid value: null used for required field: \"bar\", schema type: STRING");
120117
}
121118

122119
@Test
123120
void mapWithMissingField() {
124121
final SinkRecord originalRecord = record(null, new HashMap<>());
125-
assertDoesNotThrow(() -> transformation().apply(originalRecord),
126-
FIELD + " field must be present and its value can't be null: " + originalRecord);
122+
assertThatNoException()
123+
.describedAs(FIELD + " field must be present and its value can't be null: " + originalRecord)
124+
.isThrownBy(() -> transformation().apply(originalRecord));
127125
}
128126

129127
@Test
130128
void mapWithoutSchema() {
131-
final HashMap<Object, Object> valueMap = new HashMap<>();
129+
final Map<Object, Object> valueMap = new HashMap<>();
132130
valueMap.put(BAR_FIELD, BAR_VALUE);
133131
valueMap.put(TEST_FIELD, TEST_VALUE);
134132
valueMap.put(AGE_FIELD, AGE_VALUE);
135133
valueMap.put(FOO_FIELD, FOO_VALUE);
136-
final HashMap<Object, Object> newValueMap = new HashMap<>();
134+
final Map<Object, Object> newValueMap = new HashMap<>();
137135
newValueMap.put(BAR_FIELD, BAR_VALUE);
138136
newValueMap.put(FIELD, TEST_VALUE + "-" + FOO_VALUE + "-" + BAR_VALUE + "-" + AGE_VALUE);
139137
newValueMap.put(TEST_FIELD, TEST_VALUE);

src/test/java/io/aiven/kafka/connect/transforms/ExtractTimestampConfigTest.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,41 @@
2323

2424
import org.junit.jupiter.api.Test;
2525

26-
import static org.junit.jupiter.api.Assertions.assertEquals;
27-
import static org.junit.jupiter.api.Assertions.assertThrows;
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2828

2929
class ExtractTimestampConfigTest {
3030
@Test
3131
void emptyConfig() {
3232
final Map<String, String> props = new HashMap<>();
33-
final Throwable e = assertThrows(ConfigException.class, () -> new ExtractTimestampConfig(props));
34-
assertEquals("Missing required configuration \"field.name\" which has no default value.",
35-
e.getMessage());
33+
assertThatThrownBy(() -> new ExtractTimestampConfig(props))
34+
.isInstanceOf(ConfigException.class)
35+
.hasMessage("Missing required configuration \"field.name\" which has no default value.");
3636
}
3737

3838
@Test
3939
void emptyFieldName() {
4040
final Map<String, String> props = new HashMap<>();
4141
props.put("field.name", "");
42-
final Throwable e = assertThrows(ConfigException.class, () -> new ExtractTimestampConfig(props));
43-
assertEquals("Invalid value for configuration field.name: String must be non-empty",
44-
e.getMessage());
42+
assertThatThrownBy(() -> new ExtractTimestampConfig(props))
43+
.isInstanceOf(ConfigException.class)
44+
.hasMessage("Invalid value for configuration field.name: String must be non-empty");
4545
}
4646

4747
@Test
4848
void definedFieldName() {
4949
final Map<String, String> props = new HashMap<>();
5050
props.put("field.name", "test");
5151
final ExtractTimestampConfig config = new ExtractTimestampConfig(props);
52-
assertEquals("test", config.fieldName());
52+
assertThat(config.fieldName()).isEqualTo("test");
5353
}
5454

5555
@Test
5656
void emptyTimestampResolution() {
5757
final var props = new HashMap<>();
5858
props.put("field.name", "test");
5959
final var config = new ExtractTimestampConfig(props);
60-
assertEquals(ExtractTimestampConfig.TimestampResolution.MILLISECONDS, config.timestampResolution());
60+
assertThat(config.timestampResolution()).isEqualTo(ExtractTimestampConfig.TimestampResolution.MILLISECONDS);
6161
}
6262

6363
@Test
@@ -69,7 +69,7 @@ void definedTimestampResolutionInSeconds() {
6969
ExtractTimestampConfig.TimestampResolution.SECONDS.resolution
7070
);
7171
final var config = new ExtractTimestampConfig(props);
72-
assertEquals(ExtractTimestampConfig.TimestampResolution.SECONDS, config.timestampResolution());
72+
assertThat(config.timestampResolution()).isEqualTo(ExtractTimestampConfig.TimestampResolution.SECONDS);
7373
}
7474

7575
@Test
@@ -81,7 +81,7 @@ void definedTimestampResolutionInMillis() {
8181
ExtractTimestampConfig.TimestampResolution.MILLISECONDS.resolution
8282
);
8383
final var config = new ExtractTimestampConfig(props);
84-
assertEquals(ExtractTimestampConfig.TimestampResolution.MILLISECONDS, config.timestampResolution());
84+
assertThat(config.timestampResolution()).isEqualTo(ExtractTimestampConfig.TimestampResolution.MILLISECONDS);
8585
}
8686

8787
@Test
@@ -92,11 +92,10 @@ void wrongTimestampResolution() {
9292
ExtractTimestampConfig.EPOCH_RESOLUTION_CONFIG,
9393
"foo"
9494
);
95-
final var e = assertThrows(ConfigException.class, () -> new ExtractTimestampConfig(props));
96-
assertEquals(
97-
"Invalid value foo for configuration timestamp.resolution: "
98-
+ "Unsupported resolution type 'foo'. Supported are: milliseconds, seconds",
99-
e.getMessage());
95+
assertThatThrownBy(() -> new ExtractTimestampConfig(props))
96+
.isInstanceOf(ConfigException.class)
97+
.hasMessage("Invalid value foo for configuration timestamp.resolution: "
98+
+ "Unsupported resolution type 'foo'. Supported are: milliseconds, seconds");
10099
}
101100

102101
}

0 commit comments

Comments
 (0)