Spring Test在eclipse上成功但在gradle构建时失败

时间:2018-02-07 16:24:42

标签: java spring eclipse gradle build.gradle

我不知道为什么但是当我将它作为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

1 个答案:

答案 0 :(得分:0)

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