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.
@ContextConfigurationbut@SpringApplicationConfigurationinstead.@SpringApplicationConfigurationto my main project?@ContextConfigurationwith@SpringApplicationConfiguration.