Class CamelTestSupport

java.lang.Object
org.apache.camel.test.junit5.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.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected org.apache.camel.Service
     
     
    protected org.apache.camel.ConsumerTemplate
     
    protected org.apache.camel.model.ModelCamelContext
     
    protected Properties
     
    protected org.apache.camel.FluentProducerTemplate
     
    static final String
    JVM system property which can be set to true to turn on dumping route coverage statistics.
    static final String
     
    protected org.apache.camel.ProducerTemplate
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    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
    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
    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
     
    protected void
    Strategy to perform resources cleanup, after CamelContext is stopped
    org.apache.camel.ConsumerTemplate
     
    org.apache.camel.CamelContext
     
    protected org.apache.camel.CamelContext
     
    protected org.apache.camel.spi.Registry
    Override to use a custom Registry.
    protected org.apache.camel.Exchange
    Creates an exchange with the given body
    protected org.apache.camel.RoutesBuilder
    Factory method which derived classes can use to create a RouteBuilder to define the routes for testing
    protected org.apache.camel.RoutesBuilder[]
    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
    Disables the JMX agent.
    protected void
    Strategy to perform any post setup after CamelContext is created
    protected void
    Strategy to perform any post action, after CamelContext is stopped
    protected void
    Strategy to perform any pre setup, before CamelContext is created
    protected void
    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
     
    protected void
    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
    Enables the JMX agent.
    org.apache.camel.FluentProducerTemplate
     
    org.apache.camel.Service
     
    final String
    Gets the name of the current test being executed.
    protected org.apache.camel.Endpoint
     
    protected <T extends org.apache.camel.Endpoint>
    T
     
    protected org.apache.camel.component.mock.MockEndpoint
    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.
    Used for filtering routes matching the given pattern, which follows the following rules:
    Used for filtering routes matching the given pattern, which follows the following rules:
    protected int
    Returns the timeout to use when shutting down (unit in seconds).
    protected boolean
    Does this test class have any of the following annotations on the class-level.
    protected Boolean
    Whether to ignore missing locations with the PropertiesComponent.
    final boolean
    Tells whether CamelContext should be setup per test or per class.
    boolean
    Whether to dump route coverage stats at the end of the test.
    Override to enable auto mocking endpoints based on the pattern.
    Override to enable auto mocking endpoints based on the pattern, and skip sending to original endpoint.
    boolean
    Override when using advice with and return true.
    boolean
    Override to enable debugger
    boolean
    Use the RouteBuilder or not
    protected void
     
    void
    replaceRouteFromWith(String routeId, String fromEndpoint)
     
    protected org.apache.camel.Endpoint
    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
     
    protected void
    Strategy to perform resources setup, before CamelContext is created
    void
    setUseRouteBuilder(boolean useRouteBuilder)
     
    protected void
     
    protected void
     
    void
     
    org.apache.camel.ProducerTemplate
     
    long
     
    protected boolean
    Whether or not JMX should be used during testing.
    protected Properties
    Override this method to include and override properties with the Camel PropertiesComponent.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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:
    • SEPARATOR

      public static final String SEPARATOR
      See Also:
    • extra

      protected Properties extra
    • 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 Details

    • CamelTestSupport

      public CamelTestSupport()
  • Method Details

    • 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
    • setUp

      @BeforeEach public void setUp() throws Exception
      Throws:
      Exception
    • 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.
    • doSetUp

      protected void doSetUp() throws Exception
      Throws:
      Exception
    • tearDown

      @AfterEach public void tearDown() throws Exception
      Throws:
      Exception
    • 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
    • postProcessTest

      protected void postProcessTest() throws Exception
      Throws:
      Exception
    • 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.
    • stopCamelContext

      protected void stopCamelContext() throws Exception
      Throws:
      Exception
    • 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:
    • 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 exist
    • 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