검색

이 콘텐츠는 선택한 언어로 제공되지 않습니다.

Chapter 5. Creating a new Apache Camel JUnit test case

download PDF

Overview

A common way of testing routes is to use JUnit. The design time tooling includes a wizard that simplifies creating a JUnit test case for your routes. The wizard uses the endpoints you specify to generate the starting point code and configuration for the test.

Note

After you create the boilerplate JUnit test case, you need to modify it to add expectations and assertions specific to the route that you’ve created or modified, so the test is valid for the route.

Prerequisites

Before you create a new JUnit test case, you need to perform a preliminary task:

Deleting and existing JUnit test case

  1. In the Project Explorer view, expand the project’s root node to expose the <root_project>/src/test/java folder.
  2. Locate the JUnit test case file in the /src/test/java folder.

    Depending on which DSL the project is based on, the JUnit test case file is named BlueprintXmlTest.java or CamelContextXmlTest.java.

  3. Right-click the JUnit test case .java file to open the context menu, and then select Delete.

    The JUnit test case .java file disappears from the Project Explorer view.

    You can now create a new JUnit test case.

Creating and adding the src/test/java folder to the build path

  1. In the Project Explorer view, right-click the project’s root to open the context menu.
  2. Select New Folder to open the Create a new folder resource wizard.
  3. In the wizard’s project tree pane, expand the project’s root node and select the src folder.

    Make sure <project_root>/src appears in the Enter or select the parent folder field.

  4. In Folder name, enter /test/java. This folder will store the new JUnit test case you create.
  5. Click Finish.

    In the Project Explorer view, the new src/test/java folder appears under the src/main/resources folder. You can verify that this folder is on the class path by opening its context menu and selecting Build Path. If Remove from Build Path is a menu option, you know the src/test/java folder is on the class path.

    You can now create a new JUnit test case.

Creating a JUnit test case

To create a new JUnit test case for your route:

  1. In the Project Explorer view, select the routing context .xml file in your project.
  2. Right-click it to open the context menu, and then select New Camel Test Case to open the New Camel JUnit Test Case wizard, as shown in Figure 5.1, “New Camel JUnit Test Case wizard”.

    Figure 5.1. New Camel JUnit Test Case wizard

    Entry page of the New Camel JUnit Test Case wizard

    Alternatively, you can open the wizard by selecting File New Other > Fuse > Camel Test Case from the menu bar.

  3. In Source folder, accept the default location of the source code for the test case, or enter another location.

    You can click Browse button to search for a location.

  4. In Package, accept the default package name for the generated test code, or enter another package name.

    You can click Browse button to search for a package.

  5. In Camel XML file under test, accept the default pathname of the routing context file that contains the route you want to test, or enter another pathname.

    You can click Browse button to search for a context file.

  6. In Name, accept the default name for the generated test class, or enter another name.
  7. Select the method stubs you want to include in the generated code.
  8. If you want to include the default generated comments in the generated code, check the Generate comments box.
  9. Click Next button to open the Test Endpoints page. For example, Figure 5.2, “New Camel JUnit Test Case page” shows a route’s input and output file endpoints selected.

    Figure 5.2. New Camel JUnit Test Case page

    Example Test Endpoints page
  10. Under Available endpoints, select the endpoints you want to test. Click the checkbox next to any selected endpoint to deselect it.
  11. Click Finish button .

    Note

    If prompted, add JUnit to the build path.

The artifacts for the test are added to your project and appear in the Project Explorer view under src/test/java. The class implementing the test case opens in the Java editor.

Red Hat logoGithubRedditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

© 2024 Red Hat, Inc.