无法用骆驼路线中的模拟替换spring bean

时间:2016-12-14 13:51:06

标签: spring-boot apache-camel

使用@Profile我可以模拟spring bean,但是在camel路由中没有调用mock bean方法。我正在使用SpringJUnit4ClassRunner.class并使用@ActiveProfile 下面是我想在单元测试中使用我的模拟bean替换,cancelSubscriptionTransformer,myBeanClient,extendedClient bean的路径。

from("{{cancelSubscriptionFromRMQUri}}").routeId("cancelSubscriptionRoute")
        .unmarshal().json(JsonLibrary.Jackson, Subscription.class)
            .bean("cancelSubscriptionTransformer", "toKbCancelSubscription")
            .choice()
            .when().simple("${body.serviceType} == 'subscriptions'")
            .bean("myBeanClient", "cancelSubscription(${body.subscriptionId}, ${body.createdBy}, ${body.reason}, ${body.comment})")
            .bean("extendedClient", "retrieveSubscription(${body.subscriptionId}, ${body.externalKey})")
            .marshal(json)
            .to("{{cancelSubscriptionTORMQUri}}")
            .when().simple("${body.serviceType} == 'usage'")
            .bean("myBeanClient", "cancelSubscription(${body.subscriptionId}, ${body.dateTime},null, null, -1, ${body.createdBy}, ${body.reason}," +
                    " ${body.comment})")
            .endChoice();

下面是我如何定义我的ExtendedClientMock,我对其余的模拟bean使用相同的方法

@Profile("test")
@Primary
@Repository
public class ExtendedClientMock  extends ExtendedClient {

public Subscription retrieveSubscription(UUID subscriptionid, String sdpSubscriptionId) throws MyClientException {
    Subscription subs=new Subscription();
    subs.setProductName("test");
    return subs;
}
}

以下是单元测试的代码:

  @ActiveProfiles({"test", "aop"})
  @AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
  @RunWith(SpringJUnit4ClassRunner.class)
  @SpringBootTest(classes = CancelSubscriptionRouteTest.class)
  @EnableAutoConfiguration
  @ComponentScan
  @ContextConfiguration(classes = { BillingServicesApplication.class })
  @UseAdviceWith

  public class CancelSubscriptionRouteTest {

  @Autowired
  protected CamelContext camelContext;

  @Autowired
  private CancelSubscriptionTransformer cancelSubscriptionTransformer;

  @Autowired
  private ExtendedClient extendedClient;

  @Autowired
  private MyBeanClient myBeanClient;

  @EndpointInject(uri = "{{cancelSubscriptionTORMQUri}}")
  private MockEndpoint cancelSubscriptionTORMQUriEndpoint;

  @EndpointInject(uri = "{{cancelSubscriptionFromRMQUri}}")
  private ProducerTemplate cancelSubscriptionFromRMQUriEndpoint;

  @Inject
  private ObjectMapperContextResolver objectMapperContextResolver;

  @Test
  @DirtiesContext
  public void testCancelSubscriptionRoute() throws Exception {
  cancelSubscriptionTORMQUriEndpoint.expectedMessageCount(1);

    ObjectMapper objectMapper=    objectMapperContextResolver.getContext(ObjectMapperContextResolver.class);
    String jsonString=objectMapper.writeValueAsString(subscription);

CancelSubscription cancelSubscription=cancelSubscriptionTransformer.toKbCancelSubscription(subscription);

Assert.assertEquals("mock auto created by       amel",cancelSubscription.getComment()); 

cancelSubscriptionFromRMQUriEndpoint.sendBody("         {{cancelSubscriptionFromRMQUri}}",jsonString);
   cancelSubscriptionTORMQUriEndpoint.assertIsSatisfied();
  }
}

Assert.assertEquals("由amel",cancelSubscription.getComment()创建的模拟自动);通过调用在mock bean上调用的cancelSubscriptionTransformer.toKbCancelSubscription来获得满足。但是当消息被发送到cancelSubscriptionFromRMQUriEndpoint.sendBody时,路由被调用并且路由中的实际bean不会被模拟bean替换

2 个答案:

答案 0 :(得分:0)

@MickaëlB看起来问题是我没有扩展正确的bean而且我必须在我的路由构建器spring bean中使用@Inject并使用bean名称而不是bean名称的字符串格式

答案 1 :(得分:0)

这已经很老了,但我遇到了这个问题。

答案是,您应该使用.Bean(MyBean.class, "myMethod")而不是.to("bean:myBean?method=myMethod")。原因是第一种方式,骆驼将实例化bean。第二种方式,Spring控制了bean,骆驼会查找它。因此,您可以使用Spring mockBean进行更改。

顺便说一下,我现在正在使用Camel版本3,并且删除了beanRef。如果您使用beanRef,请将其替换为.to("bean:myBean?method=myMethod)

相关问题