Reputation: 274
So, I have been trying to write JUnit tests for my APIs which interact with Elasticsearch. This is like Integration test where I need to setup Elasticsearch before I can run my code.
For all the tests, I need to create a test task which will do following:
Download the zip from
compile group: 'org.elasticsearch.distribution.zip', name: 'elasticsearch', version: '6.1.1', ext: 'pom'
Run elasticsearch executable present in /bin of unzipped file. When running this executable, take as argument elasticsearch.yml
file in the command.
Once the all the tests are run, stop the elasticsearch executable, and clean the zipped folder.
Things that I can improve is that if zip file is already present in my gradle cache, then don't download it again and again.
Thanks
Upvotes: 1
Views: 727
Reputation: 4923
Add following dependency to your build.gradle
file
configurations {
elasticDist
}
...
dependencies {
elasticDist group: 'org.elasticsearch.distribution.zip', name: 'elasticsearch', version: '6.1.2', ext: 'zip'
}
Add tasks to unzip and cleanup, setup dependencies
task unzip(type: Copy) {
// to download distribution
dependsOn configurations.elasticDist
from { // use of closure defers evaluation until execution time
configurations.elasticDist.collect { zipTree(it) }
}
into file("${buildDir}/your/destination")
}
task cleanElastic(type:Delete) {
delete file("${buildDir}/your/destination")
}
test.dependsOn('unzip')
test.finalizedBy('cleanElastic')
Using your test framework of choice, configure setUp
and tearDown
to start and stop elastic respectively.
Upvotes: 3