Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,19 @@ The following example configuration creates topics called `cat` and `hat` with f

[source, java]
----
@ExtendWith(SpringExtension.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why @SpringJUnitConfig doesn't work for us here?

@EmbeddedKafka(
partitions = 5,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear from this view, but can we be sure that we don't use tabs for indents?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I had used tabs for indents.
Now, I fixed it with 8 spaces fir indents.
I attached rendered image! Please refer it!

image

topics = {"cat", "hat"}
)
public class MyTests {

@ClassRule
private static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, false, 5, "cat", "hat");
@Autowired
private EmbeddedKafkaBroker broker;

@Test
public void test() {
embeddedKafkaRule.getEmbeddedKafka()
.addTopics(new NewTopic("thing1", 10, (short) 1), new NewTopic("thing2", 15, (short) 1));
broker.addTopics(new NewTopic("thing1", 10, (short) 1), new NewTopic("thing2", 15, (short) 1));
...
}

Expand Down Expand Up @@ -225,7 +229,7 @@ The following example shows how to use it:

[source, java]
----
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@DirtiesContext
@EmbeddedKafka(partitions = 1,
topics = {
Expand All @@ -237,7 +241,7 @@ public class KafkaStreamsTests {
private EmbeddedKafkaBroker embeddedKafka;

@Test
public void someTest() {
void someTest() {
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("testGroup", "true", this.embeddedKafka);
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
ConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps);
Expand Down Expand Up @@ -333,7 +337,7 @@ The following example shows how to do so:
=====
[source, java]
----
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(properties = "spring.autoconfigure.exclude="
+ "org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration")
public class MyApplicationTests {
Expand All @@ -350,6 +354,38 @@ They include:
* xref:testing.adoc#kafka-testing-junit4-class-rule[JUnit4 Class Rule]
* xref:testing.adoc#kafka-testing-embeddedkafka-annotation[`@EmbeddedKafka` Annotation or `EmbeddedKafkaBroker` Bean]

[[kafka-testing-junit4-embedded-broker]]
=== Junit4 Embedded Broker
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we deprecate JUnit 4, we just should mention that we have done that and recommend to migrate to JUnit 5 without any further advises how to configure JUnit 4.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed all contents about JUnit4 and left comment for asking migration to JUnit5.
Thanks a lot 🙇‍♂️


The following example shows how to create an embedded broker in Junit4:
[source, java]
----
@SpringBootTest
public class MyApplicationTests {

@Autowired
private final EmbeddedKafkaBroker broker;

@Autowired
private KafkaTemplate<String, String> template;

@Test
public void test() {
...
}

@Configuration
public static class MyConfiguration {
@Bean
public EmbeddedKafkaBroker embeddedKafkaBroker() {
return new EmbeddedKafkaKraftBroker(1, 1, "someTopic");
}

}

}
----

[[kafka-testing-junit4-class-rule]]
=== JUnit4 Class Rule
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to have all the JUnit 4 text removed in favor of a single sentence that its support is deprecated for removal in the next minor version.


Expand Down Expand Up @@ -378,6 +414,10 @@ public class MyApplicationTests {

Notice that, since this is a Spring Boot application, we override the broker list property to set Spring Boot's property.

NOTE: The `EmbeddedKafkaRule` JUnit 4 rule has been removed in version 4.0.
For JUnit 4, you should use the `EmbeddedKafkaKraftBroker` directly or migrate to JUnit 5 with the `@EmbeddedKafka` annotation.
Please refer to xref:kafka-testing-junit4-embedded-broker[Junit4 Embedded Broker]

[[embedded-broker-with-springjunitconfig-annotations]]
== `@EmbeddedKafka` with `@SpringJunitConfig`

Expand All @@ -395,7 +435,7 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create

[source, java]
----
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@EmbeddedKafka(topics = "someTopic",
bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default
public class MyApplicationTests {
Expand All @@ -404,7 +444,7 @@ public class MyApplicationTests {
private KafkaTemplate<String, String> template;

@Test
public void test() {
void test() {
...
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* <p>
* The typical usage of this annotation is like:
* <pre class="code">
* &#064;RunWith(SpringRunner.class)
* &#064;ExtendWith(SpringExtension.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DITTO about @SpringJUnitConfig.
It is very rare situation when we need to use @ExtendWith(SpringExtension.class).

* &#064;EmbeddedKafka
* public class MyKafkaTests {
*
Expand All @@ -67,6 +67,7 @@
* @author Pawel Lozinski
* @author Adrian Chlebosz
* @author Soby Chacko
* @author Sanghyeok An
*
* @since 1.3
*
Expand Down Expand Up @@ -169,4 +170,3 @@
int adminTimeout() default EmbeddedKafkaBroker.DEFAULT_ADMIN_TIMEOUT;

}

Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@
* @author Dave Syer
* @author Artem Bilan
* @author Gary Russell
*
* @author Sanghyeok An
* @deprecated since Spring for Apache Kafka 4.0 in favor of the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not good style since the @author section becomes not visible and we lose a credit for those who contributed to the class.

You use too many words for the @deprecated tag. It is really obvious if we use version without project mentioning. If that is not a case, we would mention the target library name.
So, for me this sentence could be like this:

 @deprecated since 4.0 in favor of {@link org.springframework.kafka.test.condition.LogLevels}.

That is also obvious by LogLevels design that it is for JUnit 5.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@artembilan

This is not good style since the @author section becomes not visible and we lose a credit for those who contributed to the class.

Sorry to say that I don't understand your point. 😓
other spring projects do same things. for examples,

I built the java docs in my local and I can't see @author in all java docs.
However, spring-kafka java docs don't display @author tags at all as well.(https://javadoc.io/doc/org.springframework.kafka/spring-kafka/latest/org/springframework/kafka/listener/CommonErrorHandler.html)

Could you let me know about not visiable @author tag...? 😢
Thanks for your time... 🙇‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Different projects may have different styles or just don't care about readability.
I try to be with my code as friendly as possible.
That style:

image

makes it hard for me to read the code, so I imaging that not only me, therefore in my code I try to separate those sections for readers of my code.

If you use @ here in GH comments, please, wrap that into code snippet.
Otherwise GH treats it as mentioning of that user here on GH.
Just try to follow that @author link. Since there that GH user you have just called him into our discussion 🤷

I'm not sure what Javadoc link you show there, but the one we provide on our site has @author list: https://docs.spring.io/spring-kafka/docs/3.3.5/api/org/springframework/kafka/listener/AbstractMessageListenerContainer.html.

Either way, my point is not about Javadoc tool, but rather source code to be readable clearly.
In the end that's why it is an Open Source!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comments!!
I totally misunderstood your points. 😅
I thought you were letting me know that the author would not be visible in the JavaDoc.

* {@link org.springframework.kafka.test.condition.LogLevels} and JUnit Jupiter.
*/
@Deprecated(since = "4.0")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed forRemoval = true.
We really are going to remove it in the next 4.1 or 4.2

public class Log4j2LevelAdjuster implements MethodRule {

private final List<Class<?>> classes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.springframework.kafka.test.rule;
package org.springframework.kafka.test;

import java.io.IOException;
import java.net.ServerSocket;
Expand All @@ -32,7 +32,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.test.EmbeddedKafkaKraftBroker;
import org.springframework.kafka.test.utils.KafkaTestUtils;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
Expand Down