简体   繁体   中英

NiFi: How to develop a JUnit test of NiFi controller service?

I am developing a NiFi controller service, and this controller service has a property of another controller service which is dbcpservice. Accoding to the source code in github.com/apache/nifi, controller service test depends on processor test too, that means define a TestProcessor which has a property of self-defined controller service, and then operate the the controller service through this processor. But in my case, my controller service's property is another controller service(dbcpservice), I do not know how to write a junit test to set the controller service.

You can create a simple processor via anonymous inner classes (or named classes) in your test in order to exercise the behavior of your controller service. For example, DBCPServiceTest does this by defining TestProcessor alongside the test class. All that test processor needs is a property descriptor which accepts a controller service of the same type as the system under test (SUT) -- in this case, your custom controller service.

If you're asking how to configure your custom service ( CustomService from here on), you pass parameters to the TestRunner instance, like so:

final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
final CustomService service = new CustomService();
service.setNestedService(new NestedService());
final Map<String, String> properties = new HashMap<String, String>();
runner.addControllerService("custom-service-id", service, properties);

In addition to Andy's answer - you could also create a test processor with Mockito:

AbstractProcessor processor = new Mockito().spy(AbstractProcessor.class);
TestRunner testRunner = TestRunners.newTestRunner(processor);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM