Gradle compilation issue when running tests

Hi All,

I have strange issue with gradle. When I run my tests via IDE everything is working I have no errors, no compilation issues. However when I try to run gradlew test I have compilation error:

SpringDatabaseConfig.java:34: error: cannot find symbol
  lcemfb.setPackagesToScan("xxx");
        ^
  symbol:
 method setPackagesToScan(String)
  location: variable lcemfb of type LocalContainerEntityManagerFactoryBean
1 error

This is part of the class that is causing the issue

public class SpringDatabaseConfig {
   @Bean
 public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
  LocalContainerEntityManagerFactoryBean lcemfb = new LocalContainerEntityManagerFactoryBean();
  lcemfb.setDataSource(dataSource());
  lcemfb.setPackagesToScan("xxx");// here is compilation error
  lcemfb.setPersistenceUnitName("TestPU");
  HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();
  lcemfb.setJpaVendorAdapter(va);
  Properties ps = new Properties();
  ps.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
  ps.put("hibernate.hbm2ddl.auto", "create");
  lcemfb.setJpaProperties(ps);
  return lcemfb;
 }

I don’t have any idea what can be wrong. This is my environment:

Gradle 1.11
------------------------------------------------------------
  Build time:
 2014-02-11 11:34:39 UTC
Build number: none
Revision:
   a831fa866d46cbee94e61a09af15f9dd95987421
  Groovy:
     1.8.6
Ant:
        Apache Ant(TM) version 1.9.2 compiled on July 8 2013
Ivy:
        2.2.0
JVM:
        1.7.0_45 (Oracle Corporation 24.45-b08)
OS:
         Mac OS X 10.9.2 x86_64

This is my build.gradle for this project:

repositories {
 mavenCentral()
}
  dependencies {
 testCompile 'junit:junit:4.11'
 testCompile 'org.springframework:spring-jpa:2.0.8'
 testCompile 'org.springframework:spring-test:4.0.1.RELEASE'
 testCompile 'hsqldb:hsqldb:1.8.0.10'
 testCompile 'org.mockito:mockito-all:1.8.4'
 testCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
}
+ idea plugin and java

Thanks in advance for any tips or suggestions. Best Regards.

AFAIK, spring-jpa is an obsolete library. You probably want spring-orm:4.0.1-RELEASE: the spring jar containing JPA support for the version 4.0.1 of Spring (the same as the one you use for tests)

After changing this dependency indeed it work. Thanks for help. There is though still one thing that I don’t understand. Why IDE was able to compile the code but gradle wasn’t?

Best Regards

Because the IDE put the correct spring jar file before the incorrect one in the classpath, whereas Gradle put the incorrect one before the correct one.