どのようにJUnitテストにデフォルト春DataSourceを置き換えるためにメモリMariaDB4jに埋め込むには?

BurningPapaya:

以下のために私がテストを書いていますServiceそれはいくつかのデータJPAリポジトリを使用しています。問題は、いくつかのリポジトリが持つネイティブクエリの多くを使用することであるMySQLような特定の機能をstr_to_date()私が使用してサービスのメソッドをテストしようとしたときにH2、私はH2が関数を認識しないというエラーが発生しました。私は、MySQLのモードでH2を使用してみましたが、同じエラーを持っています。

ここ mariaDB4jは、回避策として提案されました。私はMavenのに依存関係を追加しました

<dependency>
    <groupId>ch.vorburger.mariaDB4j</groupId>
    <artifactId>mariaDB4j</artifactId>
    <version>2.3.0</version>
    <scope>test</scope>
</dependency>

しかし取得IllegalStateException : Failed to replace DataSource with an embedded database for tests. If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoConfigureTestDatabase

私のテストファイルには、このようになります。

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY)
public class TestPay {

    @TestConfiguration
    static class PaymentServiceTestContextConfiguration {
        @Bean
        public PaymentService paymentService(){
            return new PaymentService();
        }
    }

    @Autowired
    private PaymentService paymentService;
    @Autowired
    private TarifRepository firstRepository;
    @Autowired
    private BuildingRepository secondRepository;
    @Autowired
    private ApartmentRepository thirdRepository;

    /* Test cases here*/
}

このプロジェクトは、アノテーション駆動春ブーツとビルドです。

membersound:

私は、データベースへのアクセスを必要とするすべての統合テストで再利用することを、次のクラスを構築しますmariadbそれはおそらく改善することができた(と私は提案のために幸せになると思います)が、それは今のところ動作します:

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations="classpath:application-junit.properties")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) //otherwise mariadb is not cleaned up between tests
public abstract class MyIntegrationTest {

    private static MariaDB4jSpringService DB;

    @BeforeClass
    public static void init() throws ManagedProcessException {
        DB = new MariaDB4jSpringService();
        DB.setDefaultPort(1234);
        DB.start();
        DB.getDB().createDB("yourtables");
        DB.getDB().source("schema.sql"); // init scripts from /src/test/resources/schema.sql
    }

    @AfterClass
    public static void cleanup() {
        if (DB != null) DB.stop();
    }
}

application-junit.properties:

spring.datasource.url=jdbc:mariadb://localhost:1234/yourtables
spring.datasource.username=root
spring.datasource.password=

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=206080&siteId=1