Unit Testing of Jsp Custom Tag Before Version Spring 2.5

As it is described on this link, it is easier to mock Custom Tag with Spring 2.5 :http://blog.agilelogicsolutions.com/2011/02/unit-testing-jsp-custom-tag-using.html .

If for whatever reason you are stuck with a version of Spring before 2.5, you can still test Jsp Custom Tag.This is the class i would like to unit test :


public final class CustomTag extends javax.servlet.jsp.tagext.BodyTagSupport{

    public void writeMyTag() throws JspException {

              pageContext.getOut().write("<th> … text here <th>");              

   }

}


My problem is : how to mock the object pageContext ? My solution is to use Mockito.

I did the following unit test and mock pageContext and jspWriter:


public class CustomTagTest {

@Mock

PageContext pageContext;

@Mock

JspWriter jspWriter;

@Before

public void setup(){

MockitoAnnotations.initMocks(this);

}

@Test

public void testwriteMyTag() throws JspException, IOException {

CustomTag tag = new CustomTag ();

(Mockito.doReturn(jspWriter).when(pageContext)).getOut();

tag.setPageContext(pageContext );

tag.writeMyTag();

}

}

Here we go : i covered the custom tag code. However it does not test the output result of JspWriter.

The power of Unit Testing

Why Unit Testing is important ?

Some people think unit tests is a waste of time. For them you should spend more time developing real code instead of doing unit testing. But I disagree. I am more at ease when unit test are running every day without failure. Indeed know my code works like I want. Personally I am faster to develop code because I am testing incrementally my developement. In my experience my code tend to have less bugs too.

I find that Unit testing has many advantages:

– Gain time when reproducing a problem / testing functionality before integration. Instead of launching the entire application to test your changes, you can just test the small part you did.

– Regression testing. The code is easier to refactor when there is lot of unit tests. If there are big changes in the code the unit test should help us to avoid regression.

Unit test are great, but if they are not run daily it is like they do not exist. That’s why it is very important to have a platform that launches them every day. For example for Java projects Jenkins is a popular platform.

Not only you should automatically test the code of programming languages such as Java ,Csharp, Javascript but as well script used for the installation of the product. Ideally the installation from scratch should be run daily on a machine.

Review of the book Pragmatic Programmer

Cover book of The Pragmatic Programmer

Comments about the book

To my mind this book is mind blowing. The first time i read it , it was a revelation. Thanks to it,  I grasped the use of systematic Unit Testing.

Before reading it, I was doing unit testing systematically while bug fixing. This was because of the strong policy of my first company.

After, I also understood many other programming practices. I realised that Unit testing or automatic testing is one of the most important tasks to do while programming. It allows automatic regression test and easier maintenance and bug fixing..

Another idea important to me : code only once policy. No duplication allowed. This is what i have been implementing so far .

Addtitionally, the use of design patterns is really useful to maintain the code . Before reading the book, as a young engineer getting from university, I barely knew what it was useful for. Design pattern is a skill you can implement whatever object language you use : C++,C# ,Java,etc… I have used it with these programming languages with success. It allows better extensibility and maintainability of the code.

Whenever there is a choice between different algorithms or possibility of extensions : use design pattern. The factory pattern is the one that I used the most in the end.

Advice for a young programmer

I would advice to a young engineer to start reading this book. Indeed, it introduces you with many skills you will need to learn if you want to be an excellent programmer. To my mind, the first things you will need to learn are :

  • To add systematically unit test in your code. If you want to go further: use TDD method.
  • Have a platform which launch your unit tests everyday . Be proactive: if it does not exist, suggest one to your manager. This is very important because without it your unit tests are not used. And therefore they are useless.
  • When you resolve a bug , first reproduce the bug. The best way is to reproduce the bug with a Unit test. Make the test failed at first,when the test is reproduced,the test should pass when you found the fix.
  • Use the ‘no duplication’ of code policy.  It is better to factorise the code to avoid duplication. In the long run the maintenance of the code is easier.
  • Use design patterns to make the code more extensible. When you develop a solution in programming, you should always think long term. For example: you need to develop a simple algorithm but maybe you will use a different one in the future. Use the factory method pattern in order to call easily a different algorithm in the future.
  • Learn MVC design pattern. It teaches how to separate the UI interface into several layers. It is important to have a user interface clearly separated into different modules in order to modify change of technology later.
  • Use Sonar to improve your coding skills. This tool when used with Jenkins, will show the problem in the code.

All these tips come from the book, therefore don’t hesitate to read it. It teaches lot of important skills that experienced programmer will only learn after years of development.