Giter VIP home page Giter VIP logo

spring-boot-and-testcontainers-on-devcontainer-example's Introduction

spring-boot-and-testcontainers-on-devcontainer-example

これは何?

Visual Studio Code Dev Container上でSpring BootとTestcontainersを動かすサンプルコードです。 Nstockのtech blogの記事用に作成しました。

検証環境

  • M1 Mac & macOS Monterey
  • Visual Studio Code version: 1.78.2
  • Docker Desktop version: 4.19.0

実行方法

mvn test

解説

  • src/test/java/com/example/demo/AbstractIntegrationTest.java

    • テストを実装するクラスのベースクラス。
    • ここでTestcontainersでコンテナを起動する。
    • また DynamicPropertySourceを利用して、Spring BootのDB接続設定をTestcontainersで起動したコンテナに向けている。
  • src/test/java/com/example/demo/DemoApplicationTests.java

    • Testcontainersを利用したテスト実装。
    • AbstractIntegrationTestクラスを継承することでTestcontainersを利用する。
    • @sqlアノテーションで、起動時にschema.sqlとtest-data.sqlをTestcontainersで起動したDBにロードする。
    • テストしているのは、Testcontainersで起動したDBの中に、読みこんだデータがあるか。
  • .devcontainer/docker-compose.yml

    • VSCode Dev Container上でTestcontainersを利用するために複数設定を追加している。
    • app serviceに対して以下を追加
      • volumes に、/var/run/docker.sock をマウントする設定を追加
      • extra_hostshost.docker.internal:host-gateway を追加
      • environmentTESTCONTAINERS_HOST_OVERRIDE=host.docker.internal を追加

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.