Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Java 以驼峰路由从模拟Spring WS服务发送SOAP错误_Java_Unit Testing_Soap_Apache Camel_Spring Ws - Fatal编程技术网

Java 以驼峰路由从模拟Spring WS服务发送SOAP错误

Java 以驼峰路由从模拟Spring WS服务发送SOAP错误,java,unit-testing,soap,apache-camel,spring-ws,Java,Unit Testing,Soap,Apache Camel,Spring Ws,在系统中,有一个驼峰路由将一些输入传递给外部web服务 @Component public class MyRouteBuilder extends RouteBuilder { public void configure() { errorHandler(deadLetterChannel("direct:error").disableRedelivery()); // further routes for business logic omitte

在系统中,有一个驼峰路由将一些输入传递给外部web服务

@Component
public class MyRouteBuilder extends RouteBuilder {


    public void configure() {
        errorHandler(deadLetterChannel("direct:error").disableRedelivery());
        // further routes for business logic omitted here

        from("direct:frontendService")
        .transform()
        .simple("<urn:myPayload>...</urn:myPayload>")
        .to("spring-ws:http://external-service.com")
        .end();
    }
}
因此,我想实现一些测试用例来模拟外部服务的故障响应。我的方法是模拟对实际服务的调用,并将其替换为模拟响应:

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = CamelSpringDelegatingTestContextLoader.class, classes = { MyRouteBuilderTest.TestConfig.class,
MyRouteBuilder.class })
@MockEndpointsAndSkip(value = "spring-ws:*")
public class MyRouteBuilderTest {

  @EndpointInject(uri = "mock:error")
  protected MockEndpoint errorEndpoint;

  @EndpointInject(uri = "mock:spring-ws:http://external-service.com")
  protected MockEndpoint service;

  @Produce(uri = "direct:frontendService")
  protected ProducerTemplate frontendServiceProducer;

  @Configuration
  @PropertySource("classpath:application.properties")
  public static class TestConfig extends SingleRouteCamelConfiguration {
    @Bean
    @Override
    public RouteBuilder route() {
      return new MyRouteBuilder() {
        public void configure() throws Exception {
          super.configure();
          from("direct:error").to("mock:error");
        };

      };
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer configurer() {
      return new PropertySourcesPlaceholderConfigurer();
    }
  }

  @Test
  public void test() throws InterruptedException {

    errorEndpoint.expectedMessageCount(1);

    service.whenAnyExchangeReceived(new Processor() {

      @Override
      public void process(Exchange arg0) throws Exception {
        // How do I send some fault message here in a way that the
        // route behaves like if it was thrown from the real service
      }
    });

    Object o = frontendServiceProducer.requestBody("<some>payload</some>");


    service.expectedMessageCount(0);

    service.assertIsSatisfied();
    errorEndpoint.assertIsSatisfied();
  } 
}
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=CamelSpringDelegatingTestContextLoader.class,classes={MyRouteBuilderTest.TestConfig.class,
MyRouteBuilder.class})
@MockEndpointsAndSkip(value=“spring ws:*”)
公共类MyRouteBuilderTest{
@端点注入(uri=“mock:error”)
受保护的模拟端点错误端点;
@端点注入(uri=“mock:spring-ws:http://external-service.com")
受保护的终端服务;
@制作(uri=“直接:前端服务”)
受保护的生产者从事前端服务生产者;
@配置
@PropertySource(“类路径:application.properties”)
公共静态类TestConfig扩展了SingleRouteCamelConfiguration{
@豆子
@凌驾
公共路由生成器路由(){
返回新的MyRouteBuilder(){
public void configure()引发异常{
super.configure();
从(“直接:错误”)到(“模拟:错误”);
};
};
}
@豆子
公共静态属性资源占位符配置器配置器(){
返回新属性资源占位符配置器();
}
}
@试验
public void test()引发InterruptedException{
errorEndpoint.expectedMessageCount(1);
service.whenAnyExchangeReceived(新处理器(){
@凌驾
公共作废进程(Exchange arg0)引发异常{
//如何在此处以
//路由的行为类似于从实际服务中抛出
}
});
对象o=frontendServiceProducer.requestBody(“有效负载”);
服务。expectedMessageCount(0);
service.assertessatified();
errorEndpoint.Assertessatified();
} 
}
我知道我可以在我的定制处理器中抛出一些异常,但这不是完全相同的行为,特别是没有soap错误细节,这对于进一步的评估很重要

有没有建议如何将模拟SOAP错误发送到作为模拟服务的响应,或者还有其他方法

@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = CamelSpringDelegatingTestContextLoader.class, classes = { MyRouteBuilderTest.TestConfig.class,
MyRouteBuilder.class })
@MockEndpointsAndSkip(value = "spring-ws:*")
public class MyRouteBuilderTest {

  @EndpointInject(uri = "mock:error")
  protected MockEndpoint errorEndpoint;

  @EndpointInject(uri = "mock:spring-ws:http://external-service.com")
  protected MockEndpoint service;

  @Produce(uri = "direct:frontendService")
  protected ProducerTemplate frontendServiceProducer;

  @Configuration
  @PropertySource("classpath:application.properties")
  public static class TestConfig extends SingleRouteCamelConfiguration {
    @Bean
    @Override
    public RouteBuilder route() {
      return new MyRouteBuilder() {
        public void configure() throws Exception {
          super.configure();
          from("direct:error").to("mock:error");
        };

      };
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer configurer() {
      return new PropertySourcesPlaceholderConfigurer();
    }
  }

  @Test
  public void test() throws InterruptedException {

    errorEndpoint.expectedMessageCount(1);

    service.whenAnyExchangeReceived(new Processor() {

      @Override
      public void process(Exchange arg0) throws Exception {
        // How do I send some fault message here in a way that the
        // route behaves like if it was thrown from the real service
      }
    });

    Object o = frontendServiceProducer.requestBody("<some>payload</some>");


    service.expectedMessageCount(0);

    service.assertIsSatisfied();
    errorEndpoint.assertIsSatisfied();
  } 
}