Debug with unit test

Like any function with inputs and outputs, you can test Pulsar Functions in a similar way as you test any other function.

Debug with unit test - 图1note

Pulsar uses TestNG for testing.

For example, if you have the following function written through the language-native interface for Java:

  1. import java.util.function.Function;
  2. public class JavaNativeExclamationFunction implements Function<String, String> {
  3. @Override
  4. public String apply(String input) {
  5. return String.format("%s!", input);
  6. }
  7. }

You can write a simple unit test to test the function.

  1. @Test
  2. public void testJavaNativeExclamationFunction() {
  3. JavaNativeExclamationFunction exclamation = new JavaNativeExclamationFunction();
  4. String output = exclamation.apply("foo");
  5. Assert.assertEquals(output, "foo!");
  6. }

The following example is written through the Java SDK.

  1. import org.apache.pulsar.functions.api.Context;
  2. import org.apache.pulsar.functions.api.Function;
  3. public class ExclamationFunction implements Function<String, String> {
  4. @Override
  5. public String process(String input, Context context) {
  6. return String.format("%s!", input);
  7. }
  8. }

You can write a unit test to test this function and mock the Context parameter as follows.

  1. @Test
  2. public void testExclamationFunction() {
  3. ExclamationFunction exclamation = new ExclamationFunction();
  4. String output = exclamation.process("foo", mock(Context.class));
  5. Assert.assertEquals(output, "foo!");
  6. }