Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing ApacheCamel-测试日志消息_Unit Testing_Logging_Apache Camel - Fatal编程技术网

Unit testing ApacheCamel-测试日志消息

Unit testing ApacheCamel-测试日志消息,unit-testing,logging,apache-camel,Unit Testing,Logging,Apache Camel,我正在尝试测试一个Camel路由,它使用从(x).to(y).log(“SuccessKey123”)和一个异常(HttpOperationFailedException.class).log(“ErrorKey123”) 当消息被成功处理时,如何测试Camel记录“SuccessKey123”,或者如果抛出HttpOperationFailedException,如何测试Camel记录“ErrorKey123” 我的RouteBuilder(): 测试类: public class myHtt

我正在尝试测试一个Camel路由,它使用
从(x).to(y).log(“SuccessKey123”)
一个异常(HttpOperationFailedException.class).log(“ErrorKey123”)

当消息被成功处理时,如何测试Camel记录“SuccessKey123”,或者如果抛出HttpOperationFailedException,如何测试Camel记录“ErrorKey123”

我的RouteBuilder():

测试类:

public class myHttp4RouteBuilderTest {

    @Produce(uri = MOCK_ROUTE_FROM)
    protected ProducerTemplate template;

    @EndpointInject(uri = MOCK_ROUTE_TO)
    private MockEndpoint mockEndpoint;

    @Autowired
    private CamelContext camelContext;

    @Before
    public void setup() throws Exception{
        RouteDefinition rd = camelContext.getRouteDefinition(myHttp4RouteBuilder.ID);
        rd.adviceWith(camelContext, new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {
                replaceFromWith(MOCK_ROUTE_FROM);

                weaveByToUri(ERROR_QUEUE)
                        .replace()
                        .to(MOCK_ROUTE_TO);
            }
        });
    }

    @Test
    @DirtiesContext
    public void testSuccess() throws Exception {
            // throw an HttpOperationFailedException
        mockEndpoint.whenAnyExchangeReceived(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                throw new HttpOperationFailedException("Exception", 400, null, null, null, null);
            }
        });


        //
        //
        // How can I test here that camel logs "ErrorKey123"
        //
        //


        template.sendBody(MOCK_ROUTE_FROM, "some content");

        mockEndpoint.assertIsSatisfied();

    }
}

多谢各位

Camel使用slf4j,因此您只需在安装时向所需的记录器添加一些测试追加器,并检查之后记录的内容(甚至是模拟追加器接口)

Camel使用slf4j,因此您只需在安装时向所需记录器添加一些测试追加器,并检查之后记录的内容(甚至是模拟追加器接口)

您还可以使用Camel的建议,然后使用mock等来模拟/替换这些日志端点,然后根据您的操作断言Camel将消息路由到这些端点


您还可以使用Camel的建议,然后使用mock等来模拟/替换这些日志端点,然后根据您的操作断言Camel将消息路由到这些端点

    • 我明白了;-)你让我走对了路。谢谢

      这是我的解决方案:

      第一:创建一个自定义附加器

      package de.example.test;
      
      import org.apache.logging.log4j.core.Filter;
      import org.apache.logging.log4j.core.Layout;
      import org.apache.logging.log4j.core.LogEvent;
      import org.apache.logging.log4j.core.appender.AbstractAppender;
      import org.apache.logging.log4j.core.appender.AppenderLoggingException;
      import org.apache.logging.log4j.core.config.plugins.Plugin;
      import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
      import org.apache.logging.log4j.core.config.plugins.PluginElement;
      import org.apache.logging.log4j.core.config.plugins.PluginFactory;
      import org.apache.logging.log4j.core.layout.PatternLayout;
      import org.slf4j.event.LoggingEvent;
      
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.List;
      
      
      @Plugin(name="myAppenderForTesting", category="Core", elementType="appender", printObject=true)
      public class MyAppenderForTesting extends AbstractAppender {
      
          /** Here we collect all log messages */
          public static List<LogEvent> logEvents = new ArrayList<>();
      
          protected MyAppenderForTesting(String name, Filter filter, Layout<? extends Serializable> layout, final boolean ignoreExceptions) {
              super(name, filter, layout, ignoreExceptions);
          }
          @PluginFactory
          public static MyAppenderForTesting createAppender(
                  @PluginAttribute("name") String name,
                  @PluginElement("Layout") Layout<? extends Serializable> layout,
                  @PluginElement("Filter") final Filter filter,
                  @PluginAttribute("otherAttribute") String otherAttribute) {
      
              return new MyAppenderForTesting(name, filter, layout, true);
      
          }
          @Override
          public void append(LogEvent event) {
              try {
                  logEvents.add(event);
              } catch (Exception ex) {
                  if (!ignoreExceptions()) {
                      throw new AppenderLoggingException(ex);
                  }
              } finally {
      
              }
          }
      
          /**
           * Clear log messages
           */
          public static void clean() {
              logEvents.clear();
          }
      }
      
      在我的测试类中,我可以直接访问
      myappenderforesting.logEvents
      。比如说

          for (LogEvent event : MyAppenderForTesting.logEvents) {
              String message = event.getMessage().toString();
              if (message.contains(search)) {
                  // do somethind
              }
          }
      
      我明白了;-)你让我走对了路。谢谢

      这是我的解决方案:

      第一:创建一个自定义附加器

      package de.example.test;
      
      import org.apache.logging.log4j.core.Filter;
      import org.apache.logging.log4j.core.Layout;
      import org.apache.logging.log4j.core.LogEvent;
      import org.apache.logging.log4j.core.appender.AbstractAppender;
      import org.apache.logging.log4j.core.appender.AppenderLoggingException;
      import org.apache.logging.log4j.core.config.plugins.Plugin;
      import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
      import org.apache.logging.log4j.core.config.plugins.PluginElement;
      import org.apache.logging.log4j.core.config.plugins.PluginFactory;
      import org.apache.logging.log4j.core.layout.PatternLayout;
      import org.slf4j.event.LoggingEvent;
      
      import java.io.Serializable;
      import java.util.ArrayList;
      import java.util.List;
      
      
      @Plugin(name="myAppenderForTesting", category="Core", elementType="appender", printObject=true)
      public class MyAppenderForTesting extends AbstractAppender {
      
          /** Here we collect all log messages */
          public static List<LogEvent> logEvents = new ArrayList<>();
      
          protected MyAppenderForTesting(String name, Filter filter, Layout<? extends Serializable> layout, final boolean ignoreExceptions) {
              super(name, filter, layout, ignoreExceptions);
          }
          @PluginFactory
          public static MyAppenderForTesting createAppender(
                  @PluginAttribute("name") String name,
                  @PluginElement("Layout") Layout<? extends Serializable> layout,
                  @PluginElement("Filter") final Filter filter,
                  @PluginAttribute("otherAttribute") String otherAttribute) {
      
              return new MyAppenderForTesting(name, filter, layout, true);
      
          }
          @Override
          public void append(LogEvent event) {
              try {
                  logEvents.add(event);
              } catch (Exception ex) {
                  if (!ignoreExceptions()) {
                      throw new AppenderLoggingException(ex);
                  }
              } finally {
      
              }
          }
      
          /**
           * Clear log messages
           */
          public static void clean() {
              logEvents.clear();
          }
      }
      
      在我的测试类中,我可以直接访问
      myappenderforesting.logEvents
      。比如说

          for (LogEvent event : MyAppenderForTesting.logEvents) {
              String message = event.getMessage().toString();
              if (message.contains(search)) {
                  // do somethind
              }
          }
      

      也可以看到这个答案:也可以看到这个答案:嗯,我的目标是测试日志消息,这样我就可以对它进行grep。这对于监控是必要的,所以我必须编写一个测试来记录某条消息。嗯,我的目标是测试日志消息,以便我可以对其进行grep。这对于监控是必要的,因此我必须编写一个测试,以记录某条消息。