Code review comment: I think you can already use something along the lines of  ```java assertThatThrownBy(() -> createTableSink(pkSchema, getKeyValueOptions())     .isInstanceOf(...)     .hasMessageContaining() ```

Code before review:
public void testPrimaryKeyValidation() {
final ResolvedSchema pkSchema =
new ResolvedSchema(
SCHEMA.getColumns(),
SCHEMA.getWatermarkSpecs(),
UniqueConstraint.primaryKey(NAME, Collections.singletonList(NAME)));
Map<String, String> sinkOptions =
getModifiedOptions(
getBasicSinkOptions(),
options ->
options.put(
String.format(
"%s.%s",
TestFormatFactory.IDENTIFIER,
TestFormatFactory.CHANGELOG_MODE.key()),
"I;UA;UB;D"));
createTableSink(pkSchema, sinkOptions);
try {
createTableSink(pkSchema, getBasicSinkOptions());
fail();
} catch (Throwable t) {
String error =
"The Kafka table 'default.default.t1' with 'test-format' format"
+ " doesn't support defining PRIMARY KEY constraint on the table, because it can't"
+ " guarantee the semantic of primary key.";
assertEquals(error, t.getCause().getMessage());
}
try {
createTableSink(pkSchema, getKeyValueOptions());
fail();
} catch (Throwable t) {
String error =
"The Kafka table 'default.default.t1' with 'test-format' format"
+ " doesn't support defining PRIMARY KEY constraint on the table, because it can't"
+ " guarantee the semantic of primary key.";
assertEquals(error, t.getCause().getMessage());
}
Map<String, String> sourceOptions =
getModifiedOptions(
getBasicSourceOptions(),
options ->
options.put(
String.format(
"%s.%s",
TestFormatFactory.IDENTIFIER,
TestFormatFactory.CHANGELOG_MODE.key()),
"I;UA;UB;D"));
createTableSource(pkSchema, sourceOptions);
try {
createTableSource(pkSchema, getBasicSourceOptions());
fail();
} catch (Throwable t) {
String error =
"The Kafka table 'default.default.t1' with 'test-format' format"
+ " doesn't support defining PRIMARY KEY constraint on the table, because it can't"
+ " guarantee the semantic of primary key.";
assertEquals(error, t.getCause().getMessage());
}
}

Line under review: createTableSink(pkSchema, getKeyValueOptions());

Code after addressing the review:
public void testPrimaryKeyValidation() {
final ResolvedSchema pkSchema =
new ResolvedSchema(
SCHEMA.getColumns(),
SCHEMA.getWatermarkSpecs(),
UniqueConstraint.primaryKey(NAME, Collections.singletonList(NAME)));
Map<String, String> sinkOptions =
getModifiedOptions(
getBasicSinkOptions(),
options ->
options.put(
String.format(
"%s.%s",
TestFormatFactory.IDENTIFIER,
TestFormatFactory.CHANGELOG_MODE.key()),
"I;UA;UB;D"));
createTableSink(pkSchema, sinkOptions);
assertThatExceptionOfType(ValidationException.class)
.isThrownBy(() -> createTableSink(pkSchema, getBasicSinkOptions()))
.havingRootCause()
.withMessage(
"The Kafka table 'default.default.t1' with 'test-format' format"
+ " doesn't support defining PRIMARY KEY constraint on the table, because it can't"
+ " guarantee the semantic of primary key.");
assertThatExceptionOfType(ValidationException.class)
.isThrownBy(() -> createTableSink(pkSchema, getKeyValueOptions()))
.havingRootCause()
.withMessage(
"The Kafka table 'default.default.t1' with 'test-format' format"
+ " doesn't support defining PRIMARY KEY constraint on the table, because it can't"
+ " guarantee the semantic of primary key.");
Map<String, String> sourceOptions =
getModifiedOptions(
getBasicSourceOptions(),
options ->
options.put(
String.format(
"%s.%s",
TestFormatFactory.IDENTIFIER,
TestFormatFactory.CHANGELOG_MODE.key()),
"I;UA;UB;D"));
createTableSource(pkSchema, sourceOptions);
assertThatExceptionOfType(ValidationException.class)
.isThrownBy(() -> createTableSource(pkSchema, getBasicSourceOptions()))
.havingRootCause()
.withMessage(
"The Kafka table 'default.default.t1' with 'test-format' format"
+ " doesn't support defining PRIMARY KEY constraint on the table, because it can't"
+ " guarantee the semantic of primary key.");
}