
Testing your custom Nifi processor
After you create a custom NiFi processor, you can verify that it works using a simple set of steps.
Procedure
-
Create a class under src/test/java and declare a field with data type
org.apache.nifi.util.TestRunner.
public class MyProcessorTest { private TestRunner testRunner;
-
Create a method init() annotated with JUnit’s @Before annotation and initialize
the TestRunner.
@Before public void init() { testRunner = TestRunners.newTestRunner(MyProcessor.class); }
-
Create another method and annotate it with JUnit’s @Test. Also, get a JSON
payload which will represent the input to the processor.
@Test public void testResponse() throws Exception { final Path JSON_SNIPPET = Paths.get("src/test/resources/common/MyProcessor/scroll_input.json"); final Path JSON_SNIPPET1 = Paths.get("src/test/resources/common/MyProcessor/scroll_input_zero_doc.json");
-
In the same method, set all the required attributes in the test runner as
follows:
testRunner.setProperty(MyProcessor.SCROLL_DURATION, "1m"); Map<String, String> attributes = new HashMap<String, String>(); attributes.put(PARAM_DOT_STOREID, "1"); attributes.put(CONNECTOR_DOT_NAME, "auth.reindex"); attributes.put(ENVIRONMENT_DOT_NAME, "auth"); testRunner.enqueue(JSON_SNIPPET, attributes); testRunner.run(); attributes.put("index.scroll.uri", "_search/scroll"); testRunner.enqueue(JSON_SNIPPET1, attributes); testRunner.run();
-
In the same method, also set the expected number of FlowFiles transferred to
the respective relationships as follows:
Relationship expectedRel = MyProcessor.RELATIONSHIP_SUCCESS; testRunner.assertTransferCount(expectedRel, 1); expectedRel = MyProcessor.RELATIONSHIP_FAILURE; testRunner.assertTransferCount(expectedRel, 0);
-
Run this class as a JUnit Test. The result of the execution appears under the
JUnit View.