5

I am attempting to build a Spring Boot application, trying to stick to test driven development. My issue is that I have Spring Boot JPA included in my project but don't actually have a data source set up yet. Before adding the dependency, I was able to run my unit tests successfully.

Now that I have added the dependency, even attempting to execute my unit tests fails because it is unable to initialize a datasource for Spring Data.

I'm rather new to JUnit, Spring Boot and Mockito though. I want to be able to run my unit tests without actually having a datasource and instead mocking all of my repositories.

What is the proper way to do this?

Roman C
  • 49,761
  • 33
  • 66
  • 176
c.dunlap
  • 600
  • 1
  • 11
  • 28
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – Roman C Jun 10 '16 at 20:06
  • This link might be useful for you: http://stackoverflow.com/questions/34756264/spring-boot-with-datasource-when-testing – ritesh.garg Jun 10 '16 at 20:54
  • Make a separate profile for test suites and disable auto-configuration for JPA. – Vaelyr Jun 10 '16 at 21:09
  • You can also execute some tests (Controllers, Services) using the `MockitoRunner` instead of the Spring Runner. This will not instantiate a whole context but just inject dependencies as Mocks. This also helps speeding up the tests. We usually onyl use the SpringRunner for Integration Tests. – daniel.eichten Jun 11 '16 at 18:08

2 Answers2

11

Step one. Add to pom.xml some kind of in-memory database. For example h2:

<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <version>1.4.191</version>
  <scope>test</scope>
</dependency>

Then configure test datasource in your test application.properties located in (src/test/resources):

spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect

spring.datasource.platform=h2
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:test;MODE=MySQL;IGNORECASE=TRUE
spring.datasource.username=SA
spring.datasource.password=

This is just example for running h2 in-memory with mysql support mode. But you may use other modes (sql support) or even do not set this parameter at all.

Konstantin Konyshev
  • 1,026
  • 9
  • 18
10

If you define some SQL engine commonly used for testing (e.g. HSQL, Derby or H2), Spring Boot should recognize it as test dependency and configure Datasource bean on top of it. In order to do that, just define such engine with test scope:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>

Problem will arise when you introduce production Datasource (e.g. Postgres or MySQL). At that state you would need to

  • configure testing Datasource explicitly with @Primary annotation
  • or provide testing configuration file (e.g. src/test/resources/application.properties) where H2 will be configured.
luboskrnac
  • 23,973
  • 10
  • 81
  • 92