Не запускаются тесты JDBC c бд H2
Пишу Spring MVC приложение. В качестве основной БД использую PostgreSQL, но для тестов решил поднять базу Н2. Для тестов используется отдельная конфигурация Spring. С основной базой мои сервисы работают, а с тестовой нет. Не могу понять в чём причина. Ошибка наверняка незначительная, но опыта чтобы её найти не хватает.
Dependencies
dependencies {
//Spring
compile group: 'org.springframework', name: 'spring-core', version: '5.2.5.RELEASE'
compile group: 'org.springframework', name: 'spring-beans', version: '5.2.5.RELEASE'
compile group: 'org.springframework', name: 'spring-context', version: '5.2.5.RELEASE'
compile group: 'org.springframework', name: 'spring-jdbc', version: '5.2.5.RELEASE'
//Data base
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.12'
testCompile group: 'com.h2database', name: 'h2', version: '1.4.200'
//Tests
testCompile group: 'org.mockito', name: 'mockito-core', version: '3.3.3'
testCompile group: 'org.springframework', name: 'spring-test', version: '5.2.5.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.13'
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.2'
testConfig
package com.johnpank.notestorage.config;
import com.johnpank.notestorage.dao.NoteDAO;
import com.johnpank.notestorage.dao.NoteDAOImpl;
import com.johnpank.notestorage.dao.UserDAO;
import com.johnpank.notestorage.dao.UserDAOImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import java.util.Objects;
@Configuration
@ComponentScan("com.johnpank.notestorage")
@PropertySource("classpath:appProperties.properties")
public class TestAppConfig {
@Autowired
Environment env;
@Bean
public DataSource testDataSource(){
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(Objects.requireNonNull(env.getProperty("test.db.driver")));
dataSource.setUrl(env.getProperty("test.db.url"));
dataSource.setUsername(env.getProperty("test.db.userName"));
dataSource.setPassword(env.getProperty("test.db.password"));
return dataSource;
}
@Bean
UserDAO testUserDao(){
return new UserDAOImpl(testDataSource());
}
@Bean
NoteDAO testNoteDao(){
return new NoteDAOImpl(testDataSource());
}
}
Конфиг самого приложения отличается только названием полей properties.
test
package com.johnpank.notestorage.dao;
import com.johnpank.notestorage.config.TestAppConfig;
import com.johnpank.notestorage.models.Note;
import com.johnpank.notestorage.models.User;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Date;
import static org.junit.Assert.*;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = TestAppConfig.class, loader = AnnotationConfigContextLoader.class)
public class UserDAOImplTest {
@Autowired
private UserDAO testUserDao;
// @Before
// public void setUp(DataSource dataSource) throws Exception {
// userDao = new UserDAOImpl(dataSource);
// }
//
// @After
// public void tearDown() throws Exception {
// userDao = null;
// }
@Test
public void createUser() {
User user = new User();
user.setUser_id(1);
user.setLogin("logiiin");
user.setPassword("passsssssword");
user.setEmail("eeeeeeemaillll");
user.setReg_date(new Date());
user.setNotes(new ArrayList<Note>());
int result = testUserDao.createUser(user);
assertEquals(1,result);
}
@Test
public void getUserById() {
}
@Test
public void getUserByLogin() {
}
@Test
public void updateUser() {
}
@Test
public void deleteUser() {
}
}
properties
#DataBase
db.driver = org.postgresql.Driver
db.url = jdbc:postgresql://localhost:5432/note_storage_db
db.userName = qqq
db.password = qqq
test.db.driver = org.h2.Driver
test.db.url = jdbc:h2:mem:default
test.db.userName = 1
test.db.password = 1
Результат
Testing started at 21:49 ...
> Task :cleanTest
> Task :compileJava UP-TO-DATE
> Task :processResources UP-TO-DATE
> Task :classes UP-TO-DATE
> Task :compileTestJava UP-TO-DATE
> Task :processTestResources UP-TO-DATE
> Task :testClasses UP-TO-DATE
> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [com.johnpank.notestorage.dao.UserDAOImplTest](filter.includeTestsMatching)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 0s
6 actionable tasks: 2 executed, 4 up-to-date