Spring JmsTemplate 的启动 2.0 集成测试
Spring Boot 2.0 Integration Test for JmsTemplate
我有一个非常简单的 MessageHandler class,感谢 Spring Boot 在运行时仅需以下几行代码和配置即可工作:
消息处理程序
@Component
public class MessageHandler {
@JmsListener(destination = "${solace.jms.queueName}")
public void processMsg(Message msg) {
MessageHeaders hdrs = msg.getHeaders();
etc...
主要class
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
配置
solace.jms.client-name=eeeee
solace.jms.client-password=dddd
solace.jms.client-username=ccccc
solace.jms.msg-vpn=bbbb
solace.jms.host=smf://aaaaa.xx.yy.com:8000
solace.jms.queueName=ffffff
完整 pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>blah</groupId>
<artifactId>blahblah</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>blah.Application</start-class>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<cucumber.version>2.3.1</cucumber.version>
</properties>
<dependencies>
<dependency>
<groupId>com.solace.spring.boot</groupId>
<artifactId>solace-jms-spring-boot-parent</artifactId>
<version>2.0.0</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.solace.spring.boot</groupId>
<artifactId>solace-jms-spring-boot-starter</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
<dependency>
<groupId>com.sybase</groupId>
<artifactId>jconn4</artifactId>
<version>16</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.0.5.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.5.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>android-json</artifactId>
<groupId>com.vaadin.external.google</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.5.RELEASE</version>
<configuration>
<fork>true</fork>
<skip>false</skip>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我现在要找的是如何在编译时测试它。我的具体问题是如何将 JmsTemplate 自动装配为模拟实例,以及我应该在测试应用程序属性中使用哪些配置值。
谢谢
您可以使用嵌入式 ActiveMQ 实例代理进行测试。
When ActiveMQ is available on the classpath, Spring Boot can also configure a ConnectionFactory. If the broker is present, an embedded broker is automatically started and configured (provided no broker URL is specified through configuration).
查看 JMS mock tests,它们是 Spring 集成的高级测试示例的一部分。请注意,这与集成测试(尽管名称不同)不同,集成测试涉及连接到实际代理。模拟测试不涉及任何代理(外部或嵌入式),包括有效和无效消息测试。
作为替代方案,您可以使用 Spring Cloud Stream 来处理消息。这是构建事件驱动 Spring 启动微服务的更简单/更新的方法,并且避免了必须编写样板消息传递代码,例如在使用 JmsTemplate
时。有关详细信息,请参阅 binder available for the Solace broker. Moreover, you can also mock a MessageHandler
and test in a manner similar to the Spring Integration samples noted earlier. See this article。
我有一个非常简单的 MessageHandler class,感谢 Spring Boot 在运行时仅需以下几行代码和配置即可工作:
消息处理程序
@Component
public class MessageHandler {
@JmsListener(destination = "${solace.jms.queueName}")
public void processMsg(Message msg) {
MessageHeaders hdrs = msg.getHeaders();
etc...
主要class
@SpringBootApplication
public class Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
配置
solace.jms.client-name=eeeee
solace.jms.client-password=dddd
solace.jms.client-username=ccccc
solace.jms.msg-vpn=bbbb
solace.jms.host=smf://aaaaa.xx.yy.com:8000
solace.jms.queueName=ffffff
完整 pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>blah</groupId>
<artifactId>blahblah</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>blah.Application</start-class>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<cucumber.version>2.3.1</cucumber.version>
</properties>
<dependencies>
<dependency>
<groupId>com.solace.spring.boot</groupId>
<artifactId>solace-jms-spring-boot-parent</artifactId>
<version>2.0.0</version>
<type>pom</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.5.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.solace.spring.boot</groupId>
<artifactId>solace-jms-spring-boot-starter</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
<dependency>
<groupId>com.sybase</groupId>
<artifactId>jconn4</artifactId>
<version>16</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.0.5.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.0.5.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>android-json</artifactId>
<groupId>com.vaadin.external.google</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.5.RELEASE</version>
<configuration>
<fork>true</fork>
<skip>false</skip>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
我现在要找的是如何在编译时测试它。我的具体问题是如何将 JmsTemplate 自动装配为模拟实例,以及我应该在测试应用程序属性中使用哪些配置值。
谢谢
您可以使用嵌入式 ActiveMQ 实例代理进行测试。
When ActiveMQ is available on the classpath, Spring Boot can also configure a ConnectionFactory. If the broker is present, an embedded broker is automatically started and configured (provided no broker URL is specified through configuration).
查看 JMS mock tests,它们是 Spring 集成的高级测试示例的一部分。请注意,这与集成测试(尽管名称不同)不同,集成测试涉及连接到实际代理。模拟测试不涉及任何代理(外部或嵌入式),包括有效和无效消息测试。
作为替代方案,您可以使用 Spring Cloud Stream 来处理消息。这是构建事件驱动 Spring 启动微服务的更简单/更新的方法,并且避免了必须编写样板消息传递代码,例如在使用 JmsTemplate
时。有关详细信息,请参阅 binder available for the Solace broker. Moreover, you can also mock a MessageHandler
and test in a manner similar to the Spring Integration samples noted earlier. See this article。