2017-01-23 28 views
1

私が得ているエラーは、Testクラスに注入する前にプロパティを解析することと関係があります。プロパティが注入されると私は${property.name}になります。しかし、ネストされた依存関係があるため、Testクラスの設定は非常に間違っているようです。スプリングテストにネストされた依存関係を設定するにはどうすればよいですか?

具体的なエラー:Caused by: java.net.URISyntaxException: Illegal character in authority at index 8: https://${sqs.endpoint}

私は@Beanのための具体的な小道具をロードするために設定クラスを持っている:この@Beanが注入された場合

@Configuration 
public class AWSConfig { 

    private static final Logger LOGGER = LoggerFactory.getLogger(AWSConfig.class); 
    private @Value("${sqs.endpoint}") String endpoint; 

    @Bean(name = "awsClient") 
    @Primary 
    public AmazonSQSAsyncClient amazonSQSClient() { 
     AmazonSQSAsyncClient awsSQSAsyncClient 
       = new AmazonSQSAsyncClient(); 

     awsSQSAsyncClient.setEndpoint(endpoint); 
     return awsSQSAsyncClient; 
    } 
} 

はここにあります:

@Component 
public class SqsQueueSender { 

    private static final Logger LOGGER = LoggerFactory.getLogger(SqsQueueSender.class); 
    private final QueueMessagingTemplate queueMessagingTemplate; 

    @Autowired 
    @Qualifier("awsClient") 
    AmazonSQSAsyncClient amazonSQSAsyncClient; 

    public SqsQueueSender(AmazonSQSAsync amazonSQSAsyncClient) { 
     this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSQSAsyncClient); 
    } 

    //take advantage of convertAndSend to send POJOs in appropriate format 
    public void send(String queueName, String message) { 
     this.queueMessagingTemplate.convertAndSend(queueName, MessageBuilder.withPayload(message).build()); 
    } 
} 

このすべて動作しているようですが、少なくともアプリケーションは起動し、いずれかの場所からログを出力します。私はこのコードに対してユニットテストを実行することはできません。私は設定を正しく設定する方法を理解できません。ここではテストクラスの最新の繰り返しです:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(loader=AnnotationConfigContextLoader.class) 
public class SqsQueueSenderTest { 

    @Configuration 
    static class ContextConfiguration { 

     private @Value("${sqs.endpoint}") String endpoint; 

     @Bean(name = "awsClient") 
     @Primary 
     public AmazonSQSAsyncClient amazonSQSClient() { 
      AmazonSQSAsyncClient awsSQSAsyncClient 
        = new AmazonSQSAsyncClient(); 

      awsSQSAsyncClient.setEndpoint(endpoint); 
      return awsSQSAsyncClient; 
     } 

     @Bean 
     public SqsQueueSender sqsQueueSender() { 
      SqsQueueSender sqsQueueSender = new SqsQueueSender(amazonSQSClient()); 

      // set up the client 
      return sqsQueueSender; 
     } 
    } 

    @Autowired 
    SqsQueueSender sqsQueueSender;// = new SqsQueueSender(new AmazonSQSAsyncClient()); 


    private static final Logger LOGGER = LoggerFactory.getLogger(SqsQueueSenderTest.class); 

    // attributes for in-memory sqs server 
    AmazonSQSClient client; 
    SQSRestServer server; 
    SQSRestServerBuilder sqsRestServerBuilder; 


    @Before 
    public void startup() { 
     LOGGER.info("Building in-memory SQS server"); 
     this.server = sqsRestServerBuilder.withPort(9324).withInterface("localhost").start(); 
     this.client = new AmazonSQSClient(new BasicAWSCredentials("x", "x")); 
     client.setEndpoint("http://localhost:9324"); 
     client.createQueue("test"); 
     LOGGER.info("Finished building in-memory SQS server"); 
    } 

    @After 
    public void shutdown() { 
     LOGGER.info("Stopping in-memory SQS server"); 
     server.stopAndWait(); 
     LOGGER.info("Finished stopping in-memory SQS server"); 
    } 

    @Test 
    public void testSending() { 
     LOGGER.info("~~~~~~~~~~~~~"); 
     sqsQueueSender.send("test", "new message"); 
     LOGGER.info("The current queues are" + client.listQueues().toString()); 
     LOGGER.info("~~~~~~~~~~~~~"); 
    } 
} 

答えて

1

ジョーは、最初にすべてのテストのためのリソースで、あなたの接続プロパティを置く:

src/test/resouces/test.properties 

を次にテストクラス定義にこれを追加します。

@PropertySource(
      value={"classpath:test.properties"}, 
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(loader=AnnotationConfigContextLoader.class) 
public class SqsQueueSenderTest { 

最後に、コンフィグレーションクラスにこのBeanを追加します。

@Configuration static class ContextConfiguration { 

    @Bean 
    public static PropertySourcesPlaceholderConfigurer properties() throws Exception { 
      return new PropertySourcesPlaceholderConfigurer(); 
    } 
} 

あなたのプロパティファイルに 'sqs.endpoint' URLを配置するのを忘れないでください。

これは私の意見では、あなたのプロパティをテストクラスに注入するクリーンな方法の1つです。

+0

ありがとうございますが、実際には 'PropertySource'を内部の' @ Configuration'クラスに置き、実際には 'test/resources'ディレクトリに新しいpropsファイルを作成しなければなりませんでした。 –

+0

これは私がテストファイルに関して意味していたものです:)。また、アノテーションをBean自体に配置することもできます。とにかくそれはあなたのために働いてうれしい –

関連する問題