public abstract class CamelTestSupport extends TestSupport
CamelContext
with some routes
along with a ProducerTemplate
for use in the test case
Do not use this class for Spring Boot testing, instead use @RunWith(CamelSpringBootRunner.class)
.Modifier and Type | Field and Description |
---|---|
protected org.apache.camel.Service |
camelContextService |
protected org.apache.camel.ConsumerTemplate |
consumer |
protected org.apache.camel.model.ModelCamelContext |
context |
protected boolean |
dumpRouteStats |
protected org.apache.camel.FluentProducerTemplate |
fluentTemplate |
static String |
ROUTE_COVERAGE_ENABLED
JVM system property which can be set to true to turn on dumping route coverage statistics.
|
protected org.apache.camel.ProducerTemplate |
template |
log, LS
Constructor and Description |
---|
CamelTestSupport() |
Modifier and Type | Method and Description |
---|---|
protected void |
applyCamelPostProcessor()
Applies the
DefaultCamelBeanPostProcessor 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 |
assertMockEndpointsSatisfied()
Asserts that all the expectations of the Mock endpoints are valid
|
protected void |
assertMockEndpointsSatisfied(long timeout,
TimeUnit unit)
Asserts that all the expectations of the Mock endpoints are valid
|
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) |
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.Exchange |
createExchangeWithBody(Object body)
Creates an exchange with the given body
|
protected Context |
createJndiContext() |
protected org.apache.camel.impl.JndiRegistry |
createRegistry() |
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
RouteBuilder s 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 |
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 |
enableJMX()
Enables the JMX agent.
|
org.apache.camel.FluentProducerTemplate |
fluentTemplate() |
org.apache.camel.Service |
getCamelContextService() |
CamelTestWatcher |
getCamelTestWatcher() |
protected org.apache.camel.Endpoint |
getMandatoryEndpoint(String uri) |
protected <T extends org.apache.camel.Endpoint> |
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. |
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()
Override to control whether
CamelContext should be setup per test or per class. |
boolean |
isDumpRouteCoverage()
Whether to dump route coverage stats at the end of the test.
|
protected boolean |
isLazyLoadingTypeConverter()
Deprecated.
|
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
Is default false
|
boolean |
isUseRouteBuilder()
Use the RouteBuilder or not
|
protected void |
postProcessTest() |
void |
replaceRouteFromWith(String routeId,
String fromEndpoint) |
protected void |
resetMocks()
Reset all Mock endpoints.
|
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> |
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() |
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 . |
assertCollectionSize, assertCollectionSize, assertDirectoryEquals, assertDirectoryEquals, assertEndpointUri, assertExpression, assertFileExists, assertFileNotExists, assertInMessageBodyEquals, assertInMessageHeader, assertIsInstanceOf, assertListSize, assertListSize, assertMessageHeader, assertOneElement, assertOutMessageBodyEquals, assertOutMessageHeader, assertPredicate, assertPredicateDoesNotMatch, assertPredicateMatches, assertStringContains, body, bodyAs, createDirectory, createExchangeWithBody, deleteDirectory, deleteDirectory, exchangeProperty, faultBody, faultBodyAs, getJavaMajorVersion, getRouteList, getTestMethodName, getTestName, header, isJava15, isJava16, isJava17, isJava18, isJava19, isJavaVendor, isPlatform, outBody, outBodyAs, property, resolveMandatoryEndpoint, resolveMandatoryEndpoint, systemProperty, systemProperty, unwrap, unwrapChannel
assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotEquals, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertThat, assertThat, assertTrue, assertTrue, fail, fail
public static final String ROUTE_COVERAGE_ENABLED
protected volatile org.apache.camel.model.ModelCamelContext context
protected volatile org.apache.camel.ProducerTemplate template
protected volatile org.apache.camel.FluentProducerTemplate fluentTemplate
protected volatile org.apache.camel.ConsumerTemplate consumer
protected volatile org.apache.camel.Service camelContextService
protected boolean dumpRouteStats
public boolean isUseRouteBuilder()
CamelContext
will be auto started,
false then CamelContext
will not be auto started (you will have to start it manually)public void setUseRouteBuilder(boolean useRouteBuilder)
public boolean isDumpRouteCoverage()
public boolean isUseAdviceWith()
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.public boolean isCreateCamelContextPerClass()
CamelContext
should be setup per test or per class.
By default it will be setup/teardown per test (per test method). If you want to re-use
CamelContext
between test methods you can override this method and return true
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.public String isMockEndpoints()
EndpointHelper.matchEndpoint(String, String)
public String isMockEndpointsAndSkip()
EndpointHelper.matchEndpoint(String, String)
public boolean isUseDebugger()
public org.apache.camel.Service getCamelContextService()
public org.apache.camel.Service camelContextService()
public org.apache.camel.CamelContext context()
public org.apache.camel.ProducerTemplate template()
public org.apache.camel.FluentProducerTemplate fluentTemplate()
public org.apache.camel.ConsumerTemplate consumer()
public void setCamelContextService(org.apache.camel.Service service)
protected void doPreSetup() throws Exception
CamelContext
is createdException
protected void doPostSetup() throws Exception
CamelContext
is createdException
protected void doSpringBootCheck()
protected void doPostTearDown() throws Exception
CamelContext
is stoppedException
protected void setupResources() throws Exception
CamelContext
is createdException
protected void cleanupResources() throws Exception
CamelContext
is stoppedException
protected int getShutdownTimeout()
protected boolean useJmx()
@Deprecated protected boolean isLazyLoadingTypeConverter()
protected Properties useOverridePropertiesWithPropertiesComponent()
PropertiesComponent
.public CamelTestWatcher getCamelTestWatcher()
protected Boolean ignoreMissingLocationWithPropertiesComponent()
PropertiesComponent
.
For example when unit testing you may want to ignore locations that are
not available in the environment you use for testing.PropertiesComponent
protected void applyCamelPostProcessor() throws Exception
DefaultCamelBeanPostProcessor
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 thisException
protected boolean hasClassAnnotation(String... names)
protected org.apache.camel.CamelContext createCamelContext() throws Exception
Exception
protected org.apache.camel.impl.JndiRegistry createRegistry() throws Exception
Exception
protected org.apache.camel.RoutesBuilder createRouteBuilder() throws Exception
RouteBuilder
to define the routes for testingException
protected org.apache.camel.RoutesBuilder[] createRouteBuilders() throws Exception
RouteBuilder
s to define the routes for testingException
createRouteBuilder()
protected org.apache.camel.Endpoint resolveMandatoryEndpoint(String uri)
uri
- the Camel URI to use to create or resolve an endpointprotected <T extends org.apache.camel.Endpoint> T resolveMandatoryEndpoint(String uri, Class<T> endpointType)
uri
- the Camel URI to use to create or resolve an endpointprotected org.apache.camel.component.mock.MockEndpoint getMockEndpoint(String uri)
mock:someName
uri
- the URI which typically starts with "mock:" and has some nameprotected org.apache.camel.component.mock.MockEndpoint getMockEndpoint(String uri, boolean create) throws org.apache.camel.NoSuchEndpointException
MockEndpoint
using a URI of the form mock:someName
, optionally
creating it if it does not exist.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 existNoSuchEndpointException
is thrown if it could not be resolvedorg.apache.camel.NoSuchEndpointException
- is the mock endpoint does not existsprotected void sendBody(String endpointUri, Object body)
endpointUri
- the URI of the endpoint to send tobody
- the body for the messageprotected void sendBody(String endpointUri, Object body, Map<String,Object> headers)
endpointUri
- the URI of the endpoint to send tobody
- the body for the messageheaders
- any headers to set on the messageprotected void sendBodies(String endpointUri, Object... bodies)
endpointUri
- the endpoint URI to send tobodies
- the bodies to send, one per messageprotected org.apache.camel.Exchange createExchangeWithBody(Object body)
protected void assertExpression(org.apache.camel.Exchange exchange, String languageName, String expressionText, Object expectedValue)
protected void assertPredicate(String languageName, String expressionText, org.apache.camel.Exchange exchange, boolean expected)
protected org.apache.camel.spi.Language assertResolveLanguage(String languageName)
protected void assertMockEndpointsSatisfied() throws InterruptedException
InterruptedException
protected void assertMockEndpointsSatisfied(long timeout, TimeUnit unit) throws InterruptedException
InterruptedException
protected void resetMocks()
protected void assertValidContext(org.apache.camel.CamelContext context)
protected <T extends org.apache.camel.Endpoint> T getMandatoryEndpoint(String uri, Class<T> type)
protected org.apache.camel.Endpoint getMandatoryEndpoint(String uri)
protected void disableJMX()
setUp()
method.protected void enableJMX()
setUp()
method.protected void debugBefore(org.apache.camel.Exchange exchange, org.apache.camel.Processor processor, org.apache.camel.model.ProcessorDefinition<?> definition, String id, String label)
Apache Camel