Class CamelTestSupport

  • All Implemented Interfaces:
    org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.AfterTestExecutionCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.BeforeTestExecutionCallback, org.junit.jupiter.api.extension.Extension
    Direct Known Subclasses:
    EndpointUriFactoryTestSupport, ExchangeTestSupport

    public abstract class CamelTestSupport
    extends Object
    implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeTestExecutionCallback, org.junit.jupiter.api.extension.AfterTestExecutionCallback
    A useful base class which creates a CamelContext with some routes along with a ProducerTemplate for use in the test case Do not use this class for Spring Boot testing.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void afterAll​(org.junit.jupiter.api.extension.ExtensionContext context)  
      void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)  
      void afterTestExecution​(org.junit.jupiter.api.extension.ExtensionContext context)  
      protected void applyCamelPostProcessor()
      Applies the CamelBeanPostProcessor to this instance.
      protected void assertExpression​(org.apache.camel.Exchange exchange, String languageName, String expressionText, Object expectedValue)
      Asserts that the given language name and expression evaluates to the given value on a specific exchange
      protected void assertPredicate​(String languageName, String expressionText, org.apache.camel.Exchange exchange, boolean expected)
      Asserts that the given language name and predicate expression evaluates to the expected value on the message exchange
      protected org.apache.camel.spi.Language assertResolveLanguage​(String languageName)
      Asserts that the language name can be resolved
      protected void assertValidContext​(org.apache.camel.CamelContext context)  
      void beforeAll​(org.junit.jupiter.api.extension.ExtensionContext context)  
      void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext context)  
      void beforeTestExecution​(org.junit.jupiter.api.extension.ExtensionContext context)  
      protected void bindToRegistry​(org.apache.camel.spi.Registry registry)
      Allows to bind custom beans to the Camel Registry.
      org.apache.camel.Service camelContextService()  
      protected void cleanupResources()
      Strategy to perform resources cleanup, after CamelContext is stopped
      org.apache.camel.ConsumerTemplate consumer()  
      org.apache.camel.CamelContext context()  
      protected org.apache.camel.CamelContext createCamelContext()  
      protected org.apache.camel.spi.Registry createCamelRegistry()
      Override to use a custom Registry.
      protected org.apache.camel.Exchange createExchangeWithBody​(Object body)
      Creates an exchange with the given body
      protected org.apache.camel.RoutesBuilder createRouteBuilder()
      Factory method which derived classes can use to create a RouteBuilder to define the routes for testing
      protected org.apache.camel.RoutesBuilder[] createRouteBuilders()
      Factory method which derived classes can use to create an array of RouteBuilders to define the routes for testing
      protected void debugAfter​(org.apache.camel.Exchange exchange, org.apache.camel.Processor processor, org.apache.camel.model.ProcessorDefinition<?> definition, String id, String label, long timeTaken)
      Single step debugs and Camel invokes this method after processing the given processor
      protected void debugBefore​(org.apache.camel.Exchange exchange, org.apache.camel.Processor processor, org.apache.camel.model.ProcessorDefinition<?> definition, String id, String label)
      Single step debugs and Camel invokes this method before entering the given processor
      protected void disableJMX()
      Disables the JMX agent.
      protected void doPostSetup()
      Strategy to perform any post setup after CamelContext is created
      protected void doPostTearDown()
      Strategy to perform any post action, after CamelContext is stopped
      protected void doPreSetup()
      Strategy to perform any pre setup, before CamelContext is created
      protected void doQuarkusCheck()
      Detects if this is a Camel-quarkus test and throw an exception, as these base classes is not intended for testing Camel onQuarkus.
      protected void doSetUp()  
      protected void doSpringBootCheck()
      Detects if this is a Spring-Boot test and throws an exception, as these base classes is not intended for testing Camel on Spring Boot.
      protected void doStopCamelContext​(org.apache.camel.CamelContext context, org.apache.camel.Service camelContextService)  
      protected void enableJMX()
      Enables the JMX agent.
      protected static String fileUri​(Path testDirectory)
      Deprecated.
      protected static String fileUri​(Path testDirectory, String query)
      Deprecated.
      org.apache.camel.FluentProducerTemplate fluentTemplate()  
      org.apache.camel.Service getCamelContextService()  
      String getCurrentTestName()
      Gets the name of the current test being executed.
      protected org.apache.camel.Endpoint getMandatoryEndpoint​(String uri)  
      protected <T extends org.apache.camel.Endpoint>
      T
      getMandatoryEndpoint​(String uri, Class<T> type)  
      protected org.apache.camel.component.mock.MockEndpoint getMockEndpoint​(String uri)
      Resolves the mandatory Mock endpoint using a URI of the form mock:someName
      protected org.apache.camel.component.mock.MockEndpoint getMockEndpoint​(String uri, boolean create)
      Resolves the MockEndpoint using a URI of the form mock:someName, optionally creating it if it does not exist.
      String getRouteFilterExcludePattern()
      Used for filtering routes matching the given pattern, which follows the following rules:
      String getRouteFilterIncludePattern()
      Used for filtering routes matching the given pattern, which follows the following rules:
      protected int getShutdownTimeout()
      Returns the timeout to use when shutting down (unit in seconds).
      protected boolean hasClassAnnotation​(String... names)
      Does this test class have any of the following annotations on the class-level.
      protected Boolean ignoreMissingLocationWithPropertiesComponent()
      Whether to ignore missing locations with the PropertiesComponent.
      boolean isCreateCamelContextPerClass()
      Tells whether CamelContext should be setup per test or per class.
      boolean isDumpRouteCoverage()
      Whether to dump route coverage stats at the end of the test.
      String isMockEndpoints()
      Override to enable auto mocking endpoints based on the pattern.
      String isMockEndpointsAndSkip()
      Override to enable auto mocking endpoints based on the pattern, and skip sending to original endpoint.
      boolean isUseAdviceWith()
      Override when using advice with and return true.
      boolean isUseDebugger()
      Override to enable debugger
      boolean isUseRouteBuilder()
      Use the RouteBuilder or not
      protected void postProcessTest()  
      void replaceRouteFromWith​(String routeId, String fromEndpoint)  
      protected org.apache.camel.Endpoint resolveMandatoryEndpoint​(String uri)
      Resolves a mandatory endpoint for the given URI or an exception is thrown
      protected <T extends org.apache.camel.Endpoint>
      T
      resolveMandatoryEndpoint​(String uri, Class<T> endpointType)
      Resolves a mandatory endpoint for the given URI and expected type or an exception is thrown
      protected void sendBodies​(String endpointUri, Object... bodies)
      Sends messages to the given endpoint for each of the specified bodies
      protected void sendBody​(String endpointUri, Object body)
      Sends a message to the given endpoint URI with the body value
      protected void sendBody​(String endpointUri, Object body, Map<String,​Object> headers)
      Sends a message to the given endpoint URI with the body value and specified headers
      void setCamelContextService​(org.apache.camel.Service service)
      Allows a service to be registered a separate lifecycle service to start and stop the context; such as for Spring when the ApplicationContext is started and stopped, rather than directly stopping the CamelContext
      void setUp()  
      protected void setupResources()
      Strategy to perform resources setup, before CamelContext is created
      void setUseRouteBuilder​(boolean useRouteBuilder)  
      protected void startCamelContext()  
      protected void stopCamelContext()  
      void tearDown()  
      org.apache.camel.ProducerTemplate template()  
      long timeTaken()  
      protected boolean useJmx()
      Whether or not JMX should be used during testing.
      protected Properties useOverridePropertiesWithPropertiesComponent()
      Override this method to include and override properties with the Camel PropertiesComponent.
    • Field Detail

      • ROUTE_COVERAGE_ENABLED

        public static final String ROUTE_COVERAGE_ENABLED
        JVM system property which can be set to true to turn on dumping route coverage statistics.
        See Also:
        Constant Field Values
      • context

        protected volatile org.apache.camel.model.ModelCamelContext context
      • template

        protected volatile org.apache.camel.ProducerTemplate template
      • fluentTemplate

        protected volatile org.apache.camel.FluentProducerTemplate fluentTemplate
      • consumer

        protected volatile org.apache.camel.ConsumerTemplate consumer
      • camelContextService

        protected volatile org.apache.camel.Service camelContextService
      • camelTestSupportExtension

        @RegisterExtension
        protected CamelTestSupport camelTestSupportExtension
    • Constructor Detail

      • CamelTestSupport

        public CamelTestSupport()
    • Method Detail

      • afterTestExecution

        public void afterTestExecution​(org.junit.jupiter.api.extension.ExtensionContext context)
                                throws Exception
        Specified by:
        afterTestExecution in interface org.junit.jupiter.api.extension.AfterTestExecutionCallback
        Throws:
        Exception
      • beforeTestExecution

        public void beforeTestExecution​(org.junit.jupiter.api.extension.ExtensionContext context)
                                 throws Exception
        Specified by:
        beforeTestExecution in interface org.junit.jupiter.api.extension.BeforeTestExecutionCallback
        Throws:
        Exception
      • timeTaken

        public long timeTaken()
      • beforeEach

        public void beforeEach​(org.junit.jupiter.api.extension.ExtensionContext context)
                        throws Exception
        Specified by:
        beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
        Throws:
        Exception
      • afterEach

        public void afterEach​(org.junit.jupiter.api.extension.ExtensionContext context)
                       throws Exception
        Specified by:
        afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
        Throws:
        Exception
      • beforeAll

        public void beforeAll​(org.junit.jupiter.api.extension.ExtensionContext context)
        Specified by:
        beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
      • afterAll

        public void afterAll​(org.junit.jupiter.api.extension.ExtensionContext context)
        Specified by:
        afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
      • isUseRouteBuilder

        public boolean isUseRouteBuilder()
        Use the RouteBuilder or not
        Returns:
        true then CamelContext will be auto started, false then CamelContext will not be auto started (you will have to start it manually)
      • setUseRouteBuilder

        public void setUseRouteBuilder​(boolean useRouteBuilder)
      • isDumpRouteCoverage

        public boolean isDumpRouteCoverage()
        Whether to dump route coverage stats at the end of the test.

        This allows tooling or manual inspection of the stats, so you can generate a route trace diagram of which EIPs have been in use and which have not. Similar concepts as a code coverage report.

        You can also turn on route coverage globally via setting JVM system property CamelTestRouteCoverage=true.

        Returns:
        true to write route coverage status in an xml file in the target/camel-route-coverage directory after the test has finished.
      • isUseAdviceWith

        public boolean isUseAdviceWith()
        Override when using advice with and return true. This helps knowing advice with is to be used, and CamelContext will not be started before the advice with takes place. This helps by ensuring the advice with has been property setup before the CamelContext is started

        Important: Its important to start CamelContext manually from the unit test after you are done doing all the advice with.

        Returns:
        true if you use advice with in your unit tests.
      • isCreateCamelContextPerClass

        public final boolean isCreateCamelContextPerClass()
        Tells whether CamelContext should be setup per test or per class.

        By default it will be setup/teardown per test method. This method returns true when the camel test class is annotated with @TestInstance(TestInstance.Lifecycle.PER_CLASS).

        Important: Use this with care as the CamelContext will carry over state from previous tests, such as endpoints, components etc. So you cannot use this in all your tests.

        Setting up CamelContext uses the doPreSetup(), doSetUp(), and doPostSetup() methods in that given order.

        Returns:
        true per class, false per test.
      • isMockEndpoints

        public String isMockEndpoints()
        Override to enable auto mocking endpoints based on the pattern.

        Return * to mock all endpoints.

        See Also:
        EndpointHelper.matchEndpoint(CamelContext, String, String)
      • isMockEndpointsAndSkip

        public String isMockEndpointsAndSkip()
        Override to enable auto mocking endpoints based on the pattern, and skip sending to original endpoint.

        Return * to mock all endpoints.

        See Also:
        EndpointHelper.matchEndpoint(CamelContext, String, String)
      • replaceRouteFromWith

        public void replaceRouteFromWith​(String routeId,
                                         String fromEndpoint)
      • getRouteFilterIncludePattern

        public String getRouteFilterIncludePattern()
        Used for filtering routes matching the given pattern, which follows the following rules:

        - Match by route id - Match by route input endpoint uri

        The matching is using exact match, by wildcard and regular expression.

        For example to only include routes which starts with foo in their route id's, use: include=foo* And to exclude routes which starts from JMS endpoints, use: exclude=jms:*

        Multiple patterns can be separated by comma, for example to exclude both foo and bar routes, use: exclude=foo*,bar*

        Exclude takes precedence over include.

      • getRouteFilterExcludePattern

        public String getRouteFilterExcludePattern()
        Used for filtering routes matching the given pattern, which follows the following rules:

        - Match by route id - Match by route input endpoint uri

        The matching is using exact match, by wildcard and regular expression.

        For example to only include routes which starts with foo in their route id's, use: include=foo* And to exclude routes which starts from JMS endpoints, use: exclude=jms:*

        Multiple patterns can be separated by comma, for example to exclude both foo and bar routes, use: exclude=foo*,bar*

        Exclude takes precedence over include.

      • getCurrentTestName

        public final String getCurrentTestName()
        Gets the name of the current test being executed.
      • isUseDebugger

        public boolean isUseDebugger()
        Override to enable debugger

        Is default false

      • getCamelContextService

        public org.apache.camel.Service getCamelContextService()
      • camelContextService

        public org.apache.camel.Service camelContextService()
      • context

        public org.apache.camel.CamelContext context()
      • template

        public org.apache.camel.ProducerTemplate template()
      • fluentTemplate

        public org.apache.camel.FluentProducerTemplate fluentTemplate()
      • consumer

        public org.apache.camel.ConsumerTemplate consumer()
      • setCamelContextService

        public void setCamelContextService​(org.apache.camel.Service service)
        Allows a service to be registered a separate lifecycle service to start and stop the context; such as for Spring when the ApplicationContext is started and stopped, rather than directly stopping the CamelContext
      • doPreSetup

        protected void doPreSetup()
                           throws Exception
        Strategy to perform any pre setup, before CamelContext is created
        Throws:
        Exception
      • doPostSetup

        protected void doPostSetup()
                            throws Exception
        Strategy to perform any post setup after CamelContext is created
        Throws:
        Exception
      • doSpringBootCheck

        protected void doSpringBootCheck()
        Detects if this is a Spring-Boot test and throws an exception, as these base classes is not intended for testing Camel on Spring Boot.
      • doQuarkusCheck

        protected void doQuarkusCheck()
        Detects if this is a Camel-quarkus test and throw an exception, as these base classes is not intended for testing Camel onQuarkus.
      • doPostTearDown

        protected void doPostTearDown()
                               throws Exception
        Strategy to perform any post action, after CamelContext is stopped
        Throws:
        Exception
      • setupResources

        protected void setupResources()
                               throws Exception
        Strategy to perform resources setup, before CamelContext is created
        Throws:
        Exception
      • cleanupResources

        protected void cleanupResources()
                                 throws Exception
        Strategy to perform resources cleanup, after CamelContext is stopped
        Throws:
        Exception
      • getShutdownTimeout

        protected int getShutdownTimeout()
        Returns the timeout to use when shutting down (unit in seconds).

        Will default use 10 seconds.

        Returns:
        the timeout to use
      • useJmx

        protected boolean useJmx()
        Whether or not JMX should be used during testing.
        Returns:
        false by default.
      • useOverridePropertiesWithPropertiesComponent

        protected Properties useOverridePropertiesWithPropertiesComponent()
        Override this method to include and override properties with the Camel PropertiesComponent.
        Returns:
        additional properties to add/override.
      • ignoreMissingLocationWithPropertiesComponent

        protected Boolean ignoreMissingLocationWithPropertiesComponent()
        Whether to ignore missing locations with the PropertiesComponent. For example when unit testing you may want to ignore locations that are not available in the environment used for testing.
        Returns:
        true to ignore, false to not ignore, and null to leave as configured on the PropertiesComponent
      • applyCamelPostProcessor

        protected void applyCamelPostProcessor()
                                        throws Exception
        Applies the CamelBeanPostProcessor to this instance.

        Derived classes using IoC / DI frameworks may wish to turn this into a NoOp such as for CDI we would just use CDI to inject this

        Throws:
        Exception
      • hasClassAnnotation

        protected boolean hasClassAnnotation​(String... names)
        Does this test class have any of the following annotations on the class-level.
      • doStopCamelContext

        protected void doStopCamelContext​(org.apache.camel.CamelContext context,
                                          org.apache.camel.Service camelContextService)
      • startCamelContext

        protected void startCamelContext()
                                  throws Exception
        Throws:
        Exception
      • createCamelContext

        protected org.apache.camel.CamelContext createCamelContext()
                                                            throws Exception
        Throws:
        Exception
      • bindToRegistry

        protected void bindToRegistry​(org.apache.camel.spi.Registry registry)
                               throws Exception
        Allows to bind custom beans to the Camel Registry.
        Throws:
        Exception
      • createCamelRegistry

        protected org.apache.camel.spi.Registry createCamelRegistry()
                                                             throws Exception
        Override to use a custom Registry.

        However if you need to bind beans to the registry then this is possible already with the bind method on registry, and there is no need to override this method.

        Throws:
        Exception
      • createRouteBuilder

        protected org.apache.camel.RoutesBuilder createRouteBuilder()
                                                             throws Exception
        Factory method which derived classes can use to create a RouteBuilder to define the routes for testing
        Throws:
        Exception
      • createRouteBuilders

        protected org.apache.camel.RoutesBuilder[] createRouteBuilders()
                                                                throws Exception
        Factory method which derived classes can use to create an array of RouteBuilders to define the routes for testing
        Throws:
        Exception
        See Also:
        createRouteBuilder()
      • resolveMandatoryEndpoint

        protected org.apache.camel.Endpoint resolveMandatoryEndpoint​(String uri)
        Resolves a mandatory endpoint for the given URI or an exception is thrown
        Parameters:
        uri - the Camel URI to use to create or resolve an endpoint
        Returns:
        the endpoint
      • resolveMandatoryEndpoint

        protected <T extends org.apache.camel.Endpoint> T resolveMandatoryEndpoint​(String uri,
                                                                                   Class<T> endpointType)
        Resolves a mandatory endpoint for the given URI and expected type or an exception is thrown
        Parameters:
        uri - the Camel URI to use to create or resolve an endpoint
        Returns:
        the endpoint
      • getMockEndpoint

        protected org.apache.camel.component.mock.MockEndpoint getMockEndpoint​(String uri)
        Resolves the mandatory Mock endpoint using a URI of the form mock:someName
        Parameters:
        uri - the URI which typically starts with "mock:" and has some name
        Returns:
        the mandatory mock endpoint or an exception is thrown if it could not be resolved
      • getMockEndpoint

        protected org.apache.camel.component.mock.MockEndpoint getMockEndpoint​(String uri,
                                                                               boolean create)
                                                                        throws org.apache.camel.NoSuchEndpointException
        Resolves the MockEndpoint using a URI of the form mock:someName, optionally creating it if it does not exist. This implementation will lookup existing mock endpoints and match on the mock queue name, eg mock:foo and mock:foo?retainFirst=5 would match as the queue name is foo.
        Parameters:
        uri - the URI which typically starts with "mock:" and has some name
        create - whether or not to allow the endpoint to be created if it doesn't exist
        Returns:
        the mock endpoint or an NoSuchEndpointException is thrown if it could not be resolved
        Throws:
        org.apache.camel.NoSuchEndpointException - is the mock endpoint does not exists
      • sendBody

        protected void sendBody​(String endpointUri,
                                Object body)
        Sends a message to the given endpoint URI with the body value
        Parameters:
        endpointUri - the URI of the endpoint to send to
        body - the body for the message
      • sendBody

        protected void sendBody​(String endpointUri,
                                Object body,
                                Map<String,​Object> headers)
        Sends a message to the given endpoint URI with the body value and specified headers
        Parameters:
        endpointUri - the URI of the endpoint to send to
        body - the body for the message
        headers - any headers to set on the message
      • sendBodies

        protected void sendBodies​(String endpointUri,
                                  Object... bodies)
        Sends messages to the given endpoint for each of the specified bodies
        Parameters:
        endpointUri - the endpoint URI to send to
        bodies - the bodies to send, one per message
      • createExchangeWithBody

        protected org.apache.camel.Exchange createExchangeWithBody​(Object body)
        Creates an exchange with the given body
      • assertExpression

        protected void assertExpression​(org.apache.camel.Exchange exchange,
                                        String languageName,
                                        String expressionText,
                                        Object expectedValue)
        Asserts that the given language name and expression evaluates to the given value on a specific exchange
      • assertPredicate

        protected void assertPredicate​(String languageName,
                                       String expressionText,
                                       org.apache.camel.Exchange exchange,
                                       boolean expected)
        Asserts that the given language name and predicate expression evaluates to the expected value on the message exchange
      • assertResolveLanguage

        protected org.apache.camel.spi.Language assertResolveLanguage​(String languageName)
        Asserts that the language name can be resolved
      • assertValidContext

        protected void assertValidContext​(org.apache.camel.CamelContext context)
      • getMandatoryEndpoint

        protected <T extends org.apache.camel.Endpoint> T getMandatoryEndpoint​(String uri,
                                                                               Class<T> type)
      • getMandatoryEndpoint

        protected org.apache.camel.Endpoint getMandatoryEndpoint​(String uri)
      • disableJMX

        protected void disableJMX()
        Disables the JMX agent. Must be called before the setUp() method.
      • enableJMX

        protected void enableJMX()
        Enables the JMX agent. Must be called before the setUp() method.
      • debugBefore

        protected void debugBefore​(org.apache.camel.Exchange exchange,
                                   org.apache.camel.Processor processor,
                                   org.apache.camel.model.ProcessorDefinition<?> definition,
                                   String id,
                                   String label)
        Single step debugs and Camel invokes this method before entering the given processor
      • debugAfter

        protected void debugAfter​(org.apache.camel.Exchange exchange,
                                  org.apache.camel.Processor processor,
                                  org.apache.camel.model.ProcessorDefinition<?> definition,
                                  String id,
                                  String label,
                                  long timeTaken)
        Single step debugs and Camel invokes this method after processing the given processor