Skip to content
Advertisement

Camel unit test with cameltestsupport, template is always null

I am doing a simple unit test with Camel. All I want to do is to read JSON content from a file (under resources), send it to a Java class for validation – this is the route that I am trying to test. Whatever I do, the template (which I use to sendBody(json) is always null. Here is my code:

JavaScript

Problems I faced:

  1. Initially the result end point also was always null. (I used FilterTest.java for reference). Then I had to do an explicit

    JavaScript

    to resolve that.

  2. Then I read that I had to override the createRegistry, but I did not know how to bind. I just used the name of my validation class, but I don’t know if this is right.

    But the template is always null. The null pointer exception (NPE) is at

    JavaScript

Please also point me to some reading if necessary. The reference code that Apache Camel documentation link to did not even have the starting of the Camel that I do in the setUp method.

Advertisement

Answer

I think you’ve missed out on a lot of the really helpful stuff that CamelTestSupport does for you. It has its own setUp method that you should override. I believe your test should really look something like this:

JavaScript

Actually I would remove the override of setUp altogether and put the reading of the test data in to the test method itself. Then it’s clear what the data is being used for and you can eliminate the testJson field.

JavaScript

There, much simpler.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement