IllegalArgumentException:类 TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean 中没有可见的构造函数

2024-02-11

我正在尝试使用 h2 数据库为 Spring Boot JPA 应用程序编写集成测试。不知何故,TestEntityManager 没有被创建。我尝试在论坛上寻找一些帮助,但找不到任何相关信息。

感谢任何人可以提供帮助或提供一些指导。

Thanks.

我的代码如下:

存储库:

@Repository
public interface ConfigRepository extends JpaRepository<Config, Long> {

    Config findByKey(ConfigKey configKey);

}

配置:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "testEntityManagerFactory",
    transactionManagerRef = "testTransactionManager",
    basePackages = "com.abc.xyz.business.repository")
public class TestPersistenceConfig {

private static final String TEST_PERSISTENCE_UNIT = "emTestPersistenceUnit";

@Bean(name = "testEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws SQLException {
    LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.setPackagesToScan(new String[] { "nl.rabobank.rom.exception.manager.rom.em.business.domain" });
    entityManagerFactoryBean.setPersistenceUnitName(TEST_PERSISTENCE_UNIT);

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    entityManagerFactoryBean.setJpaVendorAdapter(vendorAdapter);
    entityManagerFactoryBean.setJpaProperties(additionalProperties());

    return entityManagerFactoryBean;
}

@Bean
@Profile("test")
public DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUrl("jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
    dataSource.setUsername("sa");
    dataSource.setPassword("sa");

    return dataSource;
}

@Bean(name = "testTransactionManager")
public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(emf);

    return transactionManager;
}

private Properties additionalProperties() {
    Properties hibernateProperties = new Properties();
    hibernateProperties.setProperty("em.hibernate.hbm2ddl.auto", "none");
    hibernateProperties.setProperty("hibernate.show_sql", "true");
    hibernateProperties.setProperty("hibernate.format_sql", "true");
    hibernateProperties.setProperty("em.hibernate.naming.physical-strategy", "org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl");
    hibernateProperties.setProperty("em.properties.hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
    return hibernateProperties;
}

}

Test:

@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles("test")
@ContextConfiguration(classes = {TestPersistenceConfig.class})
public class ConfigRepositoryTest {

    @Autowired
    private TestEntityManager entityManagerFactory;

    @Autowired
    private ConfigRepository configRepository;

    @Test
    public void test1() {
        Config config = 
    configRepository.findByKey(ConfigKey.PURGE_DATE_RETENTION_CALENDAR_TYPE);
        assertNotNull(config);
    }
}

POM.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope>
    </dependency>

    <!-- To run tests on IDE such as Eclipse, Intellij -->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-runner</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4-common</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc7</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

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

</dependencies>

堆栈跟踪:

java.lang.IllegalStateException: Failed to load ApplicationContext
...
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'TestExceptionHandlerEntityManagerFactory' defined in nl.rabobank.rom.exception.manager.rom.em.business.TestPersistenceConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalArgumentException: No visible constructors in class org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean
...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.IllegalArgumentException: No visible constructors in class org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579)
    ... 42 more
Caused by: java.lang.IllegalArgumentException: No visible constructors in class org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean
    at org.springframework.cglib.proxy.Enhancer.filterConstructors(Enhancer.java:666)
    at org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:567)

如果您在测试中有生产类型数据库,则必须添加@AutoConfigureTestDatabase(替换=替换.NONE)测试类。

只需将上述注释添加到 ConfigRepositoryTest 类即可解决您的问题。

@RunWith(SpringRunner.class)
@DataJpaTest
@ActiveProfiles("test")
@ContextConfiguration(classes = {TestPersistenceConfig.class})
@AutoConfigureTestDatabase(replace=Replace.NONE)
public class ConfigRepositoryTest {
  ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IllegalArgumentException:类 TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean 中没有可见的构造函数 的相关文章

  • 如何使用 Java 和 Selenium WebDriver 在 C 目录中创建文件夹并需要将屏幕截图保存在该目录中?

    目前正在与硒网络驱动程序和代码Java 我有一种情况 我需要在 C 目录中创建一个文件夹 并在该文件夹中创建我通过 selenium Web 驱动程序代码拍摄的屏幕截图 它需要存储在带有时间戳的文件夹中 如果我每天按计划运行脚本 所有屏幕截
  • 如何默认将 Maven 插件附加到阶段?

    我有一个 Maven 插件应该在编译阶段运行 所以在项目中consumes我的插件 我必须做这样的事情
  • Play框架运行应用程序问题

    每当我尝试运行使用以下命令创建的新 Web 应用程序时 我都会收到以下错误Play http www playframework org Error occurred during initialization of VM Could no
  • 制作一个交互式Windows服务

    我希望我的 Java 应用程序成为交互式 Windows 服务 用户登录时具有 GUI 的 Windows 服务 我搜索了这个 我发现这样做的方法是有两个程序 第一个是服务 第二个是 GUI 程序并使它们进行通信 服务将从 GUI 程序获取
  • 无法展开 RemoteViews - 错误通知

    最近 我收到越来越多的用户收到 RemoteServiceException 错误的报告 我每次给出的堆栈跟踪如下 android app RemoteServiceException Bad notification posted fro
  • Spark 1.3.1 上的 Apache Phoenix(4.3.1 和 4.4.0-HBase-0.98)ClassNotFoundException

    我正在尝试通过 Spark 连接到 Phoenix 并且在通过 JDBC 驱动程序打开连接时不断收到以下异常 为简洁起见 下面是完整的堆栈跟踪 Caused by java lang ClassNotFoundException org a
  • 控制Android的前置LED灯

    我试图在用户按下某个按钮时在前面的 LED 上实现 1 秒红色闪烁 但我很难找到有关如何访问和使用前置 LED 的文档 教程甚至代码示例 我的意思是位于 自拍 相机和触摸屏附近的 LED 我已经看到了使用手电筒和相机类 已弃用 的示例 但我
  • 斯坦福 NLP - 处理文件列表时 OpenIE 内存不足

    我正在尝试使用斯坦福 CoreNLP 中的 OpenIE 工具从多个文件中提取信息 当多个文件 而不是一个 传递到输入时 它会给出内存不足错误 All files have been queued awaiting termination
  • 从 127.0.0.1 到 2130706433,然后再返回

    使用标准 Java 库 从 IPV4 地址的点分字符串表示形式获取的最快方法是什么 127 0 0 1 到等效的整数表示 2130706433 相应地 反转所述操作的最快方法是什么 从整数开始2130706433到字符串表示形式 127 0
  • 如何将 pfx 文件转换为 jks,然后通过使用 wsdl 生成的类来使用它来签署传出的肥皂请求

    我正在寻找一个代码示例 该示例演示如何使用 PFX 证书通过 SSL 访问安全 Web 服务 我有证书及其密码 我首先使用下面提到的命令创建一个 KeyStore 实例 keytool importkeystore destkeystore
  • 为什么HashMap不能保证map的顺序随着时间的推移保持不变

    我在这里阅读有关 Hashmap 和 Hashtable 之间的区别 http javarevisited blogspot sg 2010 10 difference Between hashmap and html http javar
  • getResourceAsStream() 可以找到 jar 文件之外的文件吗?

    我正在开发一个应用程序 该应用程序使用一个加载配置文件的库 InputStream in getClass getResourceAsStream resource 然后我的应用程序打包在一个 jar文件 如果resource是在里面 ja
  • Java Integer CompareTo() - 为什么使用比较与减法?

    我发现java lang Integer实施compareTo方法如下 public int compareTo Integer anotherInteger int thisVal this value int anotherVal an
  • Eclipse Java 远程调试器通过 VPN 速度极慢

    我有时被迫离开办公室工作 这意味着我需要通过 VPN 进入我的实验室 我注意到在这种情况下使用 Eclipse 进行远程调试速度非常慢 速度慢到调试器需要 5 7 分钟才能连接到远程 jvm 连接后 每次单步执行断点 行可能需要 20 30
  • Google App Engine 如何预编译 Java?

    App Engine 对应用程序的 Java 字节码使用 预编译 过程 以增强应用程序在 Java 运行时环境中的性能 预编译代码的功能与原始字节码相同 有没有详细的信息这是做什么的 我在一个中找到了这个谷歌群组消息 http groups
  • Android 中麦克风的后台访问

    是否可以通过 Android 手机上的后台应用程序 服务 持续监控麦克风 我想做的一些想法 不断聆听背景中的声音信号 收到 有趣的 音频信号后 执行一些网络操作 如果前台应用程序需要的话 后台应用程序必须能够智能地放弃对麦克风的访问 除非可
  • 获取 JVM 上所有引导类的列表?

    有一种方法叫做findBootstrapClass对于一个类加载器 如果它是引导的 则返回一个类 有没有办法找到类已经加载了 您可以尝试首先通过例如获取引导类加载器呼叫 ClassLoader bootstrapLoader ClassLo
  • Firebase 添加新节点

    如何将这些节点放入用户节点中 并创建另一个节点来存储帖子 我的数据库参考 databaseReference child user getUid setValue userInformations 您需要使用以下代码 databaseRef
  • 捕获的图像分辨率太大

    我在做什么 我允许用户捕获图像 将其存储到 SD 卡中并上传到服务器 但捕获图像的分辨率为宽度 4608 像素和高度 2592 像素 现在我想要什么 如何在不影响质量的情况下获得小分辨率图像 例如我可以获取或设置捕获的图像分辨率为原始图像分
  • 当我从 Netbeans 创建 Derby 数据库时,它存储在哪里?

    当我从 netbeans 创建 Derby 数据库时 它存储在哪里 如何将它与项目的其余部分合并到一个文件夹中 右键单击Databases gt JavaDB in the Service查看并选择Properties This will

随机推荐

  • set_time_limit 不起作用

    我有一个bigintphp 中的类 用于计算大数 除了时间限制外 效果很好 我设置了时间限制 set time limit 900 在我的 bigint php 文件中 它在本地主机中工作 但在我的网络主机中 当我尝试计算 999 999
  • Angular 7:如何将文件/图像与我的反应式表单一起提交?

    我创建了带有文本输入的简单反应式表单 当提交表单时 我想传递文件输入中的图像 每次我用谷歌搜索时 我都会得到教程 它们向我展示如何上传文件 但它是在没有其他输入字段的情况下完成的 我知道如何做到这一点 但我不明白如何在一次提交中提交表单和文
  • 带 MonoTouch 的 sizeWithFont

    MonoTouch 中的 sizeWithFont 相当于什么 如果没有这样的方法 是否可以通过其他方式调用它 我想做的就是根据文本调整标签大小 下一个更新 MonoTouch 1 1 将提供 UIView StringSize strin
  • NSURLErrorDomain错误代码1002描述

    我是新来的iOS发展 我正在尝试加载一个JSON 这是我的功能 func loadmyJSON urlPath String let url NSURL NSURL string urlPath let session NSURLSessi
  • 从 SQLite 中的 DATETIME 获取月份

    我正在尝试从 a 中提取月份DATETIMESQLite 中的字段 month dateField 效果不太好strftime m dateStart 有任何想法吗 我不明白 答案就在你的问题中 select strftime m date
  • 如何通过 Websocket 发送文件以及附加信息?

    我正在开发一个 Web 应用程序 用于从管理界面将图像 视频等发送到两个显示器 我在服务器端使用 Node js 中的 ws 我已经实现了选择服务器上可用的图像和外部 URL 并将它们发送到客户端 但我也希望能够通过文件输入直接发送从设备中
  • 单选按钮单击和重新单击

    我在 Android 中有一个单选按钮组 选择项目时我会收到事件 目前为止还正常 但如果用户单击已选择的项目 我不会收到该事件 有没有办法知道 接收事件 用户何时点击单选按钮 无论它是否被选择 多谢 我不明白为什么当单击已选中的单选按钮时会
  • c++11 std::hash 函数对象类线程安全

    在 c 11 中声明的哈希函数类对象
  • 使用共享存储库模型的 GitHub 协作

    有人可以向我提供 GitHub 协作备忘单 供希望对存储库具有平等访问 权利的两人团队使用吗 我对使用分叉的必要性感到困惑 这对于开发人员分散的大型开源项目来说似乎是有意义的 但当我和我的搭档彼此相距 10 英尺时 似乎有点矫枉过正 Tha
  • Git:如何在同一分支中的提交之上重新建立分支? [复制]

    这个问题在这里已经有答案了 参考文献这个优秀的答案 https stackoverflow com a 38430972 3398271 我读过并且 我认为 我理解了 但据我所知并没有回答我的具体问题 因为它解释了如何重新基于提交中的提交
  • Selenium Firefox IDE 可以实现吗?

    这是这个问题的后续 想要创建一个表单填充程序 java jsp html 足够吗 https stackoverflow com questions 17329996 want to create a form filler is java
  • Dropbox Saver 可以接受来自 createObjectURL() 的数据吗?

    Dropbox Chooser 和 Saver 工具 https www dropbox com developers dropins对于这两项任务来说似乎非常酷 让用户从 Dropbox 中选择一个文件 页面就可以下载它 即 Dropbo
  • 块与 Objective-C 中的普通方法和函数有何不同?

    与 Objective C 中的普通方法和函数相比 使用块有什么优势 我已阅读文档 但找不到块而不是其他语言功能的具体用法 我确信我错过了一些东西 所以有人可以用比现有文档更简单的方式解释块的优点吗 块是一种包装一段代码并有效存储它以供以后
  • 将 Angularjs 和 Rails 应用程序分离为独立组件

    我想尝试一下 Angularjs 然而 我一直难以决定应该将我的角度应用程序放在哪里 我使用 Rails 框架作为后端 我看过教程 其中整个角度应用程序都位于 asset javascript 文件夹下 我想知道是否可以让它完全位于我的ra
  • Tailwind CSS:有没有办法定位下一个兄弟?

    我有一个带有如下标签的无线电输入 输入被隐藏 标签用于制作一个视觉上吸引人的圆圈供用户单击
  • receiveCompletion 出错时订阅取消

    在我的以 MVVM 模式设计的应用程序中 我有一个登录视图 如果存在网络或身份验证问题 登录可能会失败 我的目标是捕获错误并显示相应的警报 我为警报定义了枚举 如下所示 enum Alerts Identifiable var id Int
  • Application Insights 是否跟踪引荐来源网址?

    我想我到处查看和搜索 但找不到任何带有推荐流量数据的图表或报告 我使用的是在预览 Azure 门户中存储数据的最新版本 在旧的应用程序洞察中 即在 VS Online 中 有此数据 有谁知道在新门户中是否可以找到它以及在哪里可以找到它 提前
  • Centos 7 Postgres 服务的环境变量

    最近我遇到了使用自定义 PGDATA 路径启动 postgresql 服务的问题 它尝试查找未初始化的默认数据目录 var lib pgsql 9 3 data 因此触发了这些错误 问题似乎是 Centos 7 上的服务启动器删除了所有环境
  • 如何在 Android 上将文件从内部应用程序存储移动/重命名到外部存储?

    我正在从互联网下载文件并将流数据保存到我的应用程序内部存储中的临时文件中获取文件目录 http developer android com reference android content Context html getFilesDir
  • IllegalArgumentException:类 TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean 中没有可见的构造函数

    我正在尝试使用 h2 数据库为 Spring Boot JPA 应用程序编写集成测试 不知何故 TestEntityManager 没有被创建 我尝试在论坛上寻找一些帮助 但找不到任何相关信息 感谢任何人可以提供帮助或提供一些指导 Than