Class BrokerRunning
java.lang.Object
org.junit.rules.TestWatcher
org.springframework.amqp.rabbit.junit.BrokerRunning
- All Implemented Interfaces:
org.junit.rules.TestRule
public final class BrokerRunning
extends org.junit.rules.TestWatcher
A rule that prevents integration tests from failing if the Rabbit broker application is
not running or not accessible. If the Rabbit broker is not running in the background
all the tests here will simply be skipped (by default) because of a violated assumption
(showing as successful). Usage:
@Rule
public static BrokerRunning brokerIsRunning = BrokerRunning.isRunning();
@Test
public void testSendAndReceive() throws Exception {
// ... test using RabbitTemplate etc.
}
The rule can be declared as static so that it only has to check once for all tests in
the enclosing test case, but there isn't a lot of overhead in making it non-static.
Use isRunningWithEmptyQueues(String...) to declare and/or purge test queue(s)
when the rule is run.
Call removeTestQueues(String...) from an @After method to remove
those queues (and optionally others).
If you wish to enforce the broker being available, for example, on a CI server,
set the environment variable "RABBITMQ_SERVER_REQUIRED" to
true and the tests will fail fast.
- Since:
- 1.7
- Author:
- Dave Syer, Gary Russell, Artem Bilan
-
Method Summary
Modifier and TypeMethodDescriptionorg.junit.runners.model.Statementapply(org.junit.runners.model.Statement base, org.junit.runner.Description description) static voidClear any environment variable overrides set insetEnvironmentVariableOverrides(Map).voiddeleteExchanges(String... exchanges) Delete arbitrary exchanges from the broker.voiddeleteQueues(String... queuesToDelete) Delete arbitrary queues from the broker.static booleanfatal()Generate the connection id for the connection used by the rule's connection factory.Return the admin password.Return the admin uri.Return the admin user.com.rabbitmq.client.ConnectionFactoryGet the connection factory used by this rule.Return the port.Return the password.intgetPort()Return the port.getUser()Return the user.static BrokerRunningstatic BrokerRunningstatic BrokerRunningstatic BrokerRunningstatic BrokerRunningisRunningWithEmptyQueues(String... names) Ensure the broker is running and has a empty queue(s) with the specified name(s) in the default exchange.voidisUp()voidDelete and re-declare all the configured queues.voidremoveTestQueues(String... additionalQueues) Remove any test queues that were created by anisRunningWithEmptyQueues(String...)method.voidsetAdminPassword(String password) Set the password for the management REST API connection default "guest".voidsetAdminUri(String adminUri) Set the uri for the REST API.voidsetAdminUser(String user) Set the user for the management REST API connection default "guest".static voidsetEnvironmentVariableOverrides(Map<String, String> environmentVariables) Set environment variable overrides for host, port etc.voidsetHostName(String hostName) voidsetPassword(String password) Set the password for the amqp connection default "guest".voidsetPort(int port) voidSet the user for the amqp connection default "guest".Methods inherited from class org.junit.rules.TestWatcher
failed, finished, skipped, skipped, starting, succeeded
-
Method Details
-
setEnvironmentVariableOverrides
Set environment variable overrides for host, port etc. Will override any real environment variables, if present.The variables will only apply to rule instances that are created after this method is called. The overrides will remain until
clearEnvironmentVariableOverrides()is called.- Parameters:
environmentVariables- the variables.
-
clearEnvironmentVariableOverrides
public static void clearEnvironmentVariableOverrides()Clear any environment variable overrides set insetEnvironmentVariableOverrides(Map). -
isRunningWithEmptyQueues
Ensure the broker is running and has a empty queue(s) with the specified name(s) in the default exchange.- Parameters:
names- the queues to declare for the test.- Returns:
- a new rule that assumes an existing running broker
-
isRunning
- Returns:
- a new rule that assumes an existing running broker
-
isNotRunning
- Returns:
- a new rule that assumes there is no existing broker
-
isBrokerAndManagementRunning
- Returns:
- a new rule that assumes an existing broker with the management plugin
-
isBrokerAndManagementRunningWithEmptyQueues
- Parameters:
queues- the queues.- Returns:
- a new rule that assumes an existing broker with the management plugin with the provided queues declared (and emptied if needed)..
-
setPort
public void setPort(int port) - Parameters:
port- the port to set
-
setHostName
- Parameters:
hostName- the hostName to set
-
setUser
Set the user for the amqp connection default "guest".- Parameters:
user- the user.- Since:
- 1.7.2
-
setPassword
Set the password for the amqp connection default "guest".- Parameters:
password- the password.- Since:
- 1.7.2
-
setAdminUri
Set the uri for the REST API.- Parameters:
adminUri- the uri.- Since:
- 1.7.2
-
setAdminUser
Set the user for the management REST API connection default "guest".- Parameters:
user- the user.- Since:
- 1.7.2
-
setAdminPassword
Set the password for the management REST API connection default "guest".- Parameters:
password- the password.- Since:
- 1.7.2
-
getPort
public int getPort()Return the port.- Returns:
- the port.
- Since:
- 1.7.2
-
getHostName
Return the port.- Returns:
- the port.
- Since:
- 1.7.2
-
getUser
Return the user.- Returns:
- the user.
- Since:
- 1.7.2
-
getPassword
Return the password.- Returns:
- the password.
- Since:
- 1.7.2
-
getAdminUser
Return the admin user.- Returns:
- the user.
- Since:
- 1.7.2
-
getAdminPassword
Return the admin password.- Returns:
- the password.
- Since:
- 1.7.2
-
apply
public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement base, org.junit.runner.Description description) - Specified by:
applyin interfaceorg.junit.rules.TestRule- Overrides:
applyin classorg.junit.rules.TestWatcher
-
isUp
public void isUp() -
fatal
public static boolean fatal() -
generateId
Generate the connection id for the connection used by the rule's connection factory.- Returns:
- the id.
-
removeTestQueues
Remove any test queues that were created by anisRunningWithEmptyQueues(String...)method.- Parameters:
additionalQueues- additional queues to remove that might have been created by tests.
-
purgeTestQueues
public void purgeTestQueues()Delete and re-declare all the configured queues. Can be used between tests when a test might leave stale data and multiple tests use the same queue. -
deleteQueues
Delete arbitrary queues from the broker.- Parameters:
queuesToDelete- the queues to delete.
-
deleteExchanges
Delete arbitrary exchanges from the broker.- Parameters:
exchanges- the exchanges to delete.
-
getConnectionFactory
public com.rabbitmq.client.ConnectionFactory getConnectionFactory()Get the connection factory used by this rule.- Returns:
- the connection factory.
-
getAdminUri
Return the admin uri.- Returns:
- the uri.
- Since:
- 1.7.2
-