MySql is the most used open-source RDBMS in the world with different application development stacks. As nowadays most of the development of the system is TDD based, so for writing test cases for the MySql database will require running DB or we can use H2 database with MySql mode or we can even use Embedded my SQL for Unit and Integration testing.
After spending some time i found Wix Embedded MySql is a very handy and real MySQL embedded MySQL lib for unit and integration tests.
This can be included in projects very easily via Maven or Gradle build systems. This library downloads an embedded version of MySQL and using the Test code we configure, start the server, and stop the server.
For including in gradle
// https://mvnrepository.com/artifact/com.wix/wix-embedded-mysql
testImplementation group: 'com.wix', name: 'wix-embedded-mysql', version: '4.6.1'
For including in Maven
<!-- https://mvnrepository.com/artifact/com.wix/wix-embedded-mysql -->
<dependency>
<groupId>com.wix</groupId>
<artifactId>wix-embedded-mysql</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
You can check and change the latest version of the library.
How to Use Wix Embedded MySql?
After successfully including it in the project you need to configure and start MySql embedded server, for example, check the below code. I created one an abstract base class, which have @BeforeAll and @AfterAll static methos.
In this class, I created one Java Connection object for MYSQL after starting the server and in @AfterAll method, I am stoping the server.
In derived test classes you can use this connection object for MySQA integration test.
One can also create DataSource bean to test Spring and Hibernate projects here.