java - Apache Camel Junit 保持 Context 运行

标签 java junit routes apache-camel jms

我们有一些 JUnit 用于测试 Camel 路由和业务逻辑,在某些时候需要运行 Camel 上下文,直到从服务器收到 JUnit 测试之一的响应响应。我们正在使用 CamelSpringTestSupport。问题是,此后没有其他 JUnit 运行。

下面是MyTest.java

import java.io.File;

import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.main.Main;
import org.apache.camel.test.spring.CamelSpringTestSupport;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class MyTest extends CamelSpringTestSupport{
    String prop1;   
    Main main;
    ActiveMQComponent jms;

    public void setUp() throws Exception {
        super.setUp();
        prop1 = context.resolvePropertyPlaceholders("{{prop1}}");        
        jms = (ActiveMQComponent) context.getComponent("jms");

        RouteBuilder builder = new RouteBuilder() {

            @Override
            public void configure() throws Exception {
                from("fromUri")
                .log("Got a message")               
                .to("file://C:\\Temp\\tempfolder");         
            }
        };        
        main = new Main();               
        main.addRouteBuilder(builder);
        main.bind("jms", jms);
        // add event listener
        //main.addMainListener(new Events());
        // set the properties from a file
        //main.setPropertyPlaceholderLocations("example.properties");
        // run until you terminate the JVM
        System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n");
        main.run();
    }

    @Override
    protected AbstractXmlApplicationContext createApplicationContext() {
        return new ClassPathXmlApplicationContext("/yourcontext.xml");
    }

    @Test
    public void testMoveFile() throws Exception {
        // create a new file in the inbox folder with the name hello.txt and containing Hello World as body
        template.sendBodyAndHeader("file://C:\\targetIn", "Hello World", Exchange.FILE_NAME, "hello.txt");

        // wait a while to let the file be moved
        Thread.sleep(2000);

        // test the file was moved
        File target = new File("C:\\targetin\\hello.txt");
        assertTrue("File should have been moved", target.exists());

        // test that its content is correct as well
        String content = context.getTypeConverter().convertTo(String.class, target);
        assertEquals("Hello World", content);
    }

    @Test
    public void uploadData() throws Exception{  
        File target = new File("c:\\input.xml");
        assertTrue("File should have been moved", target.exists());

        template.sendBody(toUri, target);
    }
}

yourcontext.xml 如下所示,我可能因为有两个上下文而犯了一个基本错误?

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:broker="http://activemq.apache.org/schema/core"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
         http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

    <context:component-scan base-package="com.yourpackage" />
    <bean id="properties" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
          <property name="ignoreResourceNotFound" value="true"/>
          <property name="locations">
            <list>
                <value>classpath:yourproperties.properties</value>                              
            </list>
        </property>
    </bean>


    <camel:camelContext id="mytest" useMDCLogging="true" threadNamePattern="#camelId#:#name#-##counter#">
        <camel:contextScan />
        <camel:jmxAgent id="agent" createConnector="false" />
        <camel:template id="camelTemplate" />
    </camel:camelContext>

    <bean id="shutdown" class="org.apache.camel.impl.DefaultShutdownStrategy">
        <property name="timeout" value="120" />
    </bean>

    <bean id="jms" class="org.apache.activemq.camel.component.ActiveMQComponent">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.spring.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://${activemq.host}:${activemq.port}" />
            </bean>
        </property>
    </bean>

</beans>

最佳答案

如果该代码片段是 JUnit 测试,我不太确定为什么要使用 @Component 对其进行注释。

无论如何,假设您使用 Spring,那么 JUnit 代码应该使用 spring 支持注释,如下所示:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@ContextConfiguration
public class MyCamelTest {
    @Autowired
    protected CamelContext camelContext;

完整样本:http://camel.apache.org/spring-testing.html

如果您使用 Spring Boot,那么这里是入门示例:

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest(classes = SpringBootApplication.class)
public class SpringBootCamelTest {

    @Autowired
    private CamelContext camelContext;

对于完整示例: https://github.com/apache/camel/blob/master/examples/camel-example-spring-boot/src/test/java/sample/camel/SampleCamelApplicationTest.java

关于java - Apache Camel Junit 保持 Context 运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45359340/

相关文章:

java - 图标与具有海玻璃外观和感觉的 JButton 不匹配

c# - webforms 中的输出缓存 varybyparams 是否理解路由参数?

ruby-on-rails - 错误 "No route matches"错误 Rails

routes - 如何使用参数和正则表达式创建 KOA 路由

java - 为什么 PrintStream 类引用在 java 的 System 类中声明为静态变量?

java - IndicesExistsResponse.isExists()不适用于嵌入式ElasticSearch实例

java - 在动画模拟中将逻辑与图形分离

java - 如何在 Junit 中测试 ScheduledExecutorService 异常处理?

java - 使用名称模拟身份验证

java - 在 Spring Boot 中跳过 VPN DNS 可用性的集成测试