On 13/09/2010, at 12:03 PM, Kenneth Kousen wrote:
> That's great. I think I have it working now. Below is my build file.
> Please let me know if I've overcomplicated things.
You probably don't need the integrationTestCompile and integrationTestRuntime
configurations, as you don't add any integration test-specific dependencies to
them.
There's some thing we have planed which will make your build script simpler:
* An integration test plugin. This will add the source set, configurations, and
integrationTest task and wire everything up. Possibly when used with the jetty
and war plugins, it might also automatically start and stop the jetty container.
* A way to mark a task as a set up or tear down task for another task. So you
might do something like:
integrationTest(...) {
intializedBy jettyStart
finalizedBy jettyStop
}
(I'm sure we can come up with better names that initializedBy and finalizedBy,
btw)
Then, whenever you run the integrationTest task, Gradle will take care of
running jettyStart just before the integrationTest task, and jettyStop just
after the integrationTest task. This will take up-to-date status into account,
so that jettyStart is run only if the integrationTests are out-of-date and will
actually be executed, ie something like this pseudo-code:
// run the other tasks
if (integrationTest.isOutOfDate) {
jettyStart.execute()
try {
integrationTests.execute()
}
finally {
jettyStop.execute()
}
}
Having said all this, an alternative to using the jettyStart and jettyStop
tasks is to simply start the Jetty server programatically from your integration
tests. Jetty is super simple to configure and start from code, plus it starts
and stops really quickly. The benefit of this approach is that your test setup
is closer to the test code, and your tests don't depend on having been run from
the build - for example, you can run the tests from your IDE.
>
> I assume I need to make sure that jettyRun is a daemon thread, and I added a
> task to shut down the server.
>
> Thanks again for all your help,
>
> Ken
>
> apply plugin:'war'
> apply plugin:'jetty'
> apply plugin:'groovy'
>
> repositories {
> mavenCentral()
> }
>
> configurations {
> integrationTestCompile {
> extendsFrom testCompile
> }
> integrationTestRuntime {
> extendsFrom integrationTestCompile, testRuntime
> }
> }
>
> sourceSets {
> main {
> java { srcDirs = [] }
> groovy { srcDir 'src' }
> }
> test {
> java { srcDirs = [] }
> groovy { srcDir 'tests' }
> }
> integrationTest {
> java { srcDirs = [] }
> groovy { srcDir 'integrationTests' }
> compileClasspath = sourceSets.main.classes +
> sourceSets.test.classes +
> configurations.integrationTestCompile
> runtimeClasspath = classes +
> compileClasspath + configurations.integrationTestRuntime
> }
> }
>
> webAppDirName = 'WebContent'
> stopKey = 'foo'
> stopPort = 9451
>
> gradle.taskGraph.whenReady {graph ->
> if (graph.hasTask(integrationTest)) {
> [jettyRun, jettyRunWar]*.daemon = true
> }
> }
>
> task integrationTest(type: Test, dependsOn: jettyRun) {
> testClassesDir = sourceSets.integrationTest.classesDir
> classpath = sourceSets.integrationTest.runtimeClasspath
> }
>
> task shutDownServer(dependsOn: integrationTest) {
> jettyStop.execute()
> }
>
> build.dependsOn shutDownServer
>
> def spockVersion = '0.4-groovy-1.7'
>
> dependencies {
> groovy group:'org.codehaus.groovy', name:'groovy-all', version:'1.7.4'
> compile 'org.apache.httpcomponents:httpclient:4.0.2'
> providedCompile 'javax.servlet:servlet-api:2.5'
>
> testCompile 'junit:junit:4.8.1'
> testCompile group:'org.spockframework', name:'spock-core',
> version:spockVersion
> }
>
>
> On Sun, Sep 12, 2010 at 5:52 PM, Rene Groeschke <[email protected]> wrote:
> Hi Ken,
>
> Am 12.09.10 23:09, schrieb Kenneth Kousen:
> > Hi all,
> >
> > I've been playing around with some basic servlets (wow, how 1997, right?) to
> > simulate how RESTful web services work.
> >
> > When testing SOAP-based web services, JDK 1.6 includes an Endpoint.publish()
> > command that lets me start a tiny server in a regular Java app, and then in
> > Gradle I can start that in my tests. When I test JAX-RS web services,
> > Jersey comes with an embeddable server called Grizzly, which lets me do
> > something similar. I've been wondering, though, if the JDK or Gradle
> > included any way to do the same approach with basic servlets.
> >
> > One of the Gradle examples shows how to define a task that depends on
> > jettyRun. Calling that task builds a URL and accesses it. That works, but
> > the problem is that I have to embed all my testing code inside my Gradle
> > task. Plus the results don't show up as part of my overall test cases.
> You could use an additional sourceSet for those integration tests. have
> a look at http://bit.ly/9jEDAr
> If you configure the according test task to be dependent on jettyRun you
> can test your rest api using the gradle jetty plugin.
>
> regards,
> René
>
>
>
> --
> ------------------------------------
> Rene Groeschke
>
> [email protected]
> http://www.breskeby.com
> http://twitter.com/breskeby
> ------------------------------------
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
> http://xircles.codehaus.org/manage_email
>
>
>
>
>
> --
> Kenneth A. Kousen
> President
> Kousen IT, Inc.
>
> Email: [email protected]
> Site: http://www.kousenit.com
> Blog: http://kousenit.wordpress.com
> Twitter: @kenkousen
--
Adam Murdoch
Gradle Developer
http://www.gradle.org
CTO, Gradle Inc. - Gradle Training, Support, Consulting
http://www.gradle.biz