2012-08-27 2 views
2

Camel bean 구성 요소가 참조하는 서비스를 사용하는 Grails의 생산 경로에 대한 단위 테스트를 작성해야합니다. 내 요구 사항은 테스트 중 기존 경로를 변경하거나 복사하지 않아야합니다.Grails Services in Camel Production 경로 단위 테스트

문제는 서비스 빈을 모의 해 카멜 레지스트리에 추가하는 것입니다.

'context.registry.registry'개체에서 'bind'메서드를 사용하여이 작업을 수행 할 수있었습니다. 더 안전한 방법으로이를 수행 할 수있는 기능이 있습니까? 낙타 버전은 2.10이다 Grails는 2.1

경로는 다음과 같습니다

package com 

class CamelService { 
    def echo(text) { 
     println "text=$text" 
     text 
    } 
} 

테스트 (경로 질문 간단하게 만 복사) 다음입니다 :

from('direct:validate').to('bean:camelService?method=echo') 

CamelService 그냥 간단한 클래스입니다

package com 

import grails.test.mixin.* 
import org.apache.camel.builder.RouteBuilder 
import org.apache.camel.test.junit4.CamelTestSupport 

@TestFor(CamelService) 
class RouteTests extends CamelTestSupport { 

    @Override 
    protected RouteBuilder createRouteBuilder() throws Exception { 
     return new RouteBuilder() { 
      @Override 
      public void configure() throws Exception { 
       from('direct:validate').to('bean:camelService?method=echo') 
      } 
     }; 
    } 

    void testMockBean() throws Exception { 
     context.registry.registry.bind 'camelService', service 
     def result = template.requestBody('direct:validate', 'message') 
     assert result != null 
     assert result == 'message' 
    } 
} 

답변

1

코드 예제를 사용하여 서비스를 제공합니다. 또 다른 방법은지도 인 SimpleRegistry를 사용하는 것이므로 Map의 put 메소드를 사용하여 레지스트리에 서비스를 넣을 수 있습니다. 그런 다음 CamelTestSupport 클래스의 createCamelContext 메서드를 재정의해야하고 은 SimpleRegistry를 DefaultCamelContext의 생성자에 전달해야합니다.

어쨌든 비 Spring CamelTestSupport 클래스를 사용하는 한 코드는 JNDI 기반 등록을 사용하기 때문에 안전합니다. CamelSpringTestSupport를 사용하는 경우에는 스프링 기반 레지스트리이므로 콩을 추가 할 때 스프링 응용 프로그램 컨텍스트를 사용해야합니다.

+0

감사합니다. 이 질문에 대한 답변입니다. 위의 코드는 JNDI 레지스트리가 테스트 클래스에서 다른 것으로 변경되지 않을 때까지 안전합니다. SimpleRegistry로이를 재정의하면 예상대로 작동합니다. – droggo

0

CamelTestSupport가 아닌 CamelSpringtestSupport를 사용하여 구성 요소를 주입 할 수 있습니다. 귀하의 기본 클래스로.

Spring Test에 대한 설명서를 읽으면 확실하게 도움이되며 테스트에서 모의면을 사용하는 것이 재미있을 수도 있습니다.

어쨌거나 빈의 선언을 포함하는 테스트의 사용자 정의 컨텍스트를 빌드하여 테스트에로드 할 수 있습니다.

public class RouteTests extends CamelSpringTestSupport { 

    @Override 
    protected AbstractApplicationContext createApplicationContext() { 
     return new ClassPathXmlApplicationContext("route-test-context.xml"); 
    } 

    @Test 
    public void testMockBean(){ 
     //... 
    } 
} 

경로 테스트-context.xml에 낙타가 당신이 원하는 모든 사용자 정의 레지스트리를 플러그인하고, 상자 밖으로 당신이 결합 할 수있는 이유입니다, JNDI를 기반 레지스트리를 사용 할 수 있습니다

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:cxf="http://camel.apache.org/schema/cxf" xmlns:camel="http://camel.apache.org/schema/spring" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://camel.apache.org/schema/spring 
    http://camel.apache.org/schema/spring/camel-spring.xsd"> 

    <bean id="service" ref="com.CamelService"/> 
    <camelContext xmlns="http://camel.apache.org/schema/spring"> 
      <package>com</package> 
    </camelContext> 
</beans> 
+0

나는 그것을 알고 있지만 Spring DSL을 사용하고 싶지 않습니다. 프로그래밍 방식으로 규칙을 작성하고 런타임 재로드 작업을 일부 사용하는 계획이 있습니다. 대답은 도움이되지만, 아직 투표 할 수 없습니다. 감사합니다. – droggo