2016-12-14 1 views
-1

@Profile 사용 봄 콩을 조롱 할 수 있지만 모의 콩 메서드가 호출되지 않는 낙타 루트에서는 사용할 수 있습니다. SpringJUnit4ClassRunner.class를 사용하고 @ActiveProfile을 사용 중입니다. 아래는 단위 테스트에서 내 mock bean으로 cancelSubscriptionTransformer, myBeanClient, extendedClient 빈을 대체하려는 경로입니다.낙타 루트에서 봄 콩을 모의 루트로 대체 할 수 없습니다.

@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(); 
    } 
} 

: 단위 테스트를위한 코드는 아래

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; 
} 
} 

의 나머지 부분에 대해 동일한 방법을 사용입니다 Assert.assertEquals ("mock auto by amel", cancelSubscription.getComment()); 모의 빈에서 호출되는 cancelSubscriptionTransformer.toKbCancelSubscription을 호출하여 통계를 얻습니다. 그러나 메시지가 cancelSubscriptionFromRMQUriEndpoint.sendBody로 보내지면 경로가 호출되고 경로의 실제 빈이 모의 빈에 의해 대체되지 않습니다

+0

자세한 정보 (소스 코드, 구성, 예외 등)를 제공하십시오. http://stackoverflow.com/help/how-to-ask –

답변

0

@ MickaëlB는 올바른 빈을 확장하지 못하고 있었고 내 경로 빌더 스프링 빈에서 @Inject를 사용하고 빈 이름의 문자열 형식 대신 빈 이름을 사용하는 경우

관련 문제