Source:
<code>public void test() { <START> CachingConnectionFactory ccf = new CachingConnectionFactory("localhost"); <END> RabbitTemplate template = new RabbitTemplate(ccf); RabbitAdmin admin = new RabbitAdmin(ccf); FanoutExchange fanout = new FanoutExchange("log4j2Test"); admin.declareExchange(fanout); Queue queue = new Queue("log4j2Test"); admin.declareQueue(queue); admin.declareBinding(BindingBuilder.bind(queue).to(fanout)); Logger logger = LogManager.getLogger("foo"); logger.info("foo"); logger.info("bar"); template.setReceiveTimeout(10000); Message received = template.receive(queue.getName()); assertThat(received).isNotNull(); assertThat(received.getMessageProperties().getReceivedRoutingKey()).isEqualTo("testAppId.foo.INFO"); Object foo = received.getMessageProperties().getHeader("foo"); assertThat(foo).isNotNull(); assertThat(foo).isInstanceOf(String.class); assertThat(foo).isEqualTo("bar"); assertThat(new String(received.getBody())).startsWith("foo"); received = template.receive(queue.getName()); assertThat(received).isNotNull(); assertThat(received.getMessageProperties().getReceivedRoutingKey()).isEqualTo("testAppId.foo.INFO"); assertThat(new String(received.getBody())).startsWith("bar"); Object threadName = received.getMessageProperties().getHeaders().get("thread"); assertThat(threadName).isNotNull(); assertThat(threadName).isInstanceOf(String.class); assertThat(threadName).isEqualTo(Thread.currentThread().getName()); foo = received.getMessageProperties().getHeaders().get("foo"); assertThat(foo).isNotNull(); assertThat(foo).isInstanceOf(String.class); assertThat(foo).isEqualTo("bar"); }</code><technical_language>mind destroy() this factory in end of test avoid resources leaking</technical_language>

Target:
public void test() { CachingConnectionFactory ccf = new CachingConnectionFactory("localhost"); RabbitTemplate template = new RabbitTemplate(ccf); RabbitAdmin admin = new RabbitAdmin(ccf); FanoutExchange fanout = new FanoutExchange("log4j2Test"); admin.declareExchange(fanout); Queue queue = new Queue("log4j2Test"); admin.declareQueue(queue); admin.declareBinding(BindingBuilder.bind(queue).to(fanout)); Logger logger = LogManager.getLogger("foo"); logger.info("foo"); logger.info("bar"); template.setReceiveTimeout(10000); Message received = template.receive(queue.getName()); assertThat(received).isNotNull(); assertThat(received.getMessageProperties().getReceivedRoutingKey()).isEqualTo("testAppId.foo.INFO"); Object foo = received.getMessageProperties().getHeader("foo"); assertThat(foo).isNotNull(); assertThat(foo).isInstanceOf(String.class); assertThat(foo).isEqualTo("bar"); assertThat(new String(received.getBody())).startsWith("foo"); received = template.receive(queue.getName()); assertThat(received).isNotNull(); assertThat(received.getMessageProperties().getReceivedRoutingKey()).isEqualTo("testAppId.foo.INFO"); assertThat(new String(received.getBody())).startsWith("bar"); Object threadName = received.getMessageProperties().getHeaders().get("thread"); assertThat(threadName).isNotNull(); assertThat(threadName).isInstanceOf(String.class); assertThat(threadName).isEqualTo(Thread.currentThread().getName()); foo = received.getMessageProperties().getHeaders().get("foo"); assertThat(foo).isNotNull(); assertThat(foo).isInstanceOf(String.class); assertThat(foo).isEqualTo("bar"); ccf.destroy(); }