9

我知道 RabbitMQ 是用 Erlang 编写的,因此不能像我们对 ActiveMQ JMS 代理所做的那样嵌入到 JVM 中。

但实际上有些项目是用另一种语言完成的,可以很容易地嵌入到集成测试中。

例如,用 C++ 编写的 MongoDB 可以在 JVM 集成测试的上下文中轻松启动/停止: https ://github.com/flapdoodle-oss/embedmongo.flapdoodle.de

还有人将其移植到 Java: https ://github.com/thiloplanz/jmockmongo/


所以我只是想知道当我的应用程序是用 Java 编写的,而另一种技术是另一种语言(比如用于 RabbitMQ 的 Erlang)时,我们如何进行集成测试?

一般来说,有哪些好的做法?

我看到 3 个主要解决方案:

  • 启动一个真正的 RabbitMQ
  • 在当前使用的语言中嵌入技术的 JVM 端口
  • 使用标准技术,以便 Erlang 中的一种技术可能具有与 Java 中的另一种技术相同的行为和通信层(RabbitMQ / Qpid / StormMQ 实现 AMQP)

是否有用于启动临时 RabbitMQ 代理的 Maven/Sbt/Ant 插件?在测试课之前有任何支持 Junit/TestNG RabbitMQ 的项目吗?

我已经看到在 Java 中有一个 AMQP 的开源实现:Apache Qpid 在生产中有 RabbitMQ 时,有人使用这个实现进行集成测试吗?甚至可能吗?我正在使用 Spring 集成。


顺便说一句,我刚刚注意到 Spring-AMQP 项目在其 github 自述文件中提到:

Many of the "integration" tests here require a running RabbitMQ server - they will be skipped if the broker is not detected.

4

5 回答 5

3

If it were me, I would look at mocking the stack component in question. What's the best mock framework for Java? (although not a great Stack Overflow question) and Mock Object might help get you going.

Mocking the components makes testing much easier (IMHO) than trying to "live test" everything.

于 2013-04-19T19:34:44.717 回答
3

In the past I have used a virtual machine that had a fresh install of RabbitMQ running. Developers would run it constantly and our CI server would start a new VM for each revision of the code. Our tests would fail if they could not connect to the server instead of skipping the tests as a lack of integration tests is a serious problem.

This tends to work reasonably well and prevents needing to start and stop RabbitMQ for the tests. Our tests were split up to use vhosts for isolation and a few calls to create vhosts on demand so we could parallelize tests is we needed to.

于 2013-04-21T16:10:46.103 回答
2

I will tell you my opinion on the Apache QPid vs Rabbit MQ as I worked with both. My opinion is that they do 'extend' AMQP, but are very different. If in production you will have Rabbit MQ, then use the same one in tests - also the same version, patches, fixes, etc. There are things that Apache Qpid can do and Rabbit MQ can not and vice-versa.

Stating the obvious integration tests are done so that you could test the integration of the application. I would start an instance of Rabbit MQ.

于 2013-04-23T08:36:14.423 回答
2

Based on Philip Christiano's answer that says to use VM's, now we have Docker and I think it is the way to go to embed different technologies in docker containers.

One can start a docker container containing a RabbitMQ server, and it will be faster than using VM's because docker containers are lightweight and startup time is a lot better.

There are some maven plugins that permit to start docker containers, for exemple: http://www.alexecollins.com/content/docker-maven-plugin/

于 2014-07-23T19:26:00.937 回答
1

Since I was in your exact same situation and couldn't find a good solution, I developed it myself (inspired by Embedded-Redis, Embedded-Mongo, etc.)

This library is a wrapper around the process of downloading, extracting, starting and managing RabbitMQ so it can work like an embedded service controlled by any JVM project.

Check it out: https://github.com/AlejandroRivera/embedded-rabbitmq

It's as simple as:

EmbeddedRabbitMqConfig config = new EmbeddedRabbitMqConfig.Builder()
    .version(PredefinedVersion.V3_5_7)
    // ...
    .build();
EmbeddedRabbitMq rabbitMq = new EmbeddedRabbitMq(config);
rabbitMq.start();
...
rabbitMq.stop();

Works on Linux, Mac and Windows.

于 2016-09-12T14:14:49.987 回答