第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何嵌入內(nèi)存 MariaDB4j 以替換 JUnit 測(cè)試中的默認(rèn)

如何嵌入內(nèi)存 MariaDB4j 以替換 JUnit 測(cè)試中的默認(rèn)

翻翻過去那場(chǎng)雪 2022-06-08 17:13:08
我正在為此編寫Service使用多個(gè)數(shù)據(jù) Jpa 存儲(chǔ)庫的測(cè)試。問題是一些存儲(chǔ)庫使用大量具有MySQL特定功能的本機(jī)查詢,例如str_to_date(). 因此,當(dāng)我嘗試使用測(cè)試服務(wù)的方法時(shí),H2我收到一條錯(cuò)誤消息,指出 H2 無法識(shí)別功能。我曾嘗試在 MySQL 模式下使用 H2,但遇到了同樣的錯(cuò)誤。這里mariaDB4j 被提議作為一種解決方法。我已將依賴項(xiàng)添加到 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。我的測(cè)試文件看起來是這樣的:@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*/}該項(xiàng)目是使用注釋驅(qū)動(dòng)的 Spring Boot 構(gòu)建的。
查看完整描述

2 回答

?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

我構(gòu)建了以下類,我在每個(gè)需要數(shù)據(jù)庫訪問的集成測(cè)試中重用mariadb. 它可能會(huì)得到改進(jìn)(我很樂意提出建議),但到目前為止它仍然有效:


@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();

    }

}

應(yīng)用程序-junit.properties:


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

spring.datasource.username=root

spring.datasource.password=


查看完整回答
反對(duì) 回復(fù) 2022-06-08
?
慕虎7371278

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊

聽起來您需要明確聲明您的DataSourcefor 測(cè)試。在 h2 的情況下,可能已經(jīng)有一個(gè)由 spring 測(cè)試依賴項(xiàng)聲明的數(shù)據(jù)源 bean,但可能沒有由ch.vorburger.mariaDB4j.


這是我從互聯(lián)網(wǎng)上其他地方偷來的嵌入式 MariaDB 數(shù)據(jù)源的示例


import ch.vorburger.mariadb4j.DBConfigurationBuilder

import ch.vorburger.mariadb4j.springframework.MariaDB4jSpringService

import org.springframework.beans.factory.annotation.Value

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder

import org.springframework.context.annotation.Bean

import org.springframework.context.annotation.Configuration

import org.springframework.context.annotation.Profile


import javax.sql.DataSource


@Configuration

@Profile(['local', 'integrationTest'])

class EmbeddedMariaDbConfig {


    @Bean

    MariaDB4jSpringService mariaDB4jSpringService() {

        new MariaDB4jSpringService()

    }


    @Bean

    DataSource dataSource(MariaDB4jSpringService mariaDB4jSpringService,

                          @Value('${app.mariaDB4j.databaseName}') String databaseName,

                          @Value('${spring.datasource.username}') String datasourceUsername,

                          @Value('${spring.datasource.password}') String datasourcePassword,

                          @Value('${spring.datasource.driver-class-name}') String datasourceDriver) {

        //Create our database with default root user and no password

        mariaDB4jSpringService.getDB().createDB(databaseName)


        DBConfigurationBuilder config = mariaDB4jSpringService.getConfiguration()


        DataSourceBuilder

                .create()

                .username(datasourceUsername)

                .password(datasourcePassword)

                .url(config.getURL(databaseName))

                .driverClassName(datasourceDriver)

                .build();

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-06-08
  • 2 回答
  • 0 關(guān)注
  • 614 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)