【问题标题】:Spring Test succeed on eclipse but fail on gradle buildSpring Test 在 eclipse 上成功,但在 gradle build 上失败
【发布时间】:2018-07-18 00:39:25
【问题描述】:

我不知道为什么,但是当我以 junit 运行它时,我的 spring boot 测试在 eclipse 中成功,但是当我执行命令时失败:

gradle clean build

这是我的 build.gradle:

buildscript {
    ext {
        springBootVersion = '1.5.8.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

jar {
    baseName = rootProject.name
    version =  project.version
}

repositories {
    mavenCentral()
}



dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework:spring-jdbc')
    compile('mysql:mysql-connector-java')
    compile('javax.validation:validation-api')
    compile('org.hibernate:hibernate-core:5.2.2.Final')
    compile('org.hibernate:hibernate-tools:5.2.0.Final')
    testCompile('com.h2database:h2:1.4.196')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('org.springframework.security:spring-security-test')

}

一旦我运行 gradle 命令,我的所有测试都会失败,原因有很多,主要是无法创建 bean 并将它们自动连接到测试类。

这是一个测试示例:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@DataJpaTest
@ActiveProfiles("test")
public class IEmployeeRepositoryTest {

@Autowired
private TestEntityManager entityManager;

@Autowired
private IEmployeeRepository employeRepository;

这就是构建/测试失败的原因:

Caused by: 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 
java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties;

感谢我能得到的所有帮助,我已经为此困扰了好几个小时。

更新 这是我的 gradle 依赖项的输出: Image to gradle dependencies

我还要提一下,项目结构是这样的:

src
---main
-----java
-----resources
------application.yml
------schema.sql
---test
-----java
-----resources
------application-test.yml
------schema-test.sql

【问题讨论】:

  • 可以添加“gradle依赖”的结果吗?
  • 您能详细说明一下吗? gradle 依赖是什么意思?
  • dependencies 是一个 gradle 目标,例如“gradle build”。 “gradle dependencies”显示你的项目使用的库的版本
  • 输出太长,确定要我发吗?

标签: java spring eclipse gradle build.gradle


【解决方案1】:

我今天遇到了这种情况,就我而言,问题是我在 eclipse 项目中使用了 Java 8 JDK,而我的 Gradle 指向我的机器 JDK 11。我从 Java 11 更改为 Java 8 并且它工作正常

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 2014-01-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 2020-01-24
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多