首页 文章

在Spring Boot中使用带有CamelSpringTestSupport的测试application.properties文件

提问于
浏览
1

Prerequisites

  • Apache Tomcat 7

  • Spring 季4.1.5.RELEASE

  • Spring Boot 1.2.2.RELEASE

  • Apache Camel 2.15.1

Problem

我使用Spring Boot和EndpointSetup也使用的配置类 .

@SpringBootApplication
@Import({MyConfiguration.class, EndpointSetup.class})
public class MyFatJarRouter extends FatJarRouter { ... }


@Configuration
@ConfigurationProperties(prefix = "camel.route", ignoreUnknownFields = false)
public class MyConfiguration {
    private List<String> brokerUrl = new ArrayList<>();
    public List<String> getBrokerUrl() {return brokerUrl;}
    public void setBrokerUrl(List<String> brokerUrl) {this.brokerUrl = brokerUrl;}

}

在 生产环境 中,默认情况下将从conf / application.properties中读取属性 .

我想通过CamelSpringTestSupport测试我的路线

所以我试过以下:

我在test / resources / config / application.properties下放了一个application.properties( - >在testpath的classpath中)

然后写了以下:

public class MyJmsTest extends CamelSpringTestSupport {

    @Override
    protected AbstractApplicationContext createApplicationContext() {
        return new AnnotationConfigApplicationContext(MyFatJarRouter.class);
    }

    @Test
    public void myTest() throws Exception {
        ...
    }
}

在上面的示例中,不会从放置在测试文件夹中的application.properties读取配置 .

如何在CamelSpringTestSupport单元测试中读取测试特定的配置文件?

3 回答

  • 0

    我可能在回答方面有点迟,但有一种比黑客攻击 endpoints 更好的方法 . 以下解决方案使用Camel 2.16中引入的toD . 我编写了一个自定义组件"github"(没有使用单个Camel专有注释的's an official one as well), and the following is how I test it. Note that I' . 要注入属性,我可以使用 @SpringBootTest 中的 properties 属性,或Spring Boot中可用的任何其他标准技术 .

    请注意,我正在使用 $simple{...} 来避免与Spring属性解析冲突 .

    <rant>

    是的,Camel文档很糟糕!他们把它写成发行说明,其中有一个专门用于每个版本的部分,并且似乎没有更新文档以跟上最新版本(以下技术没有记录) . 想象一下去一家餐馆并要求特价,只有服务员告诉他们前一天和前一周的特价,依此类推 . 如何对文档进行版本控制呢?

    </rant>

    @RunWith(CamelSpringBootRunner.class)
    @SpringBootTest
    @DirtiesContext(classMode = AFTER_EACH_TEST_METHOD)
    public class GitHubRouteTest {
        @Autowired
        private CamelContext camelContext;
    
        @Autowired
        private ProducerTemplate template;
    
        @Autowired
        private GitHubClient gitHubClient;
    
        @Test
        public void testGitHubClientInvoked() throws InterruptedException {
            template.sendBodyAndHeader("direct:start", "whatever",
                    "endpoint", "commits/test/test?username=test&password=test");
    
            verify(gitHubClient).getCommitsForARepo(eq("test"), eq("master"), eq("test"), eq(20));
        }
    
        @SpringBootApplication
        public static class TestApplication {
            public static void main(String[] args) {
                new SpringApplicationBuilder()
                        .sources(TestApplication.class)
                        .web(false)
                        .run(args);
            }
    
            @Bean
            public RouteBuilder testRoute() {
                return new RouteBuilder() {
                    @Override
                    public void configure() throws Exception {
                        from("direct:start")
                                .toD("github:$simple{in.header.endpoint}");
                    }
                };
            }
    
            @Bean
            public GitHubClient mockGitHubClient() {
                GitHubClient mock = Mockito.mock(GitHubClient.class);
    
                return mock;
            }
        }
    }
    
  • 0

    我通过使用标准 spring 单元测试解决了这个问题:

    @RunWith(SpringJUnit4ClassRunner.class)
        @SpringApplicationConfiguration(classes = Application.class)
        @ActiveProfiles("test") // Load applicaton-test.properties in test/resources/config/application-test.properties
        @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) // cleanup spring context because jms broker does not exit properly
        public class MyJmsTest {
    
                private static final String MOCK_MY_ENDPOINT = "mock:myEndpoint";
    
                @Autowired
                CamelContext context;
    
                @Autowired
                ApplicationContext applicationContext;
    
                @Autowired
                ProducerTemplate producerTemplate;
    
    
    
               @Before
               public void configureMocks() throws Exception {
                  context.getRouteDefinition("MyRoute")
                 .adviceWith(context, new AdviceWithRouteBuilder() {
                    @Override
                    public void configure() throws Exception {
                        weaveByToString(".*myEndPointId.*")
                                .replace()
                                .to(MOCK_MY_ENDPOINT);
                    }
                  });
    
               final MockEndpoint endpoint = context.getEndpoint(MOCK_MY_ENDPOINT, MockEndpoint.class);
               endpoint.whenAnyExchangeReceived(new Processor() {
                   @Override
                   public void process(Exchange exchange) throws Exception {
    
                       InputStream inStream = getClass().getClassLoader().getResourceAsStream("xml/my.xml");
                       String in = context.getTypeConverter().convertTo(String.class, inStream);
                       exchange.getIn().setBody(in);
                   }
                });
              }
    
    
    
    
                @Test
                public void synchronousCallBasic_1() throws Exception {
                        final MyConfiguration MyConfiguration = applicationContext.getBean(MyConfiguration.class);
                        final String myMessageBody =
                                context.getTypeConverter().convertTo(String.class, getClass().getClassLoader()
                                        .getResourceAsStream("xml/0010_example.xml"));
    
                        final Object myResult = producerTemplate.requestBody(MyConfiguration.getActiveMqSynchronousEndpointUri(), myMessageBody);
    
                        assertThat(myResult, notNullValue());
                        assertThat((String)myResult, is("<example>1</example>"));
                }
        }
    
  • 2

    我解决了这个问题,我找到了很多注释here,现在正确地注入了测试属性:

    @RunWith(CamelSpringBootRunner.class)
    @SpringBootTest
    @ActiveProfiles("test")
    @EnableAutoConfiguration
    @ComponentScan
    @ContextConfiguration()
    public class MessageDeliveryTest{
    }
    

    此外,测试属性文件需要命名为application- .properties,其中“env”是此处使用的配置文件 . 例如 . 对于测试,属性文件应该是application-test.properties

相关问题