[Java EE] Implement a test for REST endpoint

Source: Internet
Author: User
Tags jboss

1. We have the Bookendpoint.java:

 Packagecom.pluralsight.bookstore.rest;ImportCom.pluralsight.bookstore.model.Book;Importcom.pluralsight.bookstore.repository.BookRepository;ImportJavax.inject.Inject;Importjavax.validation.constraints.Min;Importjavax.ws.rs.*;ImportJavax.ws.rs.core.Context;ImportJavax.ws.rs.core.Response;ImportJavax.ws.rs.core.UriInfo;ImportJava.net.URI;Importjava.util.List;Import StaticJavax.ws.rs.core.MediaType.APPLICATION_JSON;//Api/books@Path ("/books") Public classBookendpoint {@InjectPrivatebookrepository bookrepository; @POST @Consumes (Application_json) PublicResponse createbook (book book, @Context uriinfo uriinfo) { Book=bookrepository.create (book); URI Createeduri=uriinfo.getbaseuribuilder (). Path (Book.getid (). toString ()). build (); returnresponse.created (Createeduri). build (); } @GET @Produces (Application_json) PublicResponse getbooks () {List<Book> books =Bookrepository.findall (); if(books.size () = = 0) {            returnresponse.nocontent (). build (); }        returnResponse.ok (books). build (); } @GET @Path ("/{id: \\d+}") @Produces (Application_json) PublicResponse GetBook (@PathParam ("id") @Min (1) (Long id) { book Book=bookrepository.find (ID); if(Book = =NULL) {            returnResponse.Status (Response.Status.NOT_FOUND). build (); }        returnResponse.ok (book). build (); }    //Api/books/count@GET @Path ("/count")     PublicResponse countbooks () {Long nbofbooks=Bookrepository.countall (); if(Nbofbooks = = 0) {            returnresponse.nocontent (). build (); }        returnResponse.ok (nbofbooks). build (); } @DELETE @Path ("/{id: \\d+}")     PublicResponse Deletebook (Long id) {bookrepository.delete (ID); returnresponse.nocontent (). build (); }}

2. Creating a test for the REST endpoint:

 Packagecom.pluralsight.bookstore.rest;ImportCom.pluralsight.bookstore.model.Book;ImportCom.pluralsight.bookstore.model.Language;Importcom.pluralsight.bookstore.repository.BookRepository;Importorg.jboss.arquillian.container.test.api.Deployment;ImportOrg.jboss.arquillian.junit.Arquillian;ImportOrg.jboss.arquillian.test.api.ArquillianResource;ImportOrg.jboss.shrinkwrap.api.ShrinkWrap;ImportOrg.jboss.shrinkwrap.api.asset.EmptyAsset;Importorg.jboss.shrinkwrap.api.spec.JavaArchive;ImportOrg.junit.After;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importjavax.ws.rs.client.Entity;ImportJavax.ws.rs.client.WebTarget;ImportJavax.ws.rs.core.Response;Importjava.util.Date;Import StaticJavax.ws.rs.core.MediaType.APPLICATION_JSON;Import StaticJavax.ws.rs.core.Response.Status.CREATED;Import StaticJavax.ws.rs.core.Response.Status.NO_CONTENT;Import Staticorg.junit.assert.*; @RunWith (Arquillian.class) Public classBookendpointtest {//Testable tell Wildfly does not have this test,//cause we want to test from outside by using HTTP@Deployment (testable =false)     Public Staticjavaarchive createdeployment () {returnShrinkwrap.create (javaarchive.class). addclass (bookrepository.class). addclass (book.class). addclass (Language.class). addclass (Bookendpoint.class). addclass (jaxrsconfiguration.class). Addasmanifestresource (Emptyasset.instance,"Beans.xml"). Addasmanifestresource ("Meta-inf/test-persistence.xml", "Persistence.xml"); } @After Public voidTearDown ()throwsException {} @Test Public voidCreatebook (@ArquillianResource ("Api/books") Webtarget webtarget) {//Test Counting BooksResponse Response = Webtarget.path ("Count"). Request (). get ();        Assertequals (No_content.getstatuscode (), Response.getstatus ()); //Test Find AllResponse =webtarget.request (Application_json). get ();        Assertequals (No_content.getstatuscode (), Response.getstatus ()); //Create a bookBook book =NewBook ("ISBN",NULL, 12F, 123, Language.english,NewDate (), "ImageURL", "description"); Response=webtarget.request (Application_json). Post (entity.entity (book, Application_json));    Assertequals (Created.getstatuscode (), Response.getstatus ()); }}

[Java EE] Implement a test for REST endpoint

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.