Java 发送 JMS 消息的单元测试代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/335621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unit testing code that sends JMS messages
提问by bmw0128
I have a class that after it does some stuff, sends a JMS message.
I'd like to unit test the "stuff", but not necessarily the sending of the message.
When I run my test, the "stuff" green bars, but then fails when sending the message (it should, the app server is not running).
What is the best way to do this, is it to mock the message queue, if so, how is that done.
I am using Spring, and "jmsTemplate" is injected, along with "queue".
我有一个类,它在做一些事情之后,会发送一条 JMS 消息。我想对“东西”进行单元测试,但不一定是消息的发送。
当我运行我的测试时,“东西”绿色条,但在发送消息时失败(它应该,应用程序服务器没有运行)。做到这一点的最佳方法是模拟消息队列,如果是这样,那是如何完成的。
我正在使用 Spring,并且注入了“jmsTemplate”和“queue”。
采纳答案by Alex B
The simplest answer I would use is to stub out the message sending functionality. For example, if you have this:
我会使用的最简单的答案是删除消息发送功能。例如,如果你有这个:
public class SomeClass {
public void doit() {
//do some stuff
sendMessage( /*some parameters*/);
}
public void sendMessage( /*some parameters*/ ) {
//jms stuff
}
}
Then I would write a test that obscures the sendMessage behavior. For example:
然后我会编写一个测试来掩盖 sendMessage 行为。例如:
@Test
public void testRealWorkWithoutSendingMessage() {
SomeClass thing = new SomeClass() {
@Override
public void sendMessage( /*some parameters*/ ) { /*do nothing*/ }
}
thing.doit();
assertThat( "Good stuff happened", x, is( y ) );
}
If the amount of code that is stubbed out or obscured is substantial, I would not use an anonymousinner class but just a "normal" inner class.
如果被剔除或隐藏的代码量很大,我不会使用匿名内部类,而只会使用“普通”内部类。
回答by tunaranch
You can inject a mocked jmsTemplate.
你可以注入一个模拟的 jmsTemplate。
Assuming easymock, something like
假设easymock,类似
JmsTemplate mockTemplate = createMock(JmsTemplate.class)
That would do the trick.
这样就行了。
回答by Aaron Digulla
Another option is MockRunnerwhich provides mock environments for JDBC, JMS, JSP, JCA and EJB. This allows you to define the queues/topics just like you would in the "real" case and simply send the message.
另一种选择是MockRunner,它为 JDBC、JMS、JSP、JCA 和 EJB 提供模拟环境。这允许您像在“真实”情况下一样定义队列/主题,然后简单地发送消息。
回答by Ichthyo
Regarding how to organise all those test stubbing / mocks in a larger application...
关于如何在更大的应用程序中组织所有这些测试存根/模拟......
We build and maintain a larger Enterprise App, which is configured with Spring. The real App runs as EAR on a JBoss Appserver. We defined our Spring context(s) with a beanRefFactory.xml
我们构建和维护了一个更大的企业应用程序,它配置了 Spring。真正的应用程序在 JBoss Appserver 上作为 EAR 运行。我们用 beanRefFactory.xml 定义了我们的 Spring 上下文
<bean id="TheMegaContext"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>BasicServices.xml</value>
<value>DataAccessBeans.xml</value>
<value>LoginBeans.xml</value>
<value>BussinessServices.xml</value>
....
</list>
</constructor-arg>
</bean>
For running the unit tests, we just use a different beanRefFactory.xml, which exchanges the BasicServices to use a test version. Within that test version, we can define beans with the same names as in the production version, but with a mock/stub or whatever implementation (e.g. database uses a local Apache DPCP pooled datasource, while the production version uses the data source from the Appserver).
为了运行单元测试,我们只使用不同的 beanRefFactory.xml,它交换 BasicServices 以使用测试版本。在该测试版本中,我们可以定义与生产版本名称相同的 bean,但使用模拟/存根或任何实现(例如,数据库使用本地 Apache DPCP 池化数据源,而生产版本使用来自 Appserver 的数据源)。
回答by Kumar Manish
This is the perfect candidate to use jMock unit testing since your server is not running but you would use jMock to simulate interaction with the server.
这是使用 jMock 单元测试的完美候选者,因为您的服务器没有运行,但您将使用 jMock 来模拟与服务器的交互。