DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Spock @RunJetty

Spock @RunJetty

Taha Siddiqi user avatar by
Taha Siddiqi
·
Aug. 15, 12 · Interview
Like (0)
Save
Tweet
Share
2.45K Views

Join the DZone community and get the full member experience.

Join For Free

working with geb is fun especially when integrated with spock . in order to write tests for a tapestry app, i wanted to have something on the lines of seleniumtestcase which will automatically start and stop a jetty server. so i ended up writing a spock plugin for running jetty.

we start with the annotation(why do i always start with an annotation ? :) )

@documented
@retention(retentionpolicy.runtime)
@target({elementtype.method, elementtype.type})
@extensionannotation(jettyextension.class)
public @interface runjetty {

    string context() default "";

    int port() default 9090;

    string webappdirectory() default "src/main/webapp";

    string host() default "localhost";

}

note the annotation @extensionannotation(jettyextension.class). it tells spock what extension to use for this annotation.

public class jettyextension extends abstractannotationdrivenextension<runjetty> {

    private boolean isspecannotated;

    @override
    public void visitspecannotation(final runjetty runjetty, specinfo specinfo) {
        isspecannotated = true;
        specinfo.addlistener(new abstractrunlistener() {

            private server server;

            @override
            public void beforespec(specinfo specinfo) {
                server = jettyutils.run(runjetty.context(), 
                  runjetty.webappdirectory(), runjetty.port());
            }

            @override
            public void afterspec(specinfo specinfo) {
                jettyutils.stop(server);
            }

        });

    }

    @override
    public void visitfeatureannotation(final runjetty runjetty, featureinfo featureinfo) {
        if(isspecannotated){
            throw new runtimeexception(string.format(
                    "a single specification cannot have both specification and feature annotated " +
                    "by %s", runjetty.class.getsimplename()));
        }

        featureinfo.getparent().addlistener(new abstractrunlistener() {

            private server server;

            @override
            public void beforefeature(featureinfo featureinfo) {
                server = jettyutils.run(runjetty.context(), 
                   runjetty.webappdirectory(), runjetty.port());
            }

            @override
            public void afterfeature(featureinfo featureinfo){
                jettyutils.stop(server);
            }
        });
    }
}

public class jettyutils {

    public static server run(string contextpath, string webappdirectory, int port) {
        server server = new server(port);

        socketconnector connector = createconnector(port);
        server.setconnectors(new connector[]{connector});

        webappcontext context = createwebappcontext(webappdirectory, contextpath);
        server.sethandler(context);
        try {
            server.start();
        } catch (exception e) {
            throw new runtimeexception("could not start a jetty instance: " + e.getmessage(), e);
        }

        return server;
    }

    public static void stop(server server){
        try {
            server.stop();
        } catch (exception e) {
            throw new runtimeexception("could not stop a jetty instance : " + e.getmessage(), e);
        }
    }

    private static webappcontext createwebappcontext(string webappdirectory, string contextpath) {
        webappcontext context = new webappcontext();

        context.setwar(webappdirectory);
        context.setcontextpath(contextpath);

        return context;
    }

    private static socketconnector createconnector(int port) {
        socketconnector connector = new socketconnector();
        connector.setport(port);
        return connector;
    }

}

if the annotation is placed on a spec, this extension starts the server before a spec and stops it after all the features in the spec are run. if the annotation is placed on a feature, the server is started before the feature and stopped after the feature.

now to make the extension work with gebspec we have to make a few changes which we do by extending gebspec.

@runjetty
class jettygebspec extends gebspec {

    def setup() {
        runjetty runjetty = getrunjettyannotation()
        browser.baseurl = "http://${runjetty.host()}:${runjetty.port()}/${runjetty.context()}"
    }

    protected runjetty getrunjettyannotation() {
        runjetty runjetty = this.class.getannotation(runjetty);

        if (runjetty == null) {
            runjetty = jettygebspec.getannotation(runjetty.class);
        }

        return runjetty;
    }

}

this base class sets the baseurl for geb and also sets the defaults for @runjetty. so now you can write a geb spock test like this.

public class mytest extends jettygebspec {

   def "test my page"(){
      given:
      to indexpage
   }
}

Spock (testing framework)

Published at DZone with permission of Taha Siddiqi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 7 Awesome Libraries for Java Unit and Integration Testing
  • A Brief Overview of the Spring Cloud Framework
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer
  • Top 10 Secure Coding Practices Every Developer Should Know

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: