Testing Applications

The pulsar-spring-test dependency includes some useful utilities when testing your applications.

1. PulsarConsumerTestUtil

The org.apache.pulsar.spring.test.support.PulsarConsumerTestUtil provides a type-safe fluent API for consuming messages from a Pulsar topic within a test.

The following example shows how to consume messages from a topic for 5 seconds:

List<Message<String>> messages = PulsarConsumerTestUtil.consumeMessages(consumerFactory)
            .fromTopic("my-topic")
            .withSchema(Schema.STRING)
            .awaitAtMost(Duration.ofSeconds(5))
            .get();

An until method is also available to allow you to specify a condition that must be met before the messages are returned. The following example uses a condition to consume 5 messages from a topic.

List<Message<String>> messages = PulsarConsumerTestUtil.consumeMessages(consumerFactory)
            .fromTopic("my-topic")
            .withSchema(Schema.STRING)
            .awaitAtMost(Duration.ofSeconds(5))
            .until(messages -> messages.size() == 5)
            .get();

A set of commonly used conditions are available in org.apache.pulsar.spring.test.support.ConsumedMessagesConditions. The following example uses the factory-provided atLeastOneMessageMatches condition to return the consumed messages once one of them has a value of "boom".

List<Message<String>> messages = PulsarConsumerTestUtil.consumeMessages(consumerFactory)
            .fromTopic("my-topic")
            .withSchema(Schema.STRING)
            .awaitAtMost(Duration.ofSeconds(5))
            .until(ConsumedMessagesConditions.atLeastOneMessageMatches("boom"))
            .get();

2. PulsarTestContainerSupport

The org.apache.pulsar.spring.test.support.PulsarTestContainerSupport interface provides a static Pulsar Testcontainer. When using Junit Jupiter, the container is automatically started once per test class via @BeforeAll annotation.

The following example shows how you can use the container support in a @SpringBootTest in conjunction with the previously mentioned PulsarConsumerTestUtil.

@SpringBootTest
class MyApplicationTests implements PulsarTestContainerSupport {

	@DynamicPropertySource
	static void pulsarProperties(DynamicPropertyRegistry registry) {
		registry.add("spring.pulsar.client.service-url", PULSAR_CONTAINER::getPulsarBrokerUrl);
		registry.add("spring.pulsar.admin.service-url", PULSAR_CONTAINER::getHttpServiceUrl);
	}

	@Test
	void sendAndReceiveWorksAsExpected(
			@Autowired PulsarTemplate<String> template,
			@Autowired PulsarConsumerFactory<String> consumerFactory) {
		var topic = "some-topic";
		var msg = "foo-5150";
		template.send(topic, msg);
		var matchedUsers = PulsarConsumerTestUtil.consumeMessages(consumerFactory)
				.fromTopic(topic)
				.withSchema(Schema.STRING)
				.awaitAtMost(Duration.ofSeconds(2))
				.until(ConsumedMessagesConditions.atLeastOneMessageMatches(msg))
				.get();
		assertThat(matchedUsers).hasSize(1);
	}
}

3. PulsarTestImage

The org.apache.pulsar.spring.test.support.PulsarTestImage class creates Pulsar Testcontainers, for tests that need their own container rather than the shared one in PulsarTestContainerSupport. The containers advertise localhost, and allow for a slow start with two startup attempts and a timeout of three minutes.

// The image of the Pulsar version that this project is built with
PulsarContainer pulsar = PulsarTestImage.createContainer();

// Another version of the apachepulsar/pulsar image
PulsarContainer pulsar = PulsarTestImage.createContainer("5.0.1");

// Another image, which is assumed to be compatible with apachepulsar/pulsar
PulsarContainer pulsar = PulsarTestImage.createContainer(DockerImageName.parse("registry.example.com/pulsar:5.0.1"));

The containers are not started, and you can customize them further, for example with withTransactions().

Your project needs a dependency on org.testcontainers:testcontainers-pulsar to use this class.

3.1. Setting the default image

By default, PulsarTestImage uses the apachepulsar/pulsar image of the Pulsar version that this project is built with. To run your tests with another image or version without changing them, set one of the following to the image name, including its version as the tag:

  • The pulsar.test.image system property, which takes precedence

  • The PULSAR_TEST_IMAGE environment variable

For example, to run the tests with another Pulsar version or with an image from another registry:

PULSAR_TEST_IMAGE=apachepulsar/pulsar:5.0.1 ./gradlew test
PULSAR_TEST_IMAGE=registry.example.com/pulsar:5.0.1 ./gradlew test

An image name without a tag uses the latest tag.

This changes the image of PulsarTestImage.createContainer() and of the shared container in PulsarTestContainerSupport. It does not change createContainer(String) and createContainer(DockerImageName), which use the version or the image that they are given.

Gradle does not pass its own system properties to the JVM that runs the tests, so with Gradle, use the environment variable or set the system property on the Test task.