Sebastian Saip Sebastian Saip - 1 year ago 42
MySQL Question

Best way to integration-test a spring mysql application?

I want to write integration tests for my application, which is using a MySQL db via jdbc.
(FYI: The application has a user database and a wiki so far, so nothing sophisticated really)

I came across a lot of posts, mostly talking about HSQLDB or H2, as well as some exotics or discontinued (like mxj)

Sadly, most of these threads were made years ago and so many things have changed since then.
I'm looking for people with recent experience (or even better having a similiar setup like I got - that means spring and the need to run these tests both locally and on jenkins)!

I understand that the best way to find the answer is to try it myself and see what works best, but maybe someone has recent experience and is willing to share :)

Technologies used:

  • MySQL

  • Spring

  • TestNG

  • Jenkins

I'd be very thankful if you can share your experience and advices with me.


So I decided to use h2, which gave me some problems with importing MySQL dumbs. The answer is here: Running h2 in MODE=MySQL doesn't support MySQL dumps

Basically, you have to remove quotes around table-names (they seem to work fine for fields nevertheless). You can use back-ticks ( ` ) or not use any ticks/quotes at all. Another thing that caused a problem was a "AUTO_INCREMENT = 1" at the end of the table definition. Since h2 uses 1 as default after the start, this command is not needed anyways.

Here's an example how to setup the embedded database:

public class InMemoryTest {
    @org.testng.annotations.Test // ofc this can be JUnit @Test as well
    public void test() throws Exception {

        Connection conn = DriverManager.
                getConnection("jdbc:h2:mem:test;MODE=MySQL;IGNORECASE=TRUE;INIT=RUNSCRIPT FROM 'src/test/resources/test.sql'");

        Statement stat = conn.createStatement();
        stat.execute("INSERT INTO `usr_avatar` (\"usr_avatar_user_id\") VALUES (1)");

and the test.sql would be:

  "usr_avatar_id" int(11) NOT NULL AUTO_INCREMENT,
  "usr_avatar_user_id" int(11) NOT NULL