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
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
FieldsModifier and TypeFieldDescriptionprotected org.apache.camel.Service
protected CamelTestSupport
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 -
Method Summary
Modifier and TypeMethodDescriptionvoid
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 theCamelBeanPostProcessor
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 exchangeprotected 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 exchangeprotected org.apache.camel.spi.Language
assertResolveLanguage
(String languageName) Asserts that the language name can be resolvedprotected 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 CamelRegistry
.org.apache.camel.Service
protected void
Strategy to perform resources cleanup, afterCamelContext
is stoppedorg.apache.camel.ConsumerTemplate
consumer()
org.apache.camel.CamelContext
context()
protected org.apache.camel.CamelContext
protected org.apache.camel.spi.Registry
Override to use a customRegistry
.protected org.apache.camel.Exchange
createExchangeWithBody
(Object body) Creates an exchange with the given bodyprotected org.apache.camel.RoutesBuilder
Factory method which derived classes can use to create aRouteBuilder
to define the routes for testingprotected org.apache.camel.RoutesBuilder[]
Factory method which derived classes can use to create an array ofRouteBuilder
s to define the routes for testingprotected 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 processorprotected 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 processorprotected void
Disables the JMX agent.protected void
Strategy to perform any post setup afterCamelContext
is createdprotected void
Strategy to perform any post action, afterCamelContext
is stoppedprotected void
Strategy to perform any pre setup, beforeCamelContext
is createdprotected 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
doSetUp()
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>
TgetMandatoryEndpoint
(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 formmock:someName
protected org.apache.camel.component.mock.MockEndpoint
getMockEndpoint
(String uri, boolean create) Resolves theMockEndpoint
using a URI of the formmock: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
hasClassAnnotation
(String... names) Does this test class have any of the following annotations on the class-level.protected Boolean
Whether to ignore missing locations with thePropertiesComponent
.final boolean
Tells whetherCamelContext
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 debuggerboolean
Use the RouteBuilder or notprotected void
void
replaceRouteFromWith
(String routeId, String fromEndpoint) protected org.apache.camel.Endpoint
Resolves a mandatory endpoint for the given URI or an exception is thrownprotected <T extends org.apache.camel.Endpoint>
TresolveMandatoryEndpoint
(String uri, Class<T> endpointType) Resolves a mandatory endpoint for the given URI and expected type or an exception is thrownprotected void
sendBodies
(String endpointUri, Object... bodies) Sends messages to the given endpoint for each of the specified bodiesprotected void
Sends a message to the given endpoint URI with the body valueprotected void
Sends a message to the given endpoint URI with the body value and specified headersvoid
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 CamelContextvoid
setUp()
protected void
Strategy to perform resources setup, beforeCamelContext
is createdvoid
setUseRouteBuilder
(boolean useRouteBuilder) protected void
protected void
void
tearDown()
org.apache.camel.ProducerTemplate
template()
long
protected boolean
useJmx()
Whether or not JMX should be used during testing.protected Properties
Override this method to include and override properties with the CamelPropertiesComponent
.
-
Field Details
-
ROUTE_COVERAGE_ENABLED
JVM system property which can be set to true to turn on dumping route coverage statistics.- See Also:
-
SEPARATOR
- See Also:
-
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
-
-
Constructor Details
-
CamelTestSupport
public CamelTestSupport()
-
-
Method Details
-
afterTestExecution
public void afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception - Specified by:
afterTestExecution
in interfaceorg.junit.jupiter.api.extension.AfterTestExecutionCallback
- Throws:
Exception
-
beforeTestExecution
public void beforeTestExecution(org.junit.jupiter.api.extension.ExtensionContext context) throws Exception - Specified by:
beforeTestExecution
in interfaceorg.junit.jupiter.api.extension.BeforeTestExecutionCallback
- Throws:
Exception
-
timeTaken
public long timeTaken() -
beforeEach
- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Throws:
Exception
-
afterEach
- Specified by:
afterEach
in interfaceorg.junit.jupiter.api.extension.AfterEachCallback
- Throws:
Exception
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeAll
in interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
afterAll
in interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-
isUseRouteBuilder
public boolean isUseRouteBuilder()Use the RouteBuilder or not- Returns:
- true then
CamelContext
will be auto started, false thenCamelContext
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, andCamelContext
will not be started before the advice with takes place. This helps by ensuring the advice with has been property setup before theCamelContext
is started Important: Its important to startCamelContext
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 whetherCamelContext
should be setup per test or per class. By default it will be setup/teardown per test method. This method returnstrue
when the camel test class is annotated with @TestInstance(TestInstance.Lifecycle.PER_CLASS). Important: Use this with care as theCamelContext
will carry over state from previous tests, such as endpoints, components etc. So you cannot use this in all your tests. Setting upCamelContext
uses thedoPreSetup()
,doSetUp()
, anddoPostSetup()
methods in that given order.- Returns:
- true per class, false per test.
-
isMockEndpoints
Override to enable auto mocking endpoints based on the pattern. Return * to mock all endpoints.- See Also:
-
isMockEndpointsAndSkip
Override to enable auto mocking endpoints based on the pattern, and skip sending to original endpoint. Return * to mock all endpoints.- See Also:
-
replaceRouteFromWith
-
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
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
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
- Throws:
Exception
-
doPreSetup
Strategy to perform any pre setup, beforeCamelContext
is created- Throws:
Exception
-
doPostSetup
Strategy to perform any post setup afterCamelContext
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
- Throws:
Exception
-
tearDown
- Throws:
Exception
-
doPostTearDown
Strategy to perform any post action, afterCamelContext
is stopped- Throws:
Exception
-
setupResources
Strategy to perform resources setup, beforeCamelContext
is created- Throws:
Exception
-
cleanupResources
Strategy to perform resources cleanup, afterCamelContext
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
Override this method to include and override properties with the CamelPropertiesComponent
.- Returns:
- additional properties to add/override.
-
ignoreMissingLocationWithPropertiesComponent
Whether to ignore missing locations with thePropertiesComponent
. 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
- Throws:
Exception
-
applyCamelPostProcessor
Applies theCamelBeanPostProcessor
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
Does this test class have any of the following annotations on the class-level. -
stopCamelContext
- Throws:
Exception
-
doStopCamelContext
protected void doStopCamelContext(org.apache.camel.CamelContext context, org.apache.camel.Service camelContextService) -
startCamelContext
- Throws:
Exception
-
createCamelContext
- Throws:
Exception
-
bindToRegistry
Allows to bind custom beans to the CamelRegistry
.- Throws:
Exception
-
createCamelRegistry
Override to use a customRegistry
.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
Factory method which derived classes can use to create aRouteBuilder
to define the routes for testing- Throws:
Exception
-
createRouteBuilders
Factory method which derived classes can use to create an array ofRouteBuilder
s to define the routes for testing- Throws:
Exception
- See Also:
-
resolveMandatoryEndpoint
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
Resolves the mandatory Mock endpoint using a URI of the formmock: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 theMockEndpoint
using a URI of the formmock: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 namecreate
- 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
Sends a message to the given endpoint URI with the body value- Parameters:
endpointUri
- the URI of the endpoint to send tobody
- the body for the message
-
sendBody
Sends a message to the given endpoint URI with the body value and specified headers- Parameters:
endpointUri
- the URI of the endpoint to send tobody
- the body for the messageheaders
- any headers to set on the message
-
sendBodies
Sends messages to the given endpoint for each of the specified bodies- Parameters:
endpointUri
- the endpoint URI to send tobodies
- the bodies to send, one per message
-
createExchangeWithBody
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
Asserts that the language name can be resolved -
assertValidContext
protected void assertValidContext(org.apache.camel.CamelContext context) -
getMandatoryEndpoint
-
getMandatoryEndpoint
-
disableJMX
protected void disableJMX()Disables the JMX agent. Must be called before thesetUp()
method. -
enableJMX
protected void enableJMX()Enables the JMX agent. Must be called before thesetUp()
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
-