4

I have a Gradle java web project being developed to run in Wildfly with the following structure:

- src/main/java
- src/main/resources
  \-- META/INF
      \-- persistence.xml
- src/test/java
- stc/test/resources
  \-- META/INF
      \-- persistence.xml

And the following build.gradle:

apply plugin: 'war'
apply plugin: 'eclipse-wtp'

sourceCompatibility = 1.8
version = '1.0'
jar {
    manifest {
        attributes 'Implementation-Title': 'Arch Project', 'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

configurations {
    provided
}

sourceSets {
    main.compileClasspath += configurations.provided
    test.compileClasspath += configurations.provided
    test.runtimeClasspath += configurations.provided
}

dependencies {

    /* Java EE 7 API. */
    provided 'javax:javaee-api:7.0'

    /* JUnit for unit testing. */
    testCompile 'junit:junit:4.1.2'

    /* DbUnit - Persitence unit testing. */
    testCompile 'org.dbunit:dbunit:2.5.0'
    testCompile 'org.slf4j:slf4j-simple:1.7.9'

    /* Hibernate. */
    testCompile 'org.hibernate:hibernate-core:4.3.7.Final'

    /* Hibernate provider for JPA. */
    testCompile 'org.hibernate:hibernate-entitymanager:4.3.7.Final'

    /* Hibernate dependency. */
    testCompile 'com.fasterxml:classmate:1.1.0'

    /* PostgreSQL for persistence tests context */
    testCompile 'org.postgresql:postgresql:9.3-1102-jdbc41'
} 

The persistence.xml file in the source folder src/test/resources is the one that I want to be used when I run my tests, instead the another from src/main/resources.
I need that because persistence.xml from src/main/resources relies on a JTA datasource defined in the Java EE application server, so, I believe it is not going to work on a local execution.
How can I configure that to run the tests successfully inside Eclipse and Gradle by the command gradle test?

Thanks in advance.

Bruno Gasparotto
  • 671
  • 12
  • 31
  • Does it help you somehow: http://forums.gradle.org/gradle/topics/resources_from_src_main_resources_not_included_for_test ? – Opal Feb 01 '15 at 08:59
  • Thx @Opal but it didn't help. If I use src/main/resources it works, the problem is when I want to use the `persistence.xml` from src/test/resources. – Bruno Gasparotto Feb 03 '15 at 15:35

1 Answers1

1

to resolve this problem follow these instructions:

In Eclipse's project properties, Java Build Path->Order and Export, put your test folder above your src folder.

as well as talk that another issue: Use different JPA.persistence.xml in Eclipse depending on production or test environment

Community
  • 1
  • 1
Clairton Luz
  • 2,116
  • 19
  • 15
  • "as well as talk that another issue:" could you please clarify what you mean? Do you mean "Please also follow the instructions given here"? But in any case there's more than one answer there, which one do you mean? I suppose [this one](http://stackoverflow.com/a/31923699/3982001) because it starts like yours, but still it is not clear. – Fabio says Reinstate Monica Sep 03 '15 at 16:56
  • Yours tests will work in eclipse if you put your test folder above your src folder. Click with rigth button in your Eclipse's project, properties, Java Build Path and go to tab Order and Export. there will be your their sources and resources than you move your src/test/resource to above your src/main/resource. So the Eclipse will found first the persistence.xml into test resource and the tests will work with test's persistence.xml in Eclipse's IDE. – Clairton Luz Sep 04 '15 at 10:49