Archive

Posts Tagged ‘powermock’

Online service development in java – a beginner’s guide

November 4, 2012 1 comment

In the coming series of posts, I will share my development experience (code included) with creating an online service from scratch.  If you read through all posts, you should reach a point where you are capable of writing code and deploying it to a production like environment which is available online, using some of the latest (and greatest) open source libraries available in java.  You will definitely not be at the end of the journey, but will get a pretty good head start.

I’ve created a sample project, which basically does nothing (well, almost nothing), but it does so in a (semi) secure way, using several top libraries.  Basic service, is a service which lets you register, login, and… do nothing.  But, it does so using:

Spring, Maven, ESAPI (+AppSensor) for input validation, JSP’s (JSTL+Tiles 2), jQuery (+UI), Mongo DB, Logback, Jackson, Mockito (+PowerMock) for unit testing, and it does it in a RESTful way, using complete Data and Presentation separation (all JSP’s get their data through REST requests, meaning you can add easily add mobile support without almost any change to the  interface), and is fully internationalized (currently supports hebrew and english, meaning we have RTL languages covered).  The complete source code for Basic service is available on google code, it is a fully functional (and GPL open source) example of combining all of these technologies , which means that you can concentrate on the next steps rather than spend time building the foundation from scratch.  It took me about 3 months to go through all the baby steps in each of those technologies (after spending some time picking the right ones of course), I hope this series will save you at least some of that time.

Please keep in mind that I do not claim this to be a complete and bullet proof piece of software, there is still much to do in order to make it “production ready”.

Basic service is deployed on Cloudbees, using Continuous Deployment,  you’ll get some insights on how to do that as well.

One final note: the title says “a beginner’s guide”, but should be read with two different interpretations: one is (possibly) you, who is just getting started with creating an online service, and the other is me, sharing my experience with jumping into these deep water a couple of months ago.  If you like what you read, you’re welcome to comment, if you see me making horrible mistakes, constructive criticism is more than welcome.

Let’s move on.  The next post will cover the basics of setting up a functional development environment, one which will allow you to develop and instantly deploy your code to production so it is available to the rest of the world

Advertisements

Writing Unit tests – a beginner’s experience log

November 2, 2012 Leave a comment

I have to admit the embarrassing truth: I’m a Unit Test noob.  While Unit Testing has been around for a while, and my teams had been writing them for quite a while, I’ve never had the chance to write them myself, and never had the time to invest in learning what they’re all about.

Since I’m now the developer in my startup, and living without Unit Testing is impossible when working in a Continuous Deployment style of product development, it’s time to dive in.

I’m a “read all about it” kind of a guy, so naturally I searched the web for stuff to read.  I’ve found a decent book called Junit in Action which was good for understanding what Unit testing means and what JUnit is all about, but that was not enough to start using the frameworks I’ve chosen (see my post here)

So, onward to online resources.  I went into the Mockito webpage, and read the documentation and example page there, which gave me a better idea of the structure.  I also read this post called “Mocks aren’t Stubs” by Martin Fowler, which gave me a better understanding of how writing Unit tests can actually get me to write better code.  Another interesting reads can be found here, and here.  I also tried to create a unit test for one of my classes (not too complex, but proved to be a challenge which forced me to use PowerMock).

In the end, there’s no better way to learn than to try to do it on your own.  I’ve read about 30 different posts from people with different implementation problems, style questions, etc., and I can proudly say I’ve finally created my very first Unit test which actually runs and verifies behavior!

Categories: R&D Tags: , ,

Choosing a Unit Testing framework

October 2, 2012 1 comment

While piecing together the parts of my service I realized one topic I haven’t touched is Unit testing.I’m going to use Continuous Deployment as my development/deployment strategy, which makes unit testing a must, but to be honest, nowadays Unit testing is a standard development practice which everyone should adopt.

So what’s available as open source today?

As of today (Oct. 2012), there are two main open source Unit Testing frameworks available for Java development: JUnit and TestNG.

On top of those, there are several toolkits which extend the basic functionality, the most talked about from what I’ve read are EasyMock and Mockito, extended by PowerMock for more advanced functionality (like mocking private and final methods), and JMockit as an alternative to all three (since it gives both the basic as well as advanced functionality).

Framework insights:

JUnit has been around for some time now, and is considered to have had the most contribution to unit testing (at least in java).  It is mature and feature rich, and seems to have all you need to get started.  It is also the most supported framework on the market, and most used, which results in built in support by most relevant tools.

TestNG seems to have many enthusiastic followers, but has not been around as long as JUnit, and seems to have (much) less market penetration.  Interestingly though, while JUnit has been losing market share, it was not lost to TestNG, which has been pretty stable in its usage statistics according to a graph I’ve seen.

In 99% of the comparisons I’ve read online between the two frameworks, TestNG was the recommended framework, mainly for its advanced features of groups, parameterized tests, and dependency between tests.  While these seem important to the unit testing novice reader (me), some argue that the reason some of these features do not appear in JUnit is because they defy the purpose of keeping a test independent of other tests.  Since most comparisons I’ve read were at least 2 years old, it might be that feature differences have changed in later versions, as it seems that JUnit is constantly catching up to what is done in TestNG (meaning that TestNG is leading the way?).  Another point which constantly comes up in favor of TestNG is that it is more suited for wide, integration tests, while JUnit is limited to the Unit Testing world.  This comes up in the context of integration tools like Selenium.

Complementing toolkits insights:

EasyMock is usually compared to Mockito, which by the way was forked from EasyMock but has been developed to a point in which (according to its developers) it has no common code.   It’s usually stated that EasyMock is more strict and breaks easier on code change, while Mockito is more lenient thus letting some code changes slide before raising a red flag.  In EasyMock you have to specify exactly what you expect to happen when the code runs, while Mockito lets you concentrate on the results.  This is referred to as an advantage by some, and disadvantage by others, since it’s a constant battle between test completeness and coding agility.  Both need to be complemented by PowerMock to be able to do advanced stuff like mocking private or final methods, with no clear winner as far as functionality or design is concerned from what I’ve read.  On the other side is JMockit, which seems to have the same type of enthusiastic followers as TestNG (though not necessarily the same ones). It is frequently referred to as a power tool, with a nice set of features and highly competitive of the EasyMock/Mockito & PowerMock pairs.

As with TestNG, JMockit seems to get most of the votes when it comes to feature comparison and decision of which toolkit should be chosen by newcomers, but is not the most widely used toolkit (not sure which one of the two has the lead here).

Bottom line:

All frameworks/tools discussed in this post are actively developed (as of Oct. 2012).  While it sounds like the newcomers (TestNG and JMockit) have the advantage over the older and more widely used projects, I’m inclined to use the more established and popular tools, as I feel that as a new user I would have less issues and better integration support.

Having said that, I have a feeling that once I collect enough milage with unit testing as a concept, it will be a good time to revisit this decision, as the alternatives seem to offer good value over the older tools (according to those that know both), and worth the investment of learning a new tool.

To be continued…

Relevant links:

Martin Fowler is always fun to read, in this post he talks about JMockit as a power tool which lets you do great stuff, at the expense of fixing up your smelly code.  Worth reading…

This post describes some guy’s experience with both toolkits (Mockito and EasyMock).

This document shows a comparison between writing tests in  jMock vs. EasyMock.  While it doesn’t show Mockito, and it is rather old (2008), you can still get the hang of what writing tests is all about.  There are other posts with such comparisons, I’d suggest finding a few to understand what each toolkit looks like when implementing tests.

Here is an interesting discussion on stackoverflow.

Categories: R&D Tags: , , , , ,