IgorShare Thoughts and Ideas

Consulting and Training

Archive for the ‘Unit Testing’ Category

Mocking, embedded-Jetty and expectations for outgoing HTTP requests in Java

Posted by Igor Moochnick on 09/05/2008

If your classes in Java are executing outgoing HTTP calls it’s not easy to unit-test them. In many cases it requires to set-up separate web servers and deploy a test-verification code. I’m going to give you a way to do it in-line, just as you do it with any other unit-test.

In order to do this we’ll use embedded-Jetty. We’ll host Jetty inside the unit-testing framework.

For our example I’m going to use a TestNG unit-testing framework, but, as you can guess, you’re welcome to use any other you see fit.

1. First of all we need to initialize Jetty. We’re going to do it in @BeforeClass method:

private Server server;
private String returnUrl;

@Parameters({ "test-jetty-port" })
public void setUpJetty(@Optional("8123") int testPort) throws Exception
	server = new Server(testPort);

	Handler handler = new AbstractHandler()
	    public void handle(String target, HttpServletRequest request, HttpServletResponse response, int dispatch)
	        throws IOException, ServletException
	        handleHttpRequest(target, request, response);
	returnUrl = "http://localhost:" + Integer.toString(testPort) + "/test";


2. In order to consume all the incoming HTTP traffic we’re going to define a handler (we’re going to discuss other opportunities in the following posts). Note that the incoming HTTP traffic is stored inside the HttpTester parser class:

private HttpTester requestTest = new HttpTester();

private String getRequestString(HttpServletRequest request) throws Exception
	ServletInputStream reqz = request.getInputStream();
	int contentLen = request.getContentLength();
	if (contentLen == -1)
		return request.toString();
	byte[] buff = new byte[contentLen];
	int realLen = reqz.read(buff);
	Assert.assertEquals(realLen, contentLen, "Content length from parameter should be equal to the real content length");
	return request.toString() + new String(buff);

public void handleHttpRequest(String target, HttpServletRequest request, HttpServletResponse response)
		requestTest.parse( getRequestString(request) );
	catch(Exception e)
		Assert.fail("Failed to parse the incoming request.", e);

3. And this is how you can define your test fixture:

public void jobReport_returns_POST_reply() throws Exception
    String xml = "<xml>some-data-here</xml>";

    object-to-test.send_post(returnUrl, xml);
    Assert.assertEquals(requestTest.getMethod(), "POST");
    Assert.assertEquals(requestTest.getURI(), "/test");
    Assert.assertEquals(requestTest.getContent(), xml);

4. After all the tests are done, don’t forget to shutdown the Jetty server:

public void tearDownJetty() throws Exception

Posted in Java, Jetty, Mocking, TestNG, Unit Testing | 1 Comment »

CodeCamp #10 [InTENsity] – see you at my presentations

Posted by Igor Moochnick on 08/28/2008

Note: Please forward this information to anyone who is interested.

The next CodeCamp #10 will be held in Waltham, MA on weekend September 20th and 21st.

For more information check Chris Bowen’s post.

Currently there are 33 submitted (and growing number of) presentations.

I’ll be presenting the “Toolbox for Agile Projects and Developers” and will be covering the topics like:

  • Agile Development Practices
  • TDD ( Test Driven Development)
  • Unit Testing
  • Mocking
  • IOC / DI (Inversion of Control / Dependency Injection)
  • ORM (Object Relational Mapping)
  • Code Coverage
  • Source Control
  • etc …

It appears that the amount of information is enormous, so I’ve decided to split these topics into 2 separate presentations.

See you all there …

Posted in Alt.Net, C#, Community, IOC/DI, Mocking, NHibernate, ORM, Presentations, Refactoring, Tutorials, Unit Testing | Leave a Comment »

Toolbox for Agile Projects and Developers @ Hartford, CT

Posted by Igor Moochnick on 08/18/2008


It was Hartford’s first CodeCamp. I think it was a great success – way to go, Hartford! More to come!

My presentation had a solid attendance and people stayed until the very end. At the end I’ve ran out of time and haven’t had a time to cover in details the Inversion-of-Control and Dependency Injection tools, but, I think, I’ll prepare a separate talk only on this topic – it’s huge and requires a lot of detailed attention.


As I’ve promised, I’m publishing the slide deck as well as all the code iterations for you to have some fun:


Iteration Introduced Link
Slide deck link
0 Base solution link
1 Unit testing link
2 Source Control & Continuous Integration (CI) link
3 Mocking, Refactoring link
4 IOC (Inversion-of-Control) & DI (Dependency Injection) link


Please feel free to contact me for more information or leave your comments on the blog. I’m available to present this topic or any other portion of it in more details on your site – let me know.

Posted in Alt.Net, C#, Community, IOC/DI, Mocking, Presentations, Refactoring, Thoughts, Tutorials, Unit Testing, Visual Studio | Leave a Comment »

%d bloggers like this: