You can simply add a gradle.properties
file with the proxy settings to your test project (run with GradleRunner
). Here’s a complete example project (Gradle 7.1 Wrapper files not shown):
├── build.gradle
└── src
└── test
└── groovy
└── com
└── example
└── MyTest.groovy
build.gradle
plugins {
id 'groovy'
id 'java-gradle-plugin'
}
repositories {
mavenCentral()
}
dependencies {
testImplementation('org.spockframework:spock-core:2.0-groovy-3.0')
}
test {
useJUnitPlatform()
}
src/test/groovy/com/example/MyTest.groovy
package com.example
import org.gradle.testkit.runner.GradleRunner
import static org.gradle.testkit.runner.TaskOutcome.*
import spock.lang.TempDir
import spock.lang.Timeout
import spock.lang.Specification
class MyTest extends Specification {
@TempDir File projDir
@Timeout(10)
def "network connection through proxy works"() {
given:
def myTestTask = 'foo'
new File(projDir, 'settings.gradle') << "rootProject.name = 'my-test'"
new File(projDir, 'gradle.properties') << '''\
systemProp.http.proxyHost = 192.168.123.123
'''
new File(projDir, 'build.gradle') << """\
task $myTestTask {
doLast {
println(new java.net.URL('http://www.example.com').text)
}
}
"""
when:
def result = GradleRunner.create()
.withProjectDir(projDir)
.withArguments(myTestTask)
.build()
then:
result.task(":$myTestTask").outcome == SUCCESS
}
}
I’ve used a non-existing dummy proxy which leads to a test failure because of a test/connection timeout (→ SpockTimeoutError
). Using a real proxy instead, the test should succeed.