You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "hjtiun852 (via GitHub)" <gi...@apache.org> on 2026/08/25 23:36:09 UTC
[PR] MINOR: Replace try/fail/catch with assertThrows in producer tests [kafka]
hjtiun852 opened a new pull request, #23277:
URL: https://github.com/apache/kafka/pull/23277
Modernize six exception assertions in ProducerRecordTest, RecordSendTest and MockProducerTest from the old try/call/fail/catch idiom to assertThrows.
assertThrows fails automatically when no exception is thrown, so a missing fail() can no longer let a test pass silently, and it pins the exact expected type. Where the original catch block asserted on the wrapped cause, the assertThrows return value preserves that check:
ExecutionException err = assertThrows(ExecutionException.class, md2::get);
assertEquals(e, err.getCause());
Follows the same cleanup as #19975.
Testing: ./gradlew :clients:test for the three test classes plus checkstyle, all green.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] MINOR: Replace try/fail/catch with assertThrows in producer tests [kafka]
Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #23277:
URL: https://github.com/apache/kafka/pull/23277#discussion_r3881924309
##########
clients/src/test/java/org/apache/kafka/clients/producer/ProducerRecordTest.java:
##########
@@ -53,26 +53,17 @@ public void testEqualsAndHashCode() {
@Test
public void testInvalidRecords() {
- try {
- new ProducerRecord<>(null, 0, "key", 1);
- fail("Expected IllegalArgumentException to be raised because topic is null");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>(null, 0, "key", 1),
+ "Expected IllegalArgumentException to be raised because topic is null");
- try {
- new ProducerRecord<>("test", 0, -1L, "key", 1);
- fail("Expected IllegalArgumentException to be raised because of negative timestamp");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>("test", 0, -1L, "key", 1),
+ "Expected IllegalArgumentException to be raised because of negative timestamp");
- try {
- new ProducerRecord<>("test", -1, "key", 1);
- fail("Expected IllegalArgumentException to be raised because of negative partition");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>("test", -1, "key", 1),
+ "Expected IllegalArgumentException to be raised because of negative partition");
Review Comment:
ditto
##########
clients/src/test/java/org/apache/kafka/clients/producer/ProducerRecordTest.java:
##########
@@ -53,26 +53,17 @@ public void testEqualsAndHashCode() {
@Test
public void testInvalidRecords() {
- try {
- new ProducerRecord<>(null, 0, "key", 1);
- fail("Expected IllegalArgumentException to be raised because topic is null");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>(null, 0, "key", 1),
+ "Expected IllegalArgumentException to be raised because topic is null");
- try {
- new ProducerRecord<>("test", 0, -1L, "key", 1);
- fail("Expected IllegalArgumentException to be raised because of negative timestamp");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>("test", 0, -1L, "key", 1),
+ "Expected IllegalArgumentException to be raised because of negative timestamp");
Review Comment:
ditto
##########
clients/src/test/java/org/apache/kafka/clients/producer/ProducerRecordTest.java:
##########
@@ -53,26 +53,17 @@ public void testEqualsAndHashCode() {
@Test
public void testInvalidRecords() {
- try {
- new ProducerRecord<>(null, 0, "key", 1);
- fail("Expected IllegalArgumentException to be raised because topic is null");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>(null, 0, "key", 1),
+ "Expected IllegalArgumentException to be raised because topic is null");
Review Comment:
This error message is redundant
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] MINOR: Replace try/fail/catch with assertThrows in producer tests [kafka]
Posted by "hjtiun852 (via GitHub)" <gi...@apache.org>.
hjtiun852 commented on PR #23277:
URL: https://github.com/apache/kafka/pull/23277#issuecomment-5463198457
Thanks for pointing these out!
I’ll take a look at these tests as well and check whether they require similar changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] MINOR: Replace try/fail/catch with assertThrows in producer tests [kafka]
Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 merged PR #23277:
URL: https://github.com/apache/kafka/pull/23277
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] MINOR: Replace try/fail/catch with assertThrows in producer tests [kafka]
Posted by "hjtiun852 (via GitHub)" <gi...@apache.org>.
hjtiun852 commented on code in PR #23277:
URL: https://github.com/apache/kafka/pull/23277#discussion_r3894971894
##########
clients/src/test/java/org/apache/kafka/clients/producer/ProducerRecordTest.java:
##########
@@ -53,26 +53,17 @@ public void testEqualsAndHashCode() {
@Test
public void testInvalidRecords() {
- try {
- new ProducerRecord<>(null, 0, "key", 1);
- fail("Expected IllegalArgumentException to be raised because topic is null");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>(null, 0, "key", 1),
+ "Expected IllegalArgumentException to be raised because topic is null");
Review Comment:
Thanks so much for the review! Removed.
##########
clients/src/test/java/org/apache/kafka/clients/producer/ProducerRecordTest.java:
##########
@@ -53,26 +53,17 @@ public void testEqualsAndHashCode() {
@Test
public void testInvalidRecords() {
- try {
- new ProducerRecord<>(null, 0, "key", 1);
- fail("Expected IllegalArgumentException to be raised because topic is null");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>(null, 0, "key", 1),
+ "Expected IllegalArgumentException to be raised because topic is null");
- try {
- new ProducerRecord<>("test", 0, -1L, "key", 1);
- fail("Expected IllegalArgumentException to be raised because of negative timestamp");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>("test", 0, -1L, "key", 1),
+ "Expected IllegalArgumentException to be raised because of negative timestamp");
Review Comment:
ditto
##########
clients/src/test/java/org/apache/kafka/clients/producer/ProducerRecordTest.java:
##########
@@ -53,26 +53,17 @@ public void testEqualsAndHashCode() {
@Test
public void testInvalidRecords() {
- try {
- new ProducerRecord<>(null, 0, "key", 1);
- fail("Expected IllegalArgumentException to be raised because topic is null");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>(null, 0, "key", 1),
+ "Expected IllegalArgumentException to be raised because topic is null");
- try {
- new ProducerRecord<>("test", 0, -1L, "key", 1);
- fail("Expected IllegalArgumentException to be raised because of negative timestamp");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>("test", 0, -1L, "key", 1),
+ "Expected IllegalArgumentException to be raised because of negative timestamp");
- try {
- new ProducerRecord<>("test", -1, "key", 1);
- fail("Expected IllegalArgumentException to be raised because of negative partition");
- } catch (IllegalArgumentException e) {
- //expected
- }
+ assertThrows(IllegalArgumentException.class,
+ () -> new ProducerRecord<>("test", -1, "key", 1),
+ "Expected IllegalArgumentException to be raised because of negative partition");
Review Comment:
ditto
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org