0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · บทเรียน

การตั้งค่าการทดสอบการผสานรวม

กำหนดค่าโครงการให้รองรับการทดสอบการผสานรวม รวมถึงการจัดการสภาพแวดล้อมและสิ่งที่ต้องพึ่งพาสำหรับการทดสอบ

การตั้งค่าการทดสอบการผสานรวม เป็นบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Testing Mastery: JUnit, Mockito & Integration Tests และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Integration Test Setup Intro

Welcome to setting up Integration Tests! After learning about the difference between unit and integration tests, it's time to prepare our project.

Proper setup is crucial for integration tests to run reliably. It ensures your tests are isolated, repeatable, and don't interfere with your development environment.

Separate Source Folders

The first step is to clearly separate your integration tests from your unit tests. This helps organize your codebase and allows build tools to treat them differently.

  • Unit Tests: Typically reside in src/test/java.
  • Integration Tests: Often placed in a dedicated folder like src/it/java or src/integrationTest/java.

This separation makes it easier to run them at different stages of your build process.

Maven: Configure IT Sources

If you're using Maven, you need to tell your pom.xml to recognize the new integration test source folder. The build-helper-maven-plugin is commonly used for this.

It adds src/it/java as a test source directory, allowing Maven to compile your integration tests.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.5.0</version>
        <executions>
          <execution>
            <id>add-integration-test-sources</id>
            <phase>generate-test-sources</phase>
            <goals>
              <goal>add-test-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>src/it/java</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  </build>
</project>

Gradle: Configure IT Sources

For Gradle users, defining a separate source set for integration tests is straightforward. This allows you to compile and run integration tests independently.

The example below shows how to define an integrationTest source set and configure its dependencies.

sourceSets {
    integrationTest {
        java.srcDir 'src/it/java'
        resources.srcDir 'src/it/resources'
        compileClasspath += sourceSets.main.output
        runtimeClasspath += sourceSets.main.output
    }
}

configurations {
    integrationTestImplementation.extendsFrom testImplementation
    integrationTestRuntimeOnly.extendsFrom testRuntimeOnly
}

task integrationTest(type: Test) {
    testClassesDirs = sourceSets.integrationTest.output.classesDirs
    classpath = sourceSets.integrationTest.runtimeClasspath
    shouldRunAfter test
}

check.dependsOn integrationTest

Essential Test Dependencies

Integration tests often require specific dependencies that unit tests might not. These can include:

  • In-memory databases: Like H2 or HSQLDB for fast, isolated database testing.
  • Testcontainers: A library to spin up real service containers (databases, message queues) in Docker.
  • Web testing frameworks: For testing web layers (e.g., Spring's spring-test, MockMvc).

Add these to your test or integration test dependency scope.

Maven Failsafe Plugin

While Maven's Surefire plugin runs unit tests, the Maven Failsafe Plugin is designed for integration tests.

It executes tests during the integration-test and verify phases, ensuring that any setup (like starting a server) happens before tests run and teardown occurs afterward.

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>3.2.5</version>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      ...
    </plugins>
  </build>
</project>

Dedicated Test Configuration

Integration tests often need a different configuration than your main application or unit tests. For example, they might connect to a specific test database or use different external service URLs.

It's good practice to use separate configuration files (e.g., application-it.properties for Spring Boot) that override default settings during integration test execution.

Preparing the Test Environment

Before integration tests run, you often need to prepare the environment. This could involve:

  • Starting an embedded database.
  • Spinning up Docker containers for external services using Testcontainers.
  • Initializing specific test data.

JUnit's @BeforeAll and @AfterAll annotations are perfect for setting up and tearing down resources once per test class.

Environment Setup Example

Here's a simple example demonstrating how you might use @BeforeAll to 'prepare' a resource for your integration tests. This setup runs once before all tests in the class.

Try running this example to see the setup message.

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class SimpleIntegrationSetupTest {

    @BeforeAll
    static void setupEnvironment() {
        System.out.println("--- Setting up test environment (e.g., starting a database) ---");
        // In a real scenario, you'd start a container or connect to a test DB here.
    }

    @AfterAll
    static void tearDownEnvironment() {
        System.out.println("--- Tearing down test environment (e.g., stopping a database) ---");
        // In a real scenario, you'd stop containers or clean up resources here.
    }

    @Test
    void testSomethingIntegrated() {
        System.out.println("  Running an integration test...");
        // Your actual integration test logic would go here.
        // assert something...
    }

    public static void main(String[] args) {
        // This main method allows the snippet to be runnable.
        // In a real project, tests are run by build tools.
        System.out.println("This is a placeholder for running the test.");
        // You would typically use JUnit's API to run tests programmatically if not using Maven/Gradle.
    }
}

Running ITs Question

You've set up your project for integration tests using Maven. Which Maven command is typically used to execute these tests using the Failsafe plugin?

Recap: Structured Testing

Great job! You've learned how to set up your project for integration tests.

  • We separate integration tests into their own source folders (e.g., src/it/java).
  • Build tools like Maven (with build-helper-maven-plugin and maven-failsafe-plugin) or Gradle are configured to recognize and run them.
  • Specific dependencies and dedicated configuration files ensure isolated test environments.
  • @BeforeAll and @AfterAll help manage resource setup and teardown.

This robust setup provides a solid foundation for writing effective integration tests!

คำถามที่พบบ่อย

บทเรียน “การตั้งค่าการทดสอบการผสานรวม” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การตั้งค่าการทดสอบการผสานรวม” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Testing Mastery: JUnit, Mockito & Integration Tests ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Testing Mastery: JUnit, Mockito & Integration Tests มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การตั้งค่าการทดสอบการผสานรวม”

กำหนดค่าโครงการให้รองรับการทดสอบการผสานรวม รวมถึงการจัดการสภาพแวดล้อมและสิ่งที่ต้องพึ่งพาสำหรับการทดสอบ คุณปฏิบัติ Testing Mastery: JUnit, Mockito & Integration Tests ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Testing Mastery: JUnit, Mockito & Integration Tests หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Testing Mastery: JUnit, Mockito & Integration Tests บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การตั้งค่าการทดสอบการผสานรวม” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests นี้ได้ไหม

ได้ บทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การทดสอบหน่วยกับการทดสอบการผสานรวม
  2. การตั้งค่าการทดสอบการผสานรวม
  3. การทดสอบการโต้ตอบกับฐานข้อมูล
  4. การทดสอบ API ภายนอกด้วย WireMock
← กลับไปที่ Testing Mastery: JUnit, Mockito & Integration Tests