0Pricing
Spring Boot 4 Microservices & REST APIs · レッスン

@SpringBootTestによる統合テスト

アプリケーションコンテキスト全体をテストします

「@SpringBootTestによる統合テスト」はCoddyKit上の無料Spring Boot 4 Microservices & REST APIsレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Microservices & REST APIs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What is an Integration Test

An integration test exercises multiple layers together — controller, service, repository — using a real Spring context.

It catches wiring and configuration problems that unit tests miss.

@SpringBootTest

@SpringBootTest boots the entire application context, loading all beans.

@SpringBootTest
class ApplicationIntegrationTest {
    @Autowired
    UserService userService;

    @Test
    void contextLoads() {
        assertThat(userService).isNotNull();
    }
}

WebEnvironment Modes

Control whether a real server starts with the webEnvironment attribute.

  • MOCK (default) — mock servlet environment
  • RANDOM_PORT — real server on a random port
@SpringBootTest(
    webEnvironment = WebEnvironment.RANDOM_PORT)
class ApiTest { }

Testing with TestRestTemplate

With a real port you can make actual HTTP calls using TestRestTemplate.

@Autowired
TestRestTemplate restTemplate;

@Test
void getsUser() {
    ResponseEntity<User> response =
        restTemplate.getForEntity("/users/1", User.class);
    assertThat(response.getStatusCode())
        .isEqualTo(HttpStatus.OK);
}

WebTestClient

WebTestClient is a fluent client that works for both MVC and WebFlux apps.

@Autowired
WebTestClient webTestClient;

@Test
void getsUser() {
    webTestClient.get().uri("/users/1")
        .exchange()
        .expectStatus().isOk()
        .expectBody()
        .jsonPath("$.name").isEqualTo("Alice");
}

Using a Test Database

By default Spring Boot can replace your datasource with an in-memory H2 database for tests.

@SpringBootTest
@AutoConfigureTestDatabase
class RepositoryIntegrationTest {
    @Autowired UserRepository repository;
}

Test Properties

Override configuration for tests with @TestPropertySource or an application-test.properties profile.

@SpringBootTest
@TestPropertySource(properties = {
    "feature.enabled=false"
})
class FeatureTest { }

Activating Profiles

Use @ActiveProfiles to run with a specific Spring profile.

@SpringBootTest
@ActiveProfiles("test")
class ServiceTest { }

Transactional Rollback

Annotate tests with @Transactional so each test's database changes roll back automatically, keeping tests isolated.

@SpringBootTest
@Transactional
class UserPersistenceTest {
    @Test
    void savesAndRollsBack() {
        repository.save(new User("Alice"));
    }
}

Slicing for Speed

Full @SpringBootTest is slow. Prefer slices like @DataJpaTest or @WebMvcTest when you only need one layer.

@DataJpaTest

@DataJpaTest loads only JPA components and an embedded database — ideal for repository tests.

@DataJpaTest
class UserRepositoryTest {
    @Autowired UserRepository repository;

    @Test
    void findsByName() {
        repository.save(new User("Alice"));
        assertThat(repository.findByName("Alice"))
            .hasSize(1);
    }
}

Quick Check

Test your understanding of integration testing.

Recap

You learned to write integration tests:

  • @SpringBootTest loads the full context
  • RANDOM_PORT + TestRestTemplate/WebTestClient for real HTTP
  • @Transactional rolls back DB changes
  • Use slices for speed

よくある質問

「@SpringBootTestによる統合テスト」レッスンは無料ですか?

はい。「@SpringBootTestによる統合テスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Microservices & REST APIsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Microservices & REST APIsコースには全4レッスンが含まれています。

「@SpringBootTestによる統合テスト」で何を学びますか?

アプリケーションコンテキスト全体をテストします ブラウザで直接実行するハンズオンコードでSpring Boot 4 Microservices & REST APIsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Spring Boot 4 Microservices & REST APIsを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSpring Boot 4 Microservices & REST APIsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「@SpringBootTestによる統合テスト」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSpring Boot 4 Microservices & REST APIsレッスンでコードを書いて実行できますか?

はい。すべてのSpring Boot 4 Microservices & REST APIsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. JUnitとMockitoによる単体テスト
  2. MockMvcによるWeb層テスト
  3. @SpringBootTestによる統合テスト
  4. Testcontainersによる実際の依存関係のテスト
← Spring Boot 4 Microservices & REST APIsに戻る