1

Is there any way to use MySQL for JUnit testing with Spring?

I ask this because I have a lot of store procedures in MYSQL and I have noticed that H2 is not working with SPs.

Edit:

Right now my configuration for src/main looks like:

@ComponentScan
@EnableAutoConfiguration
@EnableJpaRepositories
public class Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext =  SpringApplication.run(Application.class, args);
    }
}


spring.datasource.url=jdbc:mysql://localhost/mydb
spring.datasource.username=user
spring.datasource.password=pw
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Spring works fine with this configuration. It reads the application.properties, sees the spring.datasource.* and knows that I have set a datasource.

Everything is fine here.

Now, in tests I use something like:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {Application.class})
@Transactional
public class TxServiceTest {

}

This is not going to run the tests against MySQL database. It will run the tests with H2 database. I need to use MySQL for testing because I am using many stored procedures.

I have tried many other combinations but none of them works. What am I missing here?

Possible fix:

I somehow found a way to make it work. I let the main project unchanged and inside my test project I added a new class:

@Configuration
public class PersistenceContext {

    @Bean
    public DataSource dataSource() {
        org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost/mydb?noAccessToProcedureBodies=true");
        dataSource.setUsername("xx");
        dataSource.setPassword("yy");
        return dataSource;
    }

}

I don't like this solution too much but it is working.

3
  • 1
    The problem is your test, you shouldn't be using @ContextConfiguration but @SpringApplicationConfiguration instead. Commented Mar 7, 2015 at 12:04
  • Should I point @SpringApplicationConfiguration to my main project? Commented Mar 7, 2015 at 12:05
  • 1
    Well I would suspect that your tests are already pointing to that, else what the heck are you testing. Just replace @ContextConfiguration with @SpringApplicationConfiguration. Commented Mar 7, 2015 at 12:07

1 Answer 1

1

Sounds like Integration Test. First, make sure that your database is always up-to-date in order to avoid restores and work in common errors. You can use liquibase or flyway to manage deltas. Also, make sure that after execution of the tests the state of the database has not been altered. I mean if you have a table without data at the beginning, after the execution of your test this table must not have data. You can use dbunit or spring-dbunit which is an extension of dbunit for spring to manage this.

In order to use a local mysql database you can use this plugin http://mysql.jcabi.com/

EDIT 01

Replace your h2 dataSource bean:

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/demo" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>
Sign up to request clarification or add additional context in comments.

10 Comments

Isn't there any way to just set to testing to run with the dataSource pointing to MySQL database? I mean doing it the same way as we do in our application (not in test).
Sure, you can. My approach was based on a Continuous Integration process.
Can you, please, give me a some hints on how to do it? I don't need continuous integration at this point and the way it works in my Application doesn't work in the tests too.
Do you mean how to write this unit test? Check this link about testing in spring docs.spring.io/spring/docs/current/spring-framework-reference/…
I wrote unit tests and it is working but with H2 database by default. I want to make it work with MySQL. I tried to remove the H2 from my gradle.build but it is still looking after it, even if I provide something similar for dataSource with the configuration used in my Apllication.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.