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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s