Spring boot 无法在驼峰路线中用mock替换SpringBean
使用@Profile,我可以模拟Springbean,但是在camel路由中,没有调用哪个mockbean方法。我正在使用SpringJUnit4ClassRunner.class和@ActiveProfile 下面是我想在单元测试中用模拟bean替换cancelSubscriptionTransformer、myBeanClient、extendedClient bean的路径Spring boot 无法在驼峰路线中用mock替换SpringBean,spring-boot,apache-camel,Spring Boot,Apache Camel,使用@Profile,我可以模拟Springbean,但是在camel路由中,没有调用哪个mockbean方法。我正在使用SpringJUnit4ClassRunner.class和@ActiveProfile 下面是我想在单元测试中用模拟bean替换cancelSubscriptionTransformer、myBeanClient、extendedClient bean的路径 from("{{cancelSubscriptionFromRMQUri}}").routeId("cancelSu
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());通过调用在模拟bean上调用的cancelSubscriptionTransformer.toKbCancelSubscription获取statisfied。但是,当消息从rmquoriendpoint.sendBody发送到CancelSubscription时,调用了路由,路由中的实际bean没有被模拟bean替换。MickaëlB看起来问题在于我没有扩展正确的bean,而且我不得不在我的路由生成器Springbean中使用@Inject,并使用bean名称而不是bean名称的字符串格式。这很旧,但我遇到了这个问题 答案是,您应该使用
.to(“Bean:MyBean?method=myMethod”)
而不是.Bean(MyBean.class,“myMethod”)
。原因是第一种方式,Camel将实例化bean。第二条路,春天控制了豆子,骆驼会查它。因此,您可以使用SpringMockBean来更改它
顺便说一下,我现在使用的是Camel版本3,beanRef被删除了。如果您使用beanRef,请将其替换为
.to(“bean:myBean?method=myMethod)
请提供更多信息(源代码、配置、异常等):