【问题标题】:I want to verify that my camel consumer app is throwing specific exception via junit test我想验证我的骆驼消费者应用程序是否通过 junit 测试抛出特定异常
【发布时间】:2020-07-07 21:54:55
【问题描述】:
 // custom exception

        public class customexception () {

        log.error("exception);
        }

    --- apache camel consumer app which has custom exception

        public consumer app implements Processor {

        //process & validate

    @override
    public process(message) {

        if(condition met)
             throw customexception();

        }
       }

        // my junit test class

        @runwith(CamelSpringBootRunner)
        @springboottest
        public mytestclass {


        @Autowired
        ProducerTemplate producer;

        @Test
        public testexception () {

        producer.sendbody("invalid message");

        //??? assert customexception is thrown by consumer

        }
        }

这里 Junit 的 expectedCondition 将不起作用,因为我在测试中没有遇到异常,但是正在侦听我的测试产生的消息的独立消费者应用程序正在引发异常。

【问题讨论】:

  • junit4? junit5?另外,您已经尝试过什么?请给我们看。
  • junit4.我添加了可能有用的代码片段

标签: java spring-boot exception junit apache-camel


【解决方案1】:

...我的测试没有抛出异常,但是正在侦听我的测试产生的消息的消费者应用程序正在抛出异常。

JUnit 无法拦截未在当前堆栈中抛出的异常,或未冒泡到测试中的异常。

听起来你在错误的地方进行单元测试,或者在错误的粒度级别。

行为(抛出异常)不是您正在测试的类的行为。这是消费者应用程序的一种行为。所以抛出异常的测试应该在消费者应用的单元测试中。

  • 这个类(生产者)的单元测试应该是它产生的消息类型会/应该产生异常。它可能应该与消费者应用程序的“模拟”对话,该应用程序捕获消息以供您的测试检查。

  • 消费者应用程序的单元测试同样应该使用来自“模拟”生产者的消息。该测试用于检查是否引发了适当的异常。

  • 您也可以在集成测试级别对此进行测试,但随后您可能会测试消费者应用程序是否(例如)在应导致此行为的场景中在其日志文件中记录错误。或者也许您不需要对此进行测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-30
    • 2015-08-27
    • 2023-03-03
    • 2013-03-30
    • 1970-01-01
    • 2013-06-03
    • 2013-05-23
    • 1970-01-01
    相关资源
    最近更新 更多