Spring unit testing, embedded RESTEasy service implementation

Source: Internet
Author: User
Tags jboss application server

RESTEasy Introduction

RESTEasy is an open-source project of JBoss. It provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It is a complete implementation of JAX-RS specifications and passes JCP certification. As a JBOSS project, it can be well integrated with the JBOSS application server. However, it can also run in any Servlet container that runs JDK 5 or a later version. RESTEasy also provides a RESTEasy JAX-RS client call framework. It can be easily integrated with EJB, Seam, Guice, Spring and Spring MVC. Supports automatic GZIP decompression on the client and server.
Based on the previous article, implement SpringJUnit4ClassRunner

[Java]
Package com. jje. common. test;
 
Import java. lang. annotation. Annotation;
Import java. lang. annotation. ElementType;
Import java. lang. annotation. Retention;
Import java. lang. annotation. RetentionPolicy;
Import java. lang. annotation. Target;
Import java. util. Collections;
Import java. util. List;
 
Import org. jboss. resteasy. plugins. server. tjws. TJWSEmbeddedJaxrsServer;
Import org. junit. runner. notification. RunNotifier;
Import org. junit. runners. model. InitializationError;
Import org. springframework. test. context. junit4.SpringJUnit4ClassRunner;
 
Public class ResteasyEmbededServer extends SpringJUnit4ClassRunner {
 
Private TJWSEmbeddedJaxrsServer server = new TJWSEmbeddedJaxrsServer ();
 
Public ResteasyEmbededServer (Class <?> Clazz) throws InitializationError {
Super (clazz );
}
 
@ Override
Public void run (RunNotifier notifier ){
Int port = findAnnotationValueByClass (Port. class). value ();
Class [] resourceClasses = findAnnotationValueByClass (Resources. class). value ();
 
StartServer (port, resourceClasses );
Try {
Super. run (notifier );
} Finally {
Server. stop ();
}
}
 
Private void startServer (int port, Class [] resourceClasses ){
Server. setPort (port );
List <Class> actualResourceClasses = server. getDeployment (). getActualResourceClasses ();
Collections. addAll (actualResourceClasses, resourceClasses );
Server. start ();
}
 
Private <T> T findAnnotationValueByClass (Class <T> annotationClass ){
For (Annotation annotation: getTestClass (). getAnnotations ()){
If (annotation. annotationType (). equals (annotationClass )){
Return (T) annotation;
}
}
Throw new IllegalStateException (String. format ("Can't find % s on test class: % s", annotationClass, getTestClass ()));
}
 
@ Retention (RetentionPolicy. RUNTIME)
@ Target (ElementType. TYPE)
Public static @ interface Resources {
Public Class [] value ();
}
 
@ Retention (RetentionPolicy. RUNTIME)
@ Target (ElementType. TYPE)
Public static @ interface Port {
Public int value ();
}
 
}
TJWSEmbeddedJaxrsServer is an embedded service provided by the RESTEasy framework.

To be continued


 

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.