【问题标题】:Spring-Data h2: java.io.FileNotFoundException when testing in mavenSpring-Data h2:在 Maven 中测试时出现 java.io.FileNotFoundException
【发布时间】:2017-09-15 23:38:42
【问题描述】:

我在将 spring 与 h2 结合使用时遇到问题。我使用 h2 数据库进行测试,但我的行为非常奇怪。

我的依赖:

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

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
...

我将我的测试的 jdbc 连接字符串设置为:

spring.datasource.url=jdbc:h2:mem:dataSource

不考虑基本测试:

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {
  @Test
  public void contextLoads() {
  }
}

如果我跑步

mvn 测试

在我的 pom 所在的同一目录中,一切正常。但是如果在父目录执行这个命令

mvn -f ./xxx/pom.xml 测试

测试没有成功。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: Unable to resolve persistence unit root URL

如何在父目录中运行测试?

【问题讨论】:

    标签: java spring maven spring-data h2


    【解决方案1】:

    试试 mvn -f xxx/pom.xml 测试

    【讨论】:

    • 你能给我解释一下为什么会这样吗?表示不一样吗?
    • ./ 表示当前文件夹的语法在较旧的 java 中得不到很好的支持,并且经常会导致错误。造成这种情况的一个潜在原因是使用 1.4 或以前的 Ant 模式匹配使用,具体取决于您的 maven 版本和 JDK。没有前导 / 的 -f 参数假定您正在提供具有默认模式匹配器的相对路径。将来只需尝试不同的变化,它应该可以工作。
    猜你喜欢
    • 2017-07-15
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2016-12-24
    • 2016-12-24
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多