pact-jvm-junit5 writes all pacts to target/pacts instead of $buildDir/pacts

Created on 31 May 2018  路  14Comments  路  Source: pact-foundation/pact-jvm

The title explains it.

Here is my build.gradle file for reference

buildscript {
    ext {
        springBootVersion = '2.0.2.RELEASE'
        pactDirectory = "$buildDir/pacts"
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.junit.platform:junit-platform-gradle-plugin:1.1.0")
        classpath("au.com.dius:pact-jvm-provider-gradle_2.12:3.5.16")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'au.com.dius.pact'
apply plugin: 'org.junit.platform.gradle.plugin'

group = 'io.redacted'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile("org.projectlombok:lombok:1.16.16")
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile('au.com.dius:pact-jvm-consumer-junit5_2.12:3.5.16')
    testRuntime("org.junit.jupiter:junit-jupiter-engine:5.1.0")
    testRuntime("org.junit.platform:junit-platform-launcher:1.1.0")
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.6'
}

// Set where to put pact files
test {
    systemProperties['pact.rootDir'] = pactDirectory
}

// Publish pact files
pact {
    publish {
        pactDirectory = pactDirectory
        pactBrokerUrl = 'http://localhost:3000'
    }
}

// Push pacts to broker
build.finalizedBy(pactPublish)

Most helpful comment

This fix has been released with version 3.6.4

All 14 comments

I just ran ArticlesTest from the pact-jvm project and it definitely writes the pacts to the correct directory, it must be your setup that has an issue.

13:33:00.217 [Test worker] DEBUG au.com.dius.pact.consumer.junit5.PactConsumerTestExt - Writing pact ArticlesConsumer -> ArticlesProvider to file /home/ronald/Development/Projects/Pact/pact-jvm/pact-jvm-consumer-junit5/build/2.12/pacts/ArticlesConsumer-ArticlesProvider.json
au.com.dius.pact.consumer.junit5.ArticlesTest > testArticles(MockServer) PASSED

try this

    test {
      useJUnitPlatform()
      systemProperty 'pact.rootDir', "$buildDir/pacts"
    }

I have to following two lines that are working for me:

def pactFolderString = "$buildDir" + File.separator + "pacts"
test.systemProperties["pact.rootDir"] = pactFolderString

I'm having the same issue running 3.6.1 on JUnit 4 (Android project)

I've tried your suggestions without luck

For some reason PactConsumerConfig doesn't detect that it's running inside gradle. Maybe Android doesn't set System.getenv("org.gradle.test.worker").

That said, I've been able to overwrite the pactDirectory by extending from ConsumerPactTestMk2 and modifying PactTestExecutionContext on each test run. This is my PactBaseTest

 @CallSuper
 override fun runTest(mockServer: MockServer, context: PactTestExecutionContext) {
     context.pactFolder = PACT_FOLDER
 }

@Maragues it is not Android, I have noticed this with my builds. Something has changed with an upgrade to Gradle.

@Maragues looks like org.gradle.test.worker is now set as a system property. I've updated the code to check for both.

This fix has been released with version 3.6.4

Thank you Ronald! Great work as always! :)

Still facing the issue that version 3.6.7 is writing my generated pacts to 'target/pacts' instead of '$buildDir/pacts'.

And the publish tasks default directory for hooking the pacts is in the "$buildDir/pacts"

I am running on gradle 5.3.1

Any ideas ?

@HaMatthias have a look at https://github.com/uglyog/pact-junit5-test and compare it with your project. That project writes the pact files to build/pacts

Hey @HaMatthias and @uglyog, I had the same issue where the pact files are actually stored in the target dir by default.

I noticed that it actually happens only when I run my pact tests using IntelliJ. If instead I run the same tests from CLI, it works correctly.

The issue is that I was running the tests using IntelliJ IDEA instead of Gradle (Default) as per screenshot (Settings > Build, Execution, Deployment > Build and Tools > Gradle)
image

Changing it to Gradle worked for me.

@kekko1212 when running from IntelliJ you'll need to set the pact.rootDir property to where you want the pacts written to.

@uglyog I am trying to change pact directory in JVM and it is not writting in my specified directory it always write in target/pacts

Hey @HaMatthias and @uglyog, I had the same issue where the pact files are actually stored in the target dir by default.

I noticed that it actually happens only when I run my pact tests using IntelliJ. If instead I run the same tests from CLI, it works correctly.

The issue is that I was running the tests using IntelliJ IDEA instead of Gradle (Default) as per screenshot (Settings > Build, Execution, Deployment > Build and Tools > Gradle)
image

Changing it to Gradle worked for me.

Yep, that was my problem. Problem solved for me! I preferably use IntelliJ test runner, so i added property to the test run config

Was this page helpful?
0 / 5 - 0 ratings