Embedded jetty Boot Spring (Java configuration method), JUnit test

Source: Internet
Author: User

Tag:jetty    embedded jetty   

Package Com.doctor.embeddedjetty;import Java.util.concurrent.timeunit;import Org.eclipse.jetty.server.Server; Import Org.eclipse.jetty.servlet.servletcontexthandler;import Org.eclipse.jetty.servlet.servletholder;import Org.springframework.web.context.support.annotationconfigwebapplicationcontext;import Org.springframework.web.servlet.dispatcherservlet;public class Embeddedjettyserver {private int port;p Rivate class <?> springconfiguration = null;private Server server;public embeddedjettyserver (class<?> Springconfiguration) {This (8080, springconfiguration);} Public embeddedjettyserver (int port,class<?> springconfiguration) {this.port = Port;this.springconfiguration = Springconfiguration;init ();} public void init () {server = new server (port); Servletcontexthandler context = new Servletcontexthandler (); Context.setcontextpath ("/"); Server.sethandler (context) ; Annotationconfigwebapplicationcontext webapplicationcontext = new Annotationconfigwebapplicationcontext (); WebapplicationconText.register (springconfiguration);D ispatcherservlet dispatcherservlet = new Dispatcherservlet ( Webapplicationcontext); Context.addservlet (new Servletholder (Dispatcherservlet), "/*");} public void Start () throws Exception{if (server!= null) {if (server.isstarting () | | server.isstarted () | | server.isrunning ()) {return;}} TimeUnit.SECONDS.sleep (3); Server.start ();} public void Stop () throws exception{if (server! = null) {if (server.isrunning ()) {server.stop ();}}} public void Join () throws Interruptedexception{if (Server!=null) {server.join ();}}}
Test code:
Package Com.doctor.embeddedjetty;import static Org.junit.assert.*;import static Org.hamcrest.core.isequal.*;import Org.apache.http.client.fluent.request;import Org.apache.http.client.fluent.response;import Org.junit.Test;import Org.springframework.context.annotation.componentscan;import Org.springframework.context.annotation.configuration;public class Embeddedjettyforspringmvctest {<span style= " White-space:pre "></span><span style=" White-space:pre "></span> @Test <span style=" White-space:pre "></span>public void Test () throws Throwable{<span style=" White-space:pre "></span >embeddedjettyserver jettyserver = new Embeddedjettyserver (springconfiguration.class); <span style= " White-space:pre "></span>jettyserver.start (); <span style=" White-space:pre "></span><span Style= "White-space:pre" ></span>response Response = Request.get ("http://localhost:8080/"). Execute ();< Span style= "White-space:pre" ></span>assertthaT (Response.returncontent (). asstring (), Equalto ("Hello")) <span style= "White-space:pre" ></span> Jettyserver.stop (); <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></ Span><span style= "White-space:pre" ></span><span style= "White-space:pre" ></span>@ Configuration<span style= "White-space:pre" ></span> @ComponentScan ("Com.doctor.embeddedjetty") < Span style= "White-space:pre" ></span>static class Springconfiguration{<span style= "White-space:pre" > </span><span style= "White-space:pre" &GT;&LT;/SPAN&GT;}

Embedded jetty Start Spring (Java configuration method), JUnit test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.