0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Apache Camel × Spring Bootアプリを本番同様に起動してJUnitテストする

Last updated at Posted at 2025-04-27

137D2946-F5A6-4F9A-A14E-B4EC0B074AF4.png

はじめに

Apache Camelでルートをテストする場合、単一ルートのみを対象にするのであればCamelContext単体を起動してテストすることも可能です。
しかし、実際の業務アプリケーションでは、

  • 複数のルートが連携して一連のフローを形成している
  • 起動時にDI(依存性注入)されるコンポーネントや設定情報に依存している

ことが多く、CamelContext単独起動だけでは本番環境と同じ動作を再現できない場面に直面します。

特に、

  • DIされるはずのServiceやComponentが見つからない
  • application.propertiesで管理される設定値が参照できない
  • 一連のフローを通した動作確認ができない

という問題が発生しやすくなります。

この課題を解決するには、
JUnit5からSpring Bootアプリケーションごと起動し、本番と同じDI環境でCamelルートをテストする方法が有効です。

これにより、

  • 本番同様のDI済みコンポーネントと設定値を利用できる
  • 複数ルートが連携する一連の処理を実際に通してテストできる

という実用的な検証が可能になります。

なお、この方法ではSpring Bootアプリケーション全体を起動するため、テスト開始までに数秒〜十数秒の起動時間がかかります。
ですが、得られるテストの正確性を考えると、十分に許容できるコストです。


環境

  • Java 17
  • Spring Boot 3.2系
  • Apache Camel Spring Boot 4.8
  • Camel XML DSL

アプリケーション起動クラス

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ImportResource("classpath:camel-context.xml") // XML DSLを読み込む
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

テストクラス

package com.example.demo;

import org.apache.camel.CamelContext;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;

@SpringBootTest(
    classes = DemoApplication.class,
    properties = {
        "spring.profiles.active=test",
        "log.file.name=test-application.log",
        "hostname=test-host",
        "aws.access.key.id=dummy-access-key",
        "aws.secret.access.key=dummy-secret-key",
        "kafka.bootstrap.servers=localhost:9092",
        "kafka.consumer.topic=test-topic",
        "kafka.consumer.group.id=test-group",
        "kafka.producer.topic=test-producer-topic"
    }
)
@CamelSpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class DemoRouteTest {

    @Autowired
    private CamelContext context;

    @Test
    void testRoute() {
        // ここにテストコードを記述
    }
}

ポイントまとめ

  • テスト専用プロパティを@SpringBootTestのpropertiesに直接記述
  • 環境変数(AWSキーなど)は別途テスト実行時に渡す
  • CamelContextだけでなく、DIされるBeanすべてを本番同様に利用可能
  • 一連のフロー全体を正しく検証できる

注意点

  • Spring Bootアプリ全体を起動するため、テスト開始まで多少の起動時間がかかる
    (目安:数秒〜十数秒)
  • 軽量な単体テストと使い分けが重要

まとめ

Apache Camel Spring Bootアプリケーションでは、
JUnit5から本番同様にアプリごと起動してテストを行うことで、

  • 複雑な一連のフロー全体の動作確認
  • 本番に近いDI環境での検証
  • 外部連携を含めた実践的なテスト

が可能になります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?