2

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:

gradle clean build

Here is my 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.

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

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
Lucy101
  • 63
  • 1
  • 9
  • Can you add the result of "gradle dependencies" ? – olikaf Feb 07 '18 at 16:32
  • can you please elaborate? what do you mean by gradle dependencies? – Lucy101 Feb 07 '18 at 16:33
  • dependencies is a gradle target, like "gradle build". "gradle dependencies" displays the versions of the libraries used by your project – olikaf Feb 07 '18 at 16:38
  • The output is too long, are you sure you want me to post it? – Lucy101 Feb 07 '18 at 16:44
  • In fact only the test scope is needed. It starts with testRuntime - Runtime dependencies for source set 'test'. – olikaf Feb 07 '18 at 16:47
  • This is what I'm getting for that scope: testRuntimeOnly - Runtime only dependencies for source set 'test'. (n) No dependencies – Lucy101 Feb 07 '18 at 16:51
  • But you should have some dependencies ? Things like +--- fr.opensagres.xdocreport:fr.opensagres.xdocreport.itext.extension:1.0.3 | \--- com.lowagie:itext:2.1.7 +--- junit:junit:4.7 -> 4.12 ( Sorry no line return in comments... ) – olikaf Feb 07 '18 at 16:54
  • I updaded my post with a photo to the gradle dependencies – Lucy101 Feb 07 '18 at 17:03
  • 1
    Maybe duplicate of https://stackoverflow.com/questions/41761637/hibernate-5-2-7-java-lang-nosuchmethoderror-org-hibernate-engine-spi-sessionf – olikaf Feb 08 '18 at 09:59

1 Answers1

0

I faced this scenario today, In my case, the problem was I used Java 8 JDK for the eclipse project and my Gradle was pointing to my machine JDK 11. I changed into Java 8 from Java 11 and It worked

Sivaram Rasathurai
  • 5,533
  • 3
  • 22
  • 45