CLOVER馃崁

That was when it all began.

Database Rider銇⊿pring Framework銈掑悎銈忋仜銇︿娇銇嗘檪銇丏irtiesContext銈掍娇銇嗗牬鍚堛伅cacheConnection銈掔劇鍔广伀銇椼仧鏂广亴銈堛亜銇ㄣ亜銇嗚┍

銇撱倢銇併仾銇倰銇椼仧銇忋仸鏇搞亜銇熴倐銇紵

Database Rider銇⊿pring Boot锛Spring Framework锛夈倰鍚堛倧銇涖仸浣裤仯銇︺亜銇︺@DirtiesContext銈€儙銉嗐兗銈枫儳銉銈掍娇銇嗐仺閫旂銇儐銈广儓銇
澶辨晽銇欍倠銈堛亞銇仾銇c仸銉忋優銇c仧銇仹銉°儮銆

銇┿亞銇勩亞銇撱仺銇嬶紵

Mockito銈掍娇銇c仧鏅傘仾銇┿Spring FrameworkApplicationContext銈掔牬妫勩仐銇熴亜鏅傘伀@DirtiesContext銈€儙銉嗐兗銈枫儳銉銈掍娇銇嗐亾銇ㄣ亴銇傘倞銇俱仚銆

Testing / Appendix / Annotations / Spring Testing Annotations / @DirtiesContext

DirtiesContext (Spring Framework 6.0.7 API)

@DirtiesContext銈€儙銉嗐兗銈枫儳銉銈掍娇銇嗐仺瑕忓畾銇偪銈ゃ儫銉炽偘銇ApplicationContext銈掋儉銉笺儐銈c仹銇傘倠銇ㄣ優銉笺偗銇椼佺牬妫勩仚銈嬨亾銇ㄣ亴
銇с亶銇俱仚銆

渚嬨仺銇椼仸銇併偡銉炽偘銉儓銉炽仹銇傘倠Spring銇х鐞嗐仌銈屻仧Bean銇姸鎱嬪鏇淬倰琛屻仯銇熸檪銆佺祫銇胯炯銇裤儑銉笺偪銉欍兗銈广伄鐘舵厠澶夋洿銈掕銇c仧鏅傘仾銇┿亴
鎸欍亽銈夈倢銇︺亜銇俱仚銆

@DirtiesContext銈€儙銉嗐兗銈枫儳銉銇偗銉┿偣銇俱仧銇儭銈姐儍銉夈伀浠樹笌銇欍倠銇撱仺銇屻仹銇嶏紙銇濄倢銇炪倢銈儵銈广儮銉笺儔銆併儭銈姐儍銉夈儮銉笺儔銇ㄣ仐銇
鎵便倧銈屻伨銇欙級銆併儉銉笺儐銈c仺銉炪兗銈仚銈嬨偪銈ゃ儫銉炽偘銈掍互涓嬨伄銈堛亞銇埗寰°仚銈嬨亾銇ㄣ亴銇с亶銇俱仚銆

  • 鐝惧湪銇儐銈广儓銈儵銈广伄鍓嶏紙BEFORE_CLASS锛忋偗銉┿偣銉€兗銉夛級
  • 鐝惧湪銇儐銈广儓銈儵銈广伄鍚勩儐銈广儓銉°偨銉冦儔銇疅琛屽墠锛BEFORE_EACH_TEST_METHOD锛忋偗銉┿偣銉€兗銉夛級
  • 鐝惧湪銇儐銈广儓銉°偨銉冦儔銇疅琛屽墠锛BEFORE_METHOD锛忋儭銈姐儍銉夈儮銉笺儔锛
  • 鐝惧湪銇儐銈广儓銉°偨銉冦儔銇疅琛屽緦锛AFTER_METHOD锛忋儭銈姐儍銉夈儮銉笺儔锛
  • 鐝惧湪銇偗銉┿偣銇悇銉嗐偣銉堛儭銈姐儍銉夈伄瀹熻寰岋紙AFTER_EACH_TEST_METHOD锛忋偗銉┿偣銉€兗銉夛級
  • 鐝惧湪銇儐銈广儓銈儵銈广伄寰岋紙AFTER_CLASS锛忋偗銉┿偣銉€兗銉夛級

銉囥儠銈┿儷銉堛伅銆併偗銉┿偣銉€兗銉夈伄鍫村悎銇AFTER_CLASS銆併儭銈姐儍銉夈儮銉笺儔銇牬鍚堛伅AFTER_METHOD銇с仚銆

銇撱倢銇ㄩ枹閫c仚銈嬭┍椤屻亴ApplicationContext銇偔銉c儍銈枫儱銇伄銇с仚銇屻併仢銈屻伅銇撱仭銈夈伀鏇搞亱銈屻仸銇勩伨銇欍

Testing / Spring TestContext Framework / Context Management / Context Caching

銇с併亾銈屻仺Database Rider銇屻仼銇嗛枹淇傘亴銇傘倠銇亱銇ㄣ亜銇嗐亾銇ㄣ仹銇欍亴銆丏atabase Rider銇儑銉曘偐銉儓銇с儑銉笺偪銉欍兗銈规帴缍氥倰銈儯銉冦偡銉ャ仐銇俱仚銆

Database Rider Documentation / Configuration / DBUnit configuration

cacheConnection銇ㄣ亜銇嗐儣銉儜銉嗐偅銇с仚銇傘亾銇°倝銇屻儑銉曘偐銉儓銇true銇仾銇c仸銇勩伨銇欍

Database connection will be reused among tests

銉囥兗銈裤儥銉笺偣鎺ョ稓銇屻儐銈广儓銇у啀鍒╃敤銇曘倢銈嬨併仺鏇搞亱銈屻仸銇勩伨銇欍伃銆

@DirtiesContextApplicationContext銈掔牬妫勩仚銈嬨倛銇嗐伀銇椼仧鍫村悎銆丏atabase Rider銇Spring Framework绲岀敱銇у彇寰椼仐銇︺亜銈
銉囥兗銈裤儥銉笺偣鎺ョ稓銇岀劇鍔广伀銇倞銆丏atabase Rider銈掍娇銇c仧銉嗐偣銉堛亴杌掍甫銇垮け鏁椼仐銇︺亜銇忋倛銇嗐伀銇倞銇俱仚銆

Rider JUnit 5銉€偢銉ャ兗銉丷ider Spring銉€偢銉ャ兗銉伄銇┿仭銈夈倰浣裤仯銇︺倐鐧虹敓銇椼伨銇欍

銇撱伄銇傘仧銈娿伄issue銈掕銇︺亜銇︺佸晱椤屻倰鎶婃彙銇椼伨銇椼仧銆

Issue using @ExpectedDataSet without @DataSet · Issue #136 · database-rider/database-rider · GitHub

SpringBoot 2.x, JUnit5 and DatabaseRider configuration woes · Issue #183 · database-rider/database-rider · GitHub

銇撱伄鍟忛銇伓銇ゃ亱銇c仧鍫村悎銇cacheConnectionfalse銇仚銈嬨亾銇ㄣ亴鍥為伩绛栥伀銇倞銇俱仚銆

銇撱伄銇傘仧銈娿倰瑕嬨仸銇勩亶銇熴亜銇ㄦ濄亜銇俱仚銆

鐠板

浠婂洖銇挵澧冦伅銆併亾銇°倝銆

$ java --version
openjdk 17.0.6 2023-01-17
OpenJDK Runtime Environment (build 17.0.6+10-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.6+10-Ubuntu-0ubuntu122.04, mixed mode, sharing)


$ mvn --version
Apache Maven 3.9.1 (2e178502fcdbffc201671fb2537d0cb4b4cc58f8)
Maven home: $HOME/.sdkman/candidates/maven/current
Java version: 17.0.6, vendor: Private Build, runtime: /usr/lib/jvm/java-17-openjdk-amd64
Default locale: ja_JP, platform encoding: UTF-8
OS name: "linux", version: "5.15.0-69-generic", arch: "amd64", family: "unix"

銉囥兗銈裤儥銉笺偣銇伅銆MySQL銈掍娇鐢ㄣ仐銇俱仚銆

 MySQL  localhost:3306 ssl  practice  SQL > select version();
+-----------+
| version() |
+-----------+
| 8.0.32    |
+-----------+
1 row in set (0.0027 sec)

MySQL銇172.17.0.2銇у嫊浣溿仐銇︺亜銈嬨倐銇仺銇椼併儑銉笺偪銉欍兗銈practice銆併偄銈偊銉炽儓銇kazuhirapassword銇ф帴缍氥仹銇嶃倠銈傘伄銇 銇椼伨銇欍

Spring Boot銉椼儹銈搞偋銈儓銈掍綔鎴愩仚銈

銇俱仛銇疭pring Boot銉椼儹銈搞偋銈儓銈掍綔鎴愩仐銇俱仚銆備緷瀛橀枹淇傘伀銇jdbcmysql銈掑惈銈併伨銇椼仧銆

$ curl -s https://start.spring.io/starter.tgz \
  -d bootVersion=3.0.5 \
  -d javaVersion=17 \
  -d type=maven-project \
  -d name=database-rider-with-dirtiescontext \
  -d groupId=org.littlewings \
  -d artifactId=database-rider-with-dirtiescontext \
  -d version=0.0.1-SNAPSHOT \
  -d packageName=org.littlewings.spring.dbrider \
  -d dependencies=jdbc,mysql \
  -d baseDir=database-rider-with-dirtiescontext | tar zxvf -

銉囥偅銉偗銉銉唴銇Щ鍕曘

$ cd database-rider-with-dirtiescontext

鐢熸垚銇曘倢銇Maven渚濆瓨闁總銇仼銆

        <properties>
                <java.version>17</java.version>
        </properties>
        <dependencies>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-jdbc</artifactId>
                </dependency>

                <dependency>
                        <groupId>com.mysql</groupId>
                        <artifactId>mysql-connector-j</artifactId>
                        <scope>runtime</scope>
                </dependency>
                <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-test</artifactId>
                        <scope>test</scope>
                </dependency>
        </dependencies>

        <build>
                <plugins>
                        <plugin>
                                <groupId>org.springframework.boot</groupId>
                                <artifactId>spring-boot-maven-plugin</artifactId>
                        </plugin>
                </plugins>
        </build>

鑷嫊鐢熸垚銇曘倢銇銈姐兗銈广偝銉笺儔銇墛闄ゃ仐銇︺亰銇嶃伨銇欍

$ rm src/main/java/org/littlewings/spring/dbrider/DatabaseRiderWithDirtiescontextApplication.java src/test/java/org/littlewings/spring/dbrider/DatabaseRiderWithDirtiescontextApplicationTests.java

銇撱亾銇丷ider JUnit 5銇≧ider Spring銇2銇ゃ伄銉€偢銉ャ兗銉倰杩藉姞銆

     <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.database-rider</groupId>
            <artifactId>rider-junit5</artifactId>
            <version>1.36.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.database-rider</groupId>
            <artifactId>rider-spring</artifactId>
            <version>1.36.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

缍氥亜銇︺併儐銉笺儢銉畾缇┿倓銈姐兗銈广偝銉笺儔銈掍綔鎴愩仐銇︺亜銇嶃伨銇欍

浠婂洖浣裤亞銉嗐兗銉栥儷銇佷互涓嬨伄銈堛亞銇畾缇┿傘亰椤屻伅鏇哥睄銇仐銇俱仐銇熴

src/main/resources/schema.sql

drop table if exists book;

create table book(
  isbn varchar(14),
  title varchar(100),
  price int,
  primary key(isbn)
);

銇撱伄SQL銇岃捣鍕曟檪銇瘞鍥炲疅琛屻仌銈屻倠銈堛亞銇ō瀹氥

src/main/resources/application.properties

spring.datasource.url=jdbc:mysql://172.17.0.2:3306/practice?characterEncoding=utf-8&connectionCollation=utf8mb4_0900_bin
spring.datasource.username=kazuhira
spring.datasource.password=password

spring.sql.init.mode=always

銈ㄣ兂銉嗐偅銉嗐偅鐨勩仾銈儵銈广

src/main/java/org/littlewings/spring/dbrider/Book.java

package org.littlewings.spring.dbrider;

public class Book {
    String isbn;
    String title;
    Integer price;

    public static Book create(String isbn, String title, Integer price) {
        Book book = new Book();
        book.setIsbn(isbn);
        book.setTitle(title);
        book.setPrice(price);

        return book;
    }

    // getter锛弒etter銇渷鐣
}

@SpringBootApplication銈€儙銉嗐兗銈枫儳銉銈掍粯涓庛仐銇熴偗銉┿偣銆

src/main/java/org/littlewings/spring/dbrider/App.java

package org.littlewings.spring.dbrider;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
}

Database Rider銈掍娇銇c仸銉嗐偣銉堛倰鏇搞亸

銇с伅銆丏atabase Rider銈掍娇銇c仸銉嗐偣銉堛倰鏇搞亜銇︺亜銇嶃伨銇欍

Database Rider锛堛仺銇勩亞銇DbUnit锛夈伄瑷畾銆

src/test/resources/dbunit.yml

cacheConnection: true
properties:
  caseSensitiveTableNames: true

cacheConnection銇伨銇氥伅false銇仐銇︺亰銇嶃伨銇欍

銉嗐偣銉堛伅銆丼pring JDBC銈掍娇銇c仧銉囥兗銈跨櫥閷层仺Database Rider銇@ExpectedDataSet銈€儙銉嗐兗銈枫儳銉銇с伄銈€偟銉笺偡銉с兂銈掕銇嗙啊鍗樸仾銈傘伄銇
銇椼伨銇欍

Rider Spring銉€偢銉ャ兗銉倰浣裤仯銇熴儜銈裤兗銉炽

src/test/java/org/littlewings/spring/dbrider/DatabaseRiderSpringTest.java

package org.littlewings.spring.dbrider;

import com.github.database.rider.core.api.dataset.ExpectedDataSet;
import com.github.database.rider.spring.api.DBRider;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@SpringBootTest
@DBRider
public class DatabaseRiderSpringTest {
    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    @Test
    @Transactional
    //@DirtiesContext
    @ExpectedDataSet(value = "datasets/DatabaseRiderSpringTest/expect_addBook1.yml", orderBy = "price")
    void addBook1() {
        List<Book> books = List.of(
                Book.create("978-4295008477", "鏂颁笘浠ava銉椼儹銈般儵銉熴兂銈般偓銈ゃ儔[Java SE 10/11/12/13銇ㄨ█瑾炴嫛寮点儣銉偢銈с偗銉圿", 2860),
                Book.create("978-4621303252", "Effective Java 绗3鐗", 4400),
                Book.create("978-4774189093", "Java鏈牸鍏ラ杸 銆溿儮銉銉炽偣銈裤偆銉伀銈堛倠鍩虹銇嬨倝銈儢銈搞偋銈儓鎸囧悜銉诲疅鐢ㄣ儵銈ゃ儢銉┿儶銇俱仹", 3278)
        );

        books.forEach(book -> {
            System.out.println(book);
            jdbcTemplate.update("""
                            insert into book(isbn, title, price)
                            values(:isbn, :title, :price)""",
                    new BeanPropertySqlParameterSource(book));
                }
        );
    }

    @Test
    @Transactional
    //@DirtiesContext
    @ExpectedDataSet(value = "datasets/DatabaseRiderSpringTest/expect_addBook2.yml", orderBy = "price")
    void addBook2() {
        List<Book> books = List.of(
                Book.create("978-4798161488", "MySQL寰瑰簳鍏ラ杸 绗4鐗 MySQL 8.0瀵惧繙", 4180),
                Book.create("978-4798147406", "瑭宠ВMySQL 5.7 姝€伨銈夈伂閫插寲銇箺銈婇亝銈屻仾銇勩仧銈併伄銉嗐偗銉嬨偒銉偓銈ゃ儔", 3960)
        );

        books.forEach(book -> {
            System.out.println(book);
            jdbcTemplate.update("""
                            insert into book(isbn, title, price)
                            values(:isbn, :title, :price)""",
                    new BeanPropertySqlParameterSource(book));
                }
        );
    }
}

Rider Spring銇牬鍚堛伅@Transactional銈€儙銉嗐兗銈枫儳銉銇ㄩe嫊銇с亶銇俱仚銆

    @Test
    @Transactional
    //@DirtiesContext
    @ExpectedDataSet(value = "datasets/DatabaseRiderSpringTest/expect_addBook1.yml", orderBy = "price")
    void addBook1() {

@DirtiesContext銈€儙銉嗐兗銈枫儳銉銇緦銇ф湁鍔广伀銇椼伨銇欍

銈€偟銉笺偡銉с兂銇т娇銇嗐儑銉銈裤偦銉冦儓

src/test/resources/datasets/DatabaseRiderSpringTest/expect_addBook1.yml

book:
  - isbn: "978-4295008477"
    title: "鏂颁笘浠ava銉椼儹銈般儵銉熴兂銈般偓銈ゃ儔[Java SE 10/11/12/13銇ㄨ█瑾炴嫛寮点儣銉偢銈с偗銉圿"
    price: 2860
  - isbn: "978-4774189093"
    title: "Java鏈牸鍏ラ杸 銆溿儮銉銉炽偣銈裤偆銉伀銈堛倠鍩虹銇嬨倝銈儢銈搞偋銈儓鎸囧悜銉诲疅鐢ㄣ儵銈ゃ儢銉┿儶銇俱仹"
    price: 3278
  - isbn: "978-4621303252"
    title: "Effective Java 绗3鐗"
    price: 4400

src/test/resources/datasets/DatabaseRiderSpringTest/expect_addBook2.yml

book:
  - isbn: "978-4798147406"
    title: "瑭宠ВMySQL 5.7 姝€伨銈夈伂閫插寲銇箺銈婇亝銈屻仾銇勩仧銈併伄銉嗐偗銉嬨偒銉偓銈ゃ儔"
    price: 3960
  - isbn: "978-4798161488"
    title: "MySQL寰瑰簳鍏ラ杸 绗4鐗 MySQL 8.0瀵惧繙"
    price: 4180

缍氥亜銇︺丷ider JUnit 5銈掍娇銇c仧鍫村悎銆

src/test/java/org/littlewings/spring/dbrider/DatabaseRiderJUnit5Test.java

package org.littlewings.spring.dbrider;

import com.github.database.rider.core.api.dataset.DataSet;
import com.github.database.rider.core.api.dataset.ExpectedDataSet;
import com.github.database.rider.junit5.api.DBRider;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@SpringBootTest
@DBRider
public class DatabaseRiderJUnit5Test {
    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    @Test
    //@Transactional  // Rider JUnit5銇牬鍚堛伅@Transactinal銇劇鍔
    //@DirtiesContext
    @DataSet(cleanBefore = true)
    @ExpectedDataSet(value = "datasets/DatabaseRiderJUnit5Test/expect_addBook1.yml", orderBy = "price")
    void addBook1() {
        List<Book> books = List.of(
                Book.create("978-4295008477", "鏂颁笘浠ava銉椼儹銈般儵銉熴兂銈般偓銈ゃ儔[Java SE 10/11/12/13銇ㄨ█瑾炴嫛寮点儣銉偢銈с偗銉圿", 2860),
                Book.create("978-4621303252", "Effective Java 绗3鐗", 4400),
                Book.create("978-4774189093", "Java鏈牸鍏ラ杸 銆溿儮銉銉炽偣銈裤偆銉伀銈堛倠鍩虹銇嬨倝銈儢銈搞偋銈儓鎸囧悜銉诲疅鐢ㄣ儵銈ゃ儢銉┿儶銇俱仹", 3278)
        );

        books.forEach(book -> {
            System.out.println(book);
            jdbcTemplate.update("""
                            insert into book(isbn, title, price)
                            values(:isbn, :title, :price)""",
                    new BeanPropertySqlParameterSource(book));
                }
        );
    }

    @Test
    //@Transactional  // Rider JUnit5銇牬鍚堛伅@Transactinal銇劇鍔
    //@DirtiesContext
    @DataSet(cleanBefore = true)
    @ExpectedDataSet(value = "datasets/DatabaseRiderJUnit5Test/expect_addBook2.yml", orderBy = "price")
    void addBook2() {
        List<Book> books = List.of(
                Book.create("978-4798161488", "MySQL寰瑰簳鍏ラ杸 绗4鐗 MySQL 8.0瀵惧繙", 4180),
                Book.create("978-4798147406", "瑭宠ВMySQL 5.7 姝€伨銈夈伂閫插寲銇箺銈婇亝銈屻仾銇勩仧銈併伄銉嗐偗銉嬨偒銉偓銈ゃ儔", 3960)
        );

        books.forEach(book -> {
            System.out.println(book);
            jdbcTemplate.update("""
                            insert into book(isbn, title, price)
                            values(:isbn, :title, :price)""",
                    new BeanPropertySqlParameterSource(book));
                }
        );
    }
}

Rider JUnit 5銇牬鍚堛@Transactional銈€儙銉嗐兗銈枫儳銉銇ㄣ伅閫e嫊銇椼仾銇勩伄銇@DataSet銇у厛銇儑銉笺偪銈掑墛闄ゃ仚銈嬨倛銇嗐伀銇椼仸銇勩伨銇欍

    @Test
    //@Transactional  // Rider JUnit5銇牬鍚堛伅@Transactinal銇劇鍔
    //@DirtiesContext
    @DataSet(cleanBefore = true)
    @ExpectedDataSet(value = "datasets/DatabaseRiderJUnit5Test/expect_addBook1.yml", orderBy = "price")
    void addBook1() {

銈€偟銉笺偡銉с兂銇т娇銇嗐儑銉銈裤偦銉冦儓銇腑韬伅銆丷ider Spring銇倐銇仺鍚屻仒銇с仚銆

src/test/resources/datasets/DatabaseRiderJUnit5Test/expect_addBook1.yml

book:
  - isbn: "978-4295008477"
    title: "鏂颁笘浠ava銉椼儹銈般儵銉熴兂銈般偓銈ゃ儔[Java SE 10/11/12/13銇ㄨ█瑾炴嫛寮点儣銉偢銈с偗銉圿"
    price: 2860
  - isbn: "978-4774189093"
    title: "Java鏈牸鍏ラ杸 銆溿儮銉銉炽偣銈裤偆銉伀銈堛倠鍩虹銇嬨倝銈儢銈搞偋銈儓鎸囧悜銉诲疅鐢ㄣ儵銈ゃ儢銉┿儶銇俱仹"
    price: 3278
  - isbn: "978-4621303252"
    title: "Effective Java 绗3鐗"
    price: 4400

src/test/resources/datasets/DatabaseRiderJUnit5Test/expect_addBook2.yml

book:
  - isbn: "978-4798147406"
    title: "瑭宠ВMySQL 5.7 姝€伨銈夈伂閫插寲銇箺銈婇亝銈屻仾銇勩仧銈併伄銉嗐偗銉嬨偒銉偓銈ゃ儔"
    price: 3960
  - isbn: "978-4798161488"
    title: "MySQL寰瑰簳鍏ラ杸 绗4鐗 MySQL 8.0瀵惧繙"
    price: 4180

銉嗐偣銉堛倰琛屻仯銇﹀嫊浣滅⒑瑾嶃

$ mvn test

銇欍伖銇︺儜銈广仐銇俱仚銆

[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

@DirtiesContext銈€儙銉嗐兗銈枫儳銉銈掑姞銇堛倠

銇撱伄鐘舵厠銇с@DirtiesContext銈€儙銉嗐兗銈枫儳銉銈掋儐銈广儓銉°偨銉冦儔銇拷鍔犮仐銇俱仚锛堛仺銇勩亞銇嬨銈炽儭銉炽儓銈€偊銉銇В闄ゃ仹銇欍伃锛夈

Rider Spring鐗堛

    @Test
    @Transactional
    @DirtiesContext
    @ExpectedDataSet(value = "datasets/DatabaseRiderSpringTest/expect_addBook1.yml", orderBy = "price")
    void addBook1() {
        List<Book> books = List.of(
                Book.create("978-4295008477", "鏂颁笘浠ava銉椼儹銈般儵銉熴兂銈般偓銈ゃ儔[Java SE 10/11/12/13銇ㄨ█瑾炴嫛寮点儣銉偢銈с偗銉圿", 2860),
                Book.create("978-4621303252", "Effective Java 绗3鐗", 4400),
                Book.create("978-4774189093", "Java鏈牸鍏ラ杸 銆溿儮銉銉炽偣銈裤偆銉伀銈堛倠鍩虹銇嬨倝銈儢銈搞偋銈儓鎸囧悜銉诲疅鐢ㄣ儵銈ゃ儢銉┿儶銇俱仹", 3278)
        );

        books.forEach(book -> {
            System.out.println(book);
            jdbcTemplate.update("""
                            insert into book(isbn, title, price)
                            values(:isbn, :title, :price)""",
                    new BeanPropertySqlParameterSource(book));
                }
        );
    }

    @Test
    @Transactional
    @DirtiesContext
    @ExpectedDataSet(value = "datasets/DatabaseRiderSpringTest/expect_addBook2.yml", orderBy = "price")
    void addBook2() {
        List<Book> books = List.of(
                Book.create("978-4798161488", "MySQL寰瑰簳鍏ラ杸 绗4鐗 MySQL 8.0瀵惧繙", 4180),
                Book.create("978-4798147406", "瑭宠ВMySQL 5.7 姝€伨銈夈伂閫插寲銇箺銈婇亝銈屻仾銇勩仧銈併伄銉嗐偗銉嬨偒銉偓銈ゃ儔", 3960)
        );

        books.forEach(book -> {
            System.out.println(book);
            jdbcTemplate.update("""
                            insert into book(isbn, title, price)
                            values(:isbn, :title, :price)""",
                    new BeanPropertySqlParameterSource(book));
                }
        );
    }

@DirtiesContext銈€儙銉嗐兗銈枫儳銉銈掋儭銈姐儍銉夈伀浠樹笌銇椼仸銇勩倠銇仹銆併儐銈广儓銉°偨銉冦儔銇疅琛屽緦銇斻仺銇儉銉笺儐銈c仺銉炪兗銈仌銈屻
ApplicationContext銇岀牬妫勩仌銈屻倠銇撱仺銇仾銈娿伨銇欍

Rider JUnit 5鐗堛

    @Test
    //@Transactional  // Rider JUnit5銇牬鍚堛伅@Transactinal銇劇鍔
    @DirtiesContext
    @DataSet(cleanBefore = true)
    @ExpectedDataSet(value = "datasets/DatabaseRiderJUnit5Test/expect_addBook1.yml", orderBy = "price")
    void addBook1() {
        List<Book> books = List.of(
                Book.create("978-4295008477", "鏂颁笘浠ava銉椼儹銈般儵銉熴兂銈般偓銈ゃ儔[Java SE 10/11/12/13銇ㄨ█瑾炴嫛寮点儣銉偢銈с偗銉圿", 2860),
                Book.create("978-4621303252", "Effective Java 绗3鐗", 4400),
                Book.create("978-4774189093", "Java鏈牸鍏ラ杸 銆溿儮銉銉炽偣銈裤偆銉伀銈堛倠鍩虹銇嬨倝銈儢銈搞偋銈儓鎸囧悜銉诲疅鐢ㄣ儵銈ゃ儢銉┿儶銇俱仹", 3278)
        );

        books.forEach(book -> {
            System.out.println(book);
            jdbcTemplate.update("""
                            insert into book(isbn, title, price)
                            values(:isbn, :title, :price)""",
                    new BeanPropertySqlParameterSource(book));
                }
        );
    }

    @Test
    //@Transactional  // Rider JUnit5銇牬鍚堛伅@Transactinal銇劇鍔
    @DirtiesContext
    @DataSet(cleanBefore = true)
    @ExpectedDataSet(value = "datasets/DatabaseRiderJUnit5Test/expect_addBook2.yml", orderBy = "price")
    void addBook2() {
        List<Book> books = List.of(
                Book.create("978-4798161488", "MySQL寰瑰簳鍏ラ杸 绗4鐗 MySQL 8.0瀵惧繙", 4180),
                Book.create("978-4798147406", "瑭宠ВMySQL 5.7 姝€伨銈夈伂閫插寲銇箺銈婇亝銈屻仾銇勩仧銈併伄銉嗐偗銉嬨偒銉偓銈ゃ儔", 3960)
        );

        books.forEach(book -> {
            System.out.println(book);
            jdbcTemplate.update("""
                            insert into book(isbn, title, price)
                            values(:isbn, :title, :price)""",
                    new BeanPropertySqlParameterSource(book));
                }
        );
    }

銇撱倢銇с儐銈广儓銈掑疅琛屻

$ mvn test

銇欍倠銇ㄣ併儐銈广儓銇崐鍒嗐亴澶辨晽銇椼伨銇欍

[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR]   DatabaseRiderJUnit5Test.addBook2 禄 Runtime DataSet comparison failed due to fo...
[ERROR]   DatabaseRiderSpringTest.addBook2 禄 Runtime Could not get driver information fr...
[INFO]
[ERROR] Tests run: 4, Failures: 0, Errors: 2, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

銇┿仭銈夈倐銆佸悓銇樸儐銈广儓銈儵銈瑰唴銇儐銈广儓銉°偨銉冦儔銇墖鏂广亴澶辨晽銇椼仸銇勩伨銇欍

銇撱伄鏅傘丷ider Spring銇柟銇互涓嬨伄銈堛亞銇銈广偪銉冦偗銉堛儸銉笺偣銇屽嚭鍔涖仌銈屻仸銇勩伨銇椼仧銆

2023-04-16T23:57:11.724+09:00  WARN 38092 --- [           main] com.zaxxer.hikari.pool.ProxyConnection   : HikariPool-3 - Connection com.mysql.cj.jdbc.ConnectionImpl@32dcfeea marked as broken because of SQLSTATE(08003), ErrorCode(0)

java.sql.SQLNonTransientConnectionException: No operations allowed after connection closed.
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:73) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:1089) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:1079) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.zaxxer.hikari.pool.ProxyConnection.createStatement(ProxyConnection.java:284) ~[HikariCP-5.0.1.jar:na]
        at com.zaxxer.hikari.pool.HikariProxyConnection.createStatement(HikariProxyConnection.java) ~[HikariCP-5.0.1.jar:na]
        at com.github.database.rider.core.dataset.DataSetExecutorImpl.handleConstraints(DataSetExecutorImpl.java:344) ~[rider-core-1.36.0.jar:na]
        at com.github.database.rider.core.dataset.DataSetExecutorImpl.disableConstraints(DataSetExecutorImpl.java:332) ~[rider-core-1.36.0.jar:na]
        at com.github.database.rider.core.dataset.DataSetExecutorImpl.clearDatabase(DataSetExecutorImpl.java:576) ~[rider-core-1.36.0.jar:na]
        at com.github.database.rider.core.dataset.DataSetExecutorImpl.createDataSet(DataSetExecutorImpl.java:108) ~[rider-core-1.36.0.jar:na]
        at com.github.database.rider.core.RiderRunner.runBeforeTest(RiderRunner.java:44) ~[rider-core-1.36.0.jar:na]
        at com.github.database.rider.junit5.DBUnitExtension.beforeTestExecution(DBUnitExtension.java:69) ~[rider-junit5-1.36.0.jar:na]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeTestExecutionCallbacks$5(TestMethodTestDescriptor.java:191) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$6(TestMethodTestDescriptor.java:202) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:202) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeTestExecutionCallbacks(TestMethodTestDescriptor.java:190) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:136) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:150) ~[surefire-junit-platform-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:124) ~[surefire-junit-platform-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384) ~[surefire-booter-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345) ~[surefire-booter-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) ~[surefire-booter-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418) ~[surefire-booter-2.22.2.jar:2.22.2]
Caused by: com.mysql.cj.exceptions.ConnectionIsClosedException: No operations allowed after connection closed.
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.NativeSession.checkClosed(NativeSession.java:761) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:564) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        ... 58 common frames omitted

銉囥兗銈裤儥銉笺偣鎺ョ稓銇仚銇с伀闁夈仒銇︺亜銇俱仚銆併仺銇勩亞銇撱仺銇с仚銇

Caused by: com.mysql.cj.exceptions.ConnectionIsClosedException: No operations allowed after connection closed.

Rider JUnit 5銇牬鍚堛伅@DataSet銈€儙銉嗐兗銈枫儳銉銈掍娇銇c仧鍑︾悊銇уけ鏁椼仐銇︺亜銈嬨倛銇嗐仹銇椼仧銆

2023-04-16T23:57:11.725+09:00  WARN 38092 --- [           main] c.g.d.r.c.dataset.DataSetExecutorImpl    : Could not clean database before test.

java.sql.SQLNonTransientConnectionException: No operations allowed after connection closed.
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:73) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:1089) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java:1079) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.zaxxer.hikari.pool.ProxyConnection.createStatement(ProxyConnection.java:284) ~[HikariCP-5.0.1.jar:na]
        at com.zaxxer.hikari.pool.HikariProxyConnection.createStatement(HikariProxyConnection.java) ~[HikariCP-5.0.1.jar:na]
        at com.github.database.rider.core.dataset.DataSetExecutorImpl.handleConstraints(DataSetExecutorImpl.java:344) ~[rider-core-1.36.0.jar:na]
        at com.github.database.rider.core.dataset.DataSetExecutorImpl.disableConstraints(DataSetExecutorImpl.java:332) ~[rider-core-1.36.0.jar:na]
        at com.github.database.rider.core.dataset.DataSetExecutorImpl.clearDatabase(DataSetExecutorImpl.java:576) ~[rider-core-1.36.0.jar:na]
        at com.github.database.rider.core.dataset.DataSetExecutorImpl.createDataSet(DataSetExecutorImpl.java:108) ~[rider-core-1.36.0.jar:na]
        at com.github.database.rider.core.RiderRunner.runBeforeTest(RiderRunner.java:44) ~[rider-core-1.36.0.jar:na]
        at com.github.database.rider.junit5.DBUnitExtension.beforeTestExecution(DBUnitExtension.java:69) ~[rider-junit5-1.36.0.jar:na]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeTestExecutionCallbacks$5(TestMethodTestDescriptor.java:191) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeBeforeMethodsOrCallbacksUntilExceptionOccurs$6(TestMethodTestDescriptor.java:202) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeMethodsOrCallbacksUntilExceptionOccurs(TestMethodTestDescriptor.java:202) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeBeforeTestExecutionCallbacks(TestMethodTestDescriptor.java:190) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:136) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:68) ~[junit-jupiter-engine-5.9.2.jar:5.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) ~[junit-platform-engine-1.9.2.jar:1.9.2]
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128) ~[junit-platform-launcher-1.3.1.jar:1.3.1]
        at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invokeAllTests(JUnitPlatformProvider.java:150) ~[surefire-junit-platform-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.junitplatform.JUnitPlatformProvider.invoke(JUnitPlatformProvider.java:124) ~[surefire-junit-platform-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:384) ~[surefire-booter-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:345) ~[surefire-booter-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) ~[surefire-booter-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418) ~[surefire-booter-2.22.2.jar:2.22.2]
Caused by: com.mysql.cj.exceptions.ConnectionIsClosedException: No operations allowed after connection closed.
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
        at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.NativeSession.checkClosed(NativeSession.java:761) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:564) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        at com.mysql.cj.jdbc.ConnectionImpl.createStatement(ConnectionImpl.java) ~[mysql-connector-j-8.0.32.jar:8.0.32]
        ... 58 common frames omitted

銇┿仯銇°伀銇椼仸銈傘併儑銉笺偪銉欍兗銈规帴缍氥亴浣裤亪銇亸銇仯銇熴仺銇勩亞銇伅鍚屻仒銇с仚銇

Caused by: com.mysql.cj.exceptions.ConnectionIsClosedException: No operations allowed after connection closed.

瀹熻渚嬨伅mvn test銇т竴鎷疅琛屻仐銇︺亜銇俱仚銇屻佷粖鍥炪伄妲嬫垚銇犮仺銉嗐偣銉堛偗銉┿偣鍗樹綅銇с倐澶辨晽銇椼伨銇欍

銇ㄣ亜銇嗐倧銇戙仹銆@DirtiesContext銈€儙銉嗐兗銈枫儳銉銈掍娇銇c仧銇犮亼銇с儑銉曘偐銉儓銇犮仺Database Rider銇Spring Framework銈掍娇銇c仧銉嗐偣銉堛亴
涓嶅畨瀹氥伀銇倞銇俱仚銆

cacheConnection銈掔劇鍔广伀銇欍倠

銇撱伄瀵惧嚘鏂规硶銇с仚銇屻佹渶鍒濄伀鏇搞亜銇熴倛銇嗐伀cacheConnectionfalse銇仐銇︺儑銉笺偪銉欍兗銈规帴缍氥倰鍐嶅埄鐢ㄣ仚銈嬨亾銇ㄣ倰銈勩倎銇俱仚銆

src/test/resources/dbunit.yml

cacheConnection: false
properties:
  caseSensitiveTableNames: true

纰鸿獚銆

$ mvn test

銇撱亞銇欍倠銇ㄣ@DirtiesContext銈€儙銉嗐兗銈枫儳銉銈掍粯涓庛仐銇熴伨銇俱仹銈傘儐銈广儓銇垚鍔熴仚銈嬨倛銇嗐伀銇倞銇俱仚銆

[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

銇俱仺銈

銇ㄣ亜銇嗐倧銇戙仹銆丏atabase Rider銇Spring Framework銈掑悎銈忋仜銇︿娇銇c仧鏅傘伀銆@DirtiesContext銈€儙銉嗐兗銈枫儳銉銈掍娇銇嗐仺瑷畾銇倛銇c仸銇
銉忋優銈嬨仺銇勩亞瑭便仹銇椼仧銆

鏈鍒濄併儑銉笺偪銉欍兗銈规帴缍氥倰浣裤亜銇俱倧銇椼仸銇勩倠銇伀姘椼仴銇勩仸銇亱銇c仧銇仹銆佽В姹恒仚銈嬨伄銇仩銇勩伓鑻﹀姶銇椼伨銇椼仧鈥︺

瑷畾闋呯洰銇仭銈冦倱銇ㄨ銇︺亰銇嬨仾銇勩仺銇勩亼銇亜銇с仚銇