当前位置: 首页 > 知识库问答 >
问题:

带有Spring Boot单元测试设置的Apache Camel

燕宜修
2023-03-14
public class SampleRouter1 extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("direct:start") // a real entry will be jms:queue:testqueue
            .log("Direct Start Init")
            .process(new SampleProcessor1())
            .to("mock:output");
    }
}
@RunWith(CamelSpringBootRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@ContextConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("log:*")
@DisableJmx(false)
public class SampleRouter1Test1 {
    @Autowired
    protected CamelContext camelContext; // never gets wired in

运行单元测试时的例外情况是:类型为'org.apache.camel.CamelContext'的限定bean可用:需要至少有1个bean作为autowire候选对象。依赖性注释:{@org.springframework.beans.factory.annotation.autowire(required=true)}

还有一个问题是,这是为了测试SampleRouter,还是只是单元测试process类和任何其他支持类?还是使用以下内容来更改它,以便将消息传递到假的直接队列?

AdviceWith.adviceWith(camelContext, "jms:queue:testqueue", a -> { a.replaceFromWith("direct:start"); });

共有1个答案

马权
2023-03-14

因此,通过建议给我的链接,我得到了使用以下内容的单元测试:

Spring boot 2.4.2/Apache Camel 3.7.2

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-test-spring-junit5</artifactId>
            <version>3.7.2</version>
        </dependency>

我需要的单元测试注释:

@CamelSpringBootTest
@SpringBootTest // (webEnvironment = SpringBootTest.WebEnvironment.NONE)
@TestPropertySource
// TODO: Might need the dirty context here . I have not ran into that issue just yet. 
class SampleRouter1Test {

    @Autowired
    private CamelContext camelContext;

    @BeforeEach
    public void setUp() throws Exception {
//        This is to allow you to test JMS queues by replacing their JMS:Queue: entry
//        Also the "Test Router 1" is what ever you named your router via .id().
//        AdviceWith.adviceWith(camelContext, "Test Router 1", a -> {
//            a.replaceFromWith("direct:start2");
//        });

    }

    @Produce("direct:start")
    private ProducerTemplate template;

    @EndpointInject("mock:output")
    private MockEndpoint mockDone;

    @Test
    public void t1() throws Exception {
        template.sendBodyAndHeaders("Testing", new HashMap<>());

        mockDone.expectedMessageCount(1);
    }
}
@Component
public class SampleRouter1 extends RouteBuilder {

    @Autowired
    private SampleProcessor1 sampleProcessor1;

    @Override
    public void configure() throws Exception {
        from("direct:start")
                .id("Test Router 1")
                .log("Direct Start Init")
                .process(sampleProcessor1)
                .to("mock:output");
    }

}
 类似资料:
  • 需要对项目的控制器部分进行单元测试,但却得到了错误。我相信ModelAndVIew部分导致了这个问题,尽管我曾经嘲弄过它并返回ModelAndVIew,因为它是方法的返回类型。然而,它并不起作用。pom.xml没有任何问题,因此没有添加它。ProjectController: java.lang.IllegalStateException:找不到@SpringBootConfiguration,您

  • 我正在使用类似的@Input to Angular2@Input to带有get/set的属性。不过,我在弄清楚如何设置单元测试时遇到了一些麻烦。这是我的组件 在我定义组件的每个位置之前都有一个附加的 在我运行这个简单的测试之前 我收到错误 我最初的想法是创建一个模拟顺序常量,并在每个这样的常量之前,将它赋给我的组件... 但是,我不被允许做这个任务 我的下一个假设是,我必须模拟父组件Orders

  • src/cmakelists.txt: src/sqr.h src/sqr.cpp null

  • 我已经创建了一个应用程序使用Springboot和Hibernate,我想配置它的单元测试。 首先,这是DAO接口。 这是DAO接口的实现 然后我创建了一个测试类,如下所示 我已经将application.properties文件放在test和src目录的资源中。 我尝试运行此单元测试用例,但由于以下错误而失败: 那么我可以知道为DAO层配置单元测试的最佳方法吗?

  • 我有一个在命令行上运行的springboot 2应用程序。命令行参数之一是命名中带有batchNo的fileName。我正在使用命令行参数中的fileName设置我application.properties的fileName值。示例 在我的应用程序配置文件中,我像这样从applications.properties文件中读取文件名。 我的目标是能够为每个单独的测试动态设置这个文件名。 示例 如何

  • 我想测试我的SpringBoot应用程序,它使用cassandra作为CrudRepository。我最终得到了 具有 和 这就导致了 如果我使用旧版本的cassandra-unit-Spring 它以NullPointerException结束,因为没有注入值repo。 来源https://github.com/StephanPraetsch/spring.boot.cassandra.unit