. Embedded jetty Start Spring (Java configuration mode) for JUnit testing. Standard spring configuration (Java config) embedded jetty9 boot

Source: Internet
Author: User

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.contextloaderlistener;import Org.springframework.web.context.support.annotationconfigwebapplicationcontext;import   org.springframework.web.servlet.dispatcherservlet;/** * Standard spring configuration (Java config) embedded jetty9 boot * @author Doctor * * @time December 8, 2014 PM 4:07:40 */public class EmbeddedJettyServer2 {private int port;p rivate class<?> Springrootconfiguration = null;private class<?> springmvcconfiguration = null;private Server server;public EmbeddedJettyServer2 (class<?> springrootconfiguration,class<?> springmvcconfiguration) {This (8080, springrootconfiguration,springmvcconfiguration);} Public EmbeddedJettyServer2 (int port,class<?> springrootconfiguration,class<?> springmvcconfiguration) {This.port = port;this.springRootconfiguration = Springrootconfiguration;this.springmvcconfiguration = Springmvcconfiguration;init ();} public void init () {server = new server (port); Servletcontexthandler context = new Servletcontexthandler (); Context.setcontextpath ("/"); Server.sethandler (context) ; Annotationconfigwebapplicationcontext rootcontext = new Annotationconfigwebapplicationcontext (); Rootcontext.register (this.springrootconfiguration); Context.addeventlistener (New Contextloaderlistener ( Rootcontext)); Annotationconfigwebapplicationcontext mvccontext = new Annotationconfigwebapplicationcontext (); mvcContext.register (springmvcconfiguration);D ispatcherservlet dispatcherservlet = new Dispatcherservlet (mvccontext); 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 Case:

Package Com.doctor.embeddedjetty;import static Org.hamcrest.core.isequal.equalto;import static org.junit.assert.*; 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;/** * Standard spring configuration (Java config) embedded Jetty9 startup Test * * @author Doctor * * @time December 8, 2014 PM 4:06:41 */public class Embeddedjettyserver2test {@Testpublic void Test () throws Throwable{embeddedje TtyServer2 embeddedjettyserver = new EmbeddedJettyServer2 (Springrootconfiguration.class, Springmvcconfiguration.class); Embeddedjettyserver.start (); Response Response = Request.get ("Http://localhost:8080/embeddedJettyServer2Test"). Execute (); Assertthat ( Response.returncontent (). asstring (), Equalto ("Simpleserviceimpl")); Embeddedjettyserver.stop ();} @Configuration @componentscan ("Com.doctor.embeddedjetty") public static class springrootconfiguration{}@ Configuration@cOmponentscan ("Com.doctor.embeddedjetty") public static Class springmvcconfiguration{}} 

Package Com.doctor.embeddedjetty;public interface SimpleService {public String getMessage ();}
Package Com.doctor.embeddedjetty;import org.springframework.stereotype.Component; @Component ("SimpleService") public class Simpleserviceimpl implements SimpleService {@Overridepublic String getMessage () {return ' Simpleserviceimpl ";}}


Package Com.doctor.embeddedjetty;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.beans.factory.annotation.qualifier;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestmethod;import Org.springframework.web.bind.annotation.responsebody;import Org.springframework.web.bind.annotation.RestController; @RestControllerpublic class SImpleController2 {@ Autowired@qualifier ("SimpleService") private SimpleService SimpleService; @RequestMapping (value= "/ Embeddedjettyserver2test ", Method=requestmethod.get) @ResponseBodypublic String getMessage () {return Simpleservice.getmessage ();}}


. Embedded jetty Start Spring (Java configuration mode) for JUnit testing. Standard spring configuration (Java config) embedded jetty9 boot

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.