I am trying to run tests in my Spring application which need to use an embedded h2 database (instead of the sql database I use for the actual application). The problem I was facing was that it is executing all queries on the h2 database in upper case and it was failing because of that.
org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "MOOD" not found; SQL statement:
insert into mood
Now I figured out in application properties you can just put DATABASE_TO_LOWER=TRUE in the connection string, but this is not working for me. I can clearly see in the output that is not using the url I am defining in application-test.properties:
Starting embedded database: url='jdbc:h2:mem:85afe142-af28-4c3a-8226-0a77abdb004d;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
So the relevant files:
my application-test.properties:
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY-2;DATABASE_TO_LOWER=TRUE
spring.datasource.username=sa
spring.datasource.password=sa
my test class:
package com.example.demo.persistence.interfaces;
import com.example.demo.persistence.dto.Mood;
import com.example.demo.persistence.dto.MoodType;
import com.example.demo.persistence.dto.Patient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
import java.util.Calendar;
import java.util.Collection;
import static org.junit.jupiter.api.Assertions.*;
@DataJpaTest
@ActiveProfiles("test")
@TestPropertySource(locations="classpath:application-test.properties")
@EnableConfigurationProperties
class MoodRepositoryTest {
@Autowired
private TestEntityManager entityManager;
@Autowired
private MoodRepository repository;
@Autowired
private PatientRepository patientRepository;
@Test
void getMoodsByPatient() {
final Patient patient = new Patient("Kees", "spek", "straat", Calendar.getInstance().getTime());
final Patient patient2 = new Patient("Kees2", "spek2", "straat2", Calendar.getInstance().getTime());
final Mood mood1 = new Mood(patient, MoodType.GOOD,"","","","","","","",Calendar.getInstance().getTime());
final Mood mood2 = new Mood(patient, MoodType.GOOD,"","","","","","","",Calendar.getInstance().getTime());
final Mood mood3 = new Mood(patient2, MoodType.GOOD,"","","","","","","",Calendar.getInstance().getTime());
entityManager.persist(patient);
entityManager.persist(patient2);
entityManager.persist(mood1);
entityManager.persist(mood2);
entityManager.persist(mood3);
final int expectedMoodsFound = 2;
final int actualMoodsFound = ((Collection<?>)repository.getMoodsByPatient(patient)).size();
assertEquals(expectedMoodsFound,actualMoodsFound);
}
@Test
void getMoodByMoodId() {
}
@Test
void deleteAllByDateBefore() {
}
}
So my problem is, I can't seem to properly load in the application-test.properties I guess? What am I doing wrong? I read so many different things on stackoverflow and other websites and none of them worked and I feel like it's a fairly simple issue.
CodePudding user response:
The @DataJpaTest
annotation uses the meta annotation @AutoConfigureTestDatabase
which has the default configuration to replace any manually configured or auto-configured datasource:
public @interface AutoConfigureTestDatabase {
/**
* Determines what type of existing DataSource bean can be replaced.
* @return the type of existing DataSource to replace
*/
@PropertyMapping(skip = SkipPropertyMapping.ON_DEFAULT_VALUE)
Replace replace() default Replace.ANY;
/**
* What the test database can replace.
*/
enum Replace {
/**
* Replace the DataSource bean whether it was auto-configured or manually defined.
*/
ANY,
/**
* Only replace the DataSource if it was auto-configured.
*/
AUTO_CONFIGURED,
/**
* Don't replace the application default DataSource.
*/
NONE
}
}
If you want to define the database configuration on your own, you can opt-out using:
// ... existing annotations
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
class MoodRepositoryTest {
// ...
}