У меня есть менеджер сущностей в родительском тестовом классе
@RunWith(SpringRunner.class)
@SpringBootTest(
properties = {
"spring.profiles.active=test",
"spring.config.name=app-test"})
public abstract class ViewerTestBase extends DbBuilderImpl {
@Autowired EntityManager em;
Менеджер сущностей здесь в порядке. DbBuilder устанавливает тестовые данные.
В @repository это нуль
@Repository public class PaymentTransactionDao {
@Autowired EntityManager em;
Причинение сбоя контрольного примера.
Менеджер сущностей сопоставляется с базой данных h2 для тестов.
Класс конфигурации персистентности - плита котла
@Configration
@EnableTransactionManagement
public class PersistenceConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
LocalContainerEntityManagerFactoryBean em = builder.dataSource(viewerDataSource())
.packages("viewer.model")
.persistenceUnit("viewer")
.build();
return em;
}
@Bean
public PlatformTransactionManager transactionManager(
EntityManagerFactory viewerEntityManagerFactory) {
return new JpaTransactionManager(pspEntityManagerFactory);
}
@Bean
@Primary
@ConfigurationProperties(prefix = "viewer.dbo.datasource")
public DataSource viewerDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "viewer.auth.datasource")
public DataSource authDataSource() {
return DataSourceBuilder.create().build();
}
Настройка с помощью пружинного стартера jpa
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
compile("org.springframework.boot:spring-boot-devtools")
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'com.h2database', name: 'h2'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile group: 'org.springframework.boot', name: 'spring-boot-test'
testCompile group: 'org.springframework.boot', name: 'spring-boot-test-autoconfigure'