依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.0.RELEASE</version>
</dependency>
@ContextConfiguration(locations = {“classpath:applicationContext.xml”})这一行不能缺少,用来指明spring的配置文件的位置的。
加上这些注解,就和正常使用一样,自动注入相关BEAN进行测试了。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class Test1{
@Autowired
RabbitMQHelper rabbitMQHelper;
@Autowired
UdConsumerContainer udConsumerContainer;
@Test
public void test(){
QueueMQ queueMQ=new QueueMQ("logMQ",true,false,false,null);
rabbitMQHelper.declareQueue(queueMQ);
AMQP.BasicProperties.Builder basicProperties=new AMQP.BasicProperties().builder();
basicProperties.contentType("text/json");
basicProperties.contentEncoding("utf-8");
rabbitMQHelper.send("",queueMQ.getName(),true, basicProperties.build(),"好啊");
rabbitMQHelper.closeChannel();
System.out.println("");
}
@Test
public void consumer() throws Exception {
udConsumerContainer.setQueueNames("logMQ");
udConsumerContainer.setMessageHandler((o)->{
try {
System.out.println(o);
} catch (Exception e) {
e.printStackTrace();
}
return true;
});
udConsumerContainer.start();
Thread.sleep(500000);
}
}