简体   繁体   English

Spring Test 在 Eclipse 上成功,但在 gradle build 上失败

[英]Spring Test succeed on eclipse but fail on gradle build

I don't know why but my spring boot test succeed in eclipse when running it as junit, but then fail when I execute the command:我不知道为什么,但是当我将它作为junit运行时,我的spring boot测试在eclipse中成功了,但是当我执行命令时失败了:

gradle clean build

Here is my build.gradle:这是我的 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')

}

All my test fails once I run the gradle command, for various reasons, mainly not being able to create beans and autowire them to the test classes.一旦我运行 gradle 命令,我的所有测试都会失败,原因有很多,主要是无法创建 bean 并将它们自动连接到测试类。

Here is a test example:这是一个测试示例:

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

@Autowired
private TestEntityManager entityManager;

@Autowired
private IEmployeeRepository employeRepository;

And this is the reason why the build/test fails:这就是构建/测试失败的原因:

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;

I appreciate all the help I can get, I've been stuck on this for hours.我很感激我能得到的所有帮助,我已经坚持了几个小时。

UPDATE This is the output of my gradle dependencies: Image to gradle dependencies更新这是我的 gradle 依赖项的输出:图像到 gradle 依赖项

I should also mention that project structure is this:我还应该提到项目结构是这样的:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM