Is there a way to run MySQL in-memory for JUnit test cases?

前端 未结 5 1576
抹茶落季
抹茶落季 2020-12-02 07:01

I\'m just trying to add test cases for services accessing a MySQL DB, and I would like to recreate the whole schema (and for some scenarios also just use a MySQL dump file w

5条回答
  •  温柔的废话
    2020-12-02 07:21

    We use MySQL and flyway to handle the migration.

    For unit testing and simple integration tests, we use the H2 in-memory database with the MODE=MySQL param. Mode=MySQL enables the H2 DB to handle most of the MySQL dialect.

    Our test datasource in the Spring config is set up like this:

    
        
        
    
    

    (If you don't know Spring - the XML translates into calling new BasicDataSource and then call setDriverClassName and setUrl on the instance created)

    Then we use Flyway on the datasource to create the schema and read in like we would against a regular MySQL DB:

    
        
        
        
        
    
    

    You could also just use the dataSource bean in a jdbcTemplate and run some SQL scripts that way or run a number of MySQL scripts using the tag.

提交回复
热议问题