Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3783906
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:17:46+00:00 2026-05-19T11:17:46+00:00

I am using the ServletTester class provided by Jetty to test one of my

  • 0

I am using the ServletTester class provided by Jetty to test one of my servlets.

The servlet reads the the body of the request using InputStream.read() to construct a byte[] which is the decoded and acted on by the servlet.

The ServletTest class provides a method getResponses(ByteArrayBuffer) but I’m unsure how to create one of these in the correct format since it would also need to contain things like headers (e.g. “Content-Type: application/octet-stream).

Can anyone show me an easy way to construct this, preferably using an existing library so that I can use it in a similar way to the HttpTester class.

If there is a “better” way to test servlets (ideally using a local connector rather than via the tcp stack) I’d like to hear that also.

Many thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T11:17:46+00:00Added an answer on May 19, 2026 at 11:17 am

    Why use a mock at all? Why not test the servlet by running it in jetty?

    Servlet servlet = new MyServlet();
    String mapping = "/foo";
    
        Server server = new Server(0);
        Context servletContext = new Context(server, contextPath, Context.SESSIONS);
        servletContext.addServlet(new ServletHolder(servlet), mapping);
        server.start();
    
        URL url = new URL("http", "localhost", server.getConnectors()[0].getLocalPort(), "/foo?bar");
    
        //get the url...assert what you want
    
        //finally server.stop();
    

    Edit: Just wanting to reassure people that this is very fast. Its also a very reliable indicator of what your code will actually do, because it is in fact doing it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using jQuery, one can easily find out whether a particular element is visible using
Using mercurial, I've run into an odd problem where a line from one committer
Using Core Data, I have a fetch request to fetch the minimum of a
I initially just had a Java Servlet that I needed to unit test. I
I'm currently trying to get a unit test set up for an HttpServlet class
Using C#, I need a class called User that has a username, password, active
using this code I can send one texture to the shader: devcon->PSSetShaderResources(0, 1, &pTexture);
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Using a populated Table Type as the source for a TSQL-Merge. I want to
using this http://bl.ocks.org/950642 we can see how to add images to nodes, the question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.