Separation of core and test in gradle eclipse builds

Today I tried to switch the project with integration tests from maven to gradle. Everything worked perfectly, except that I have a serious problem with testng.

The project uses hibernate / JPA2 to access the database and has several tests that depend on the persistence unit in the / resources / META -INF / persistence.xml test. When I run the test suite with gradle, everything works fine. But when I run xml (or any test class on its own) from eclipse, it seems like it is trying to use main / resources / META-INF / persistence.xml.

Since I do most of my work using TDD, I really need to run / debug tests from eclipse. When I add a persistence block to the persistence.xml file, it works (it even gets some other resources from the "test" directory). This would be a workaround, but I really don't like the idea of ​​adding test resources to "main / resources"

The same project works fine when I import it using the old pom.xml from maven.

build.gradle

apply plugin: 'java' apply plugin: 'eclipse' sourceCompatibility = 1.7 version = '0.1' jar { manifest { attributes 'Implementation-Title': 'md', 'Implementation-Version': version } } repositories { mavenCentral() } dependencies { compile 'org.slf4j:slf4j-api:1.7.5' compile 'com.google.guava:guava:14.0.1' compile 'org.hibernate:hibernate-entitymanager:4.2.2.Final' compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.1.Final' testCompile 'ch.qos.logback:logback-classic:1.0.13' testCompile 'org.testng:testng:6.8.5' testCompile 'org.dbunit:dbunit:2.4.9' testCompile 'org.mockito:mockito-all:1.9.5' testCompile 'org.easytesting:fest-assert-core:2.0M10' testCompile 'org.hsqldb:hsqldb:2.2.9' } test { useTestNG(){ suites 'src/test/resources/testng.xml' } } 

Update:

The generated classpath file is as follows:

 <?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src/main/java"/> <classpathentry kind="src" path="src/main/resources"/> <classpathentry kind="src" path="src/test/java"/> <classpathentry kind="src" path="src/test/resources"/> <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> <classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/> <classpathentry kind="output" path="bin"/> </classpath> 

It seems to combine everything into a bin folder and makes no distinction between the main and the test, like the .classpath file created by maven.

+6
source share
2 answers

The problem was that the gradle eclipse plugin merges the test and main folders by default. Therefore, persistence.xml from main redefined the test version.

Adding the following code will solve the problem by changing the output directories of the generated classes and deleting the entry with the default output.

 import org.gradle.plugins.ide.eclipse.model.SourceFolder eclipse.classpath.file { beforeMerged { classpath -> classpath.entries.clear() } whenMerged { cp -> cp.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/main/") }*.output = "bin/main" cp.entries.findAll { it instanceof SourceFolder && it.path.startsWith("src/test/") }*.output = "bin/test" cp.entries.removeAll { it.kind == "output" } } } 

Update: Corresponding class path entries after the change.

 <classpathentry output="bin/main" kind="src" path="src/main/java"/> <classpathentry output="bin/main" kind="src" path="src/main/resources"/> <classpathentry output="bin/test" kind="src" path="src/test/java"/> <classpathentry output="bin/test" kind="src" path="src/test/resources"/> 
+12
source

Set the default output folder for 'build' and check the output folder for 'build-test' (tested with Gradle 2.7):

 eclipse.classpath { defaultOutputDir = file('build') file.withXml { n -> n.asNode().classpathentry.findAll { it.@path.startsWith ('src/test') } .each { it.@output = 'build-test' } } } 

The resulting .classpath file entries:

 <classpathentry kind="output" path="build"/> <classpathentry kind="src" path="src/main/java"/> <classpathentry kind="src" path="src/main/resources"/> <classpathentry kind="src" path="src/test/java" output="build-test"/> <classpathentry kind="src" path="src/test/resources" output="build-test"/> 
+1
source

Source: https://habr.com/ru/post/948636/


All Articles