使用@profile我能够模拟弹簧豆,但是在骆驼路线中未调用模拟bean方法。我正在使用springjunit4classrunner.class并使用@ActiveProfile以下是我想替换的路线,在单元测试中,我的模拟豆,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的方式,我对其余的模拟豆使用相同的方法
@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());通过调用cancelsubscriptionTransFormer.tokbcancelsubscription,该cancelsubscriptionTransubscription被指责。但是,当消息发送到cancelsubscription fromrmquriendpoint.sendbody时,路由被调用,并且路线中的实际豆不被模拟豆
@mickaëlb看起来好像我没有扩展正确的豆
这很旧,但我遇到了这个问题。
答案是,您应该使用.to("bean:myBean?method=myMethod")
,而不是.Bean(MyBean.class, "myMethod")
。原因是骆驼的第一种方法将实例化豆。第二条弹簧的控制权可以控制豆子,骆驼会查找它。因此,您可以使用Spring无知来对其进行更改。
顺便说一下,我现在正在使用骆驼版3版,然后删除了BeanRef。如果使用BeanRef,请用.to("bean:myBean?method=myMethod)
替换。