Wednesday, February 9, 2011

Unit Testing JSP Custom Tag Using Spring

In order to unit test your JSP tag, you need to mock up ServletContext and PageContext.  This would be a difficult task without the help from 3rd party library.  Spring 2.5 provides a mock up liberary bundled in spring-test-2.5.x.jar.  This greatly speed up the construction of the test code.

Now I'm going to show an example on testing a custom tag using Spring test library:

Assuming we have a custom tag as below:

public class DataInquiryTag extends TagSupport {

 private String name;  // attribute parameter name
 private String size;  // HTML element size 
 
 @Override
 public int doStartTag() throws JspException {
  String msg = "Hello "+name+"";
  pageContext.getOut().write(msg);
  return super.doStartTag();
 } 
}

We test our tag using JUnit 4.
Before every test invocation, we need to mock ServletContext and PageContext. To do that we setup @Before method as below:

@Before
 public void setup(){
  // mock ServletContext
  mockServletContext = new MockServletContext();
  // mock PageContext
  mockPageContext = new MockPageContext(mockServletContext);  
  tag = new MyTag();
  tag.setPageContext(mockPageContext);  
 }
The @Before method will use spring mock library to mock ServletContext and PageContext. The is good enough for simple pojo tag that does not rely on any application context.
What if out tag is depending on application context? An example would be a class referenced in the test implements InitializingBean interface. The use of the class is to load properties and initialize values. This requires spring context to be loaded.
To load Spring Application Context, such as WebApplicationContext, we need to add following lines in @Before method:

String configLocations = "/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-mock.xml";
 mockServletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, configLocations);
 ContextLoader loader = new ContextLoader();  
 loader.initWebApplicationContext(mockServletContext);
We first set the location of applicationContext file. Note that we add a applicationContext-mock.xml file to override beans defined in default context. This is handy because some beans may require external resource such as data source in order to be loaded correctly. We can override these beans to local pojo bean for the sake of unit testing.

Usually, application context is common to all tests so we only need to load spring context once before executing all tests. We can do so by adding these lines in @BeforeClass method. So our final setup looks like below:

@BeforeClass
 public static void init(){
  // mock ServletContext
  mockServletContext = new MockServletContext();
  //Add the following lines if your test depends on spring context to be loaded
  //For example, you have a referenced class that implements   
  org.springframework.beans.factory.InitializingBean interface
  String configLocations = "/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-mock.xml";
  mockServletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, configLocations);
  ContextLoader loader = new ContextLoader();  
  loader.initWebApplicationContext(mockServletContext);
 }
 
 @Before
 public void setup(){
  // mock PageContext
  mockPageContext = new MockPageContext(mockServletContext);  
  tag = new MyTag();
  tag.setPageContext(mockPageContext);  
 }
The test setup as above to load application context at the beginning of all test run. Then every test will create its own PageContext to hold the request and response as well as a new tag instance.

After setting up the test environment, it's time to write our test:
@Test
 public void testDoStartTag() throws Exception{  
 try{
  tag.setName("John");
  tag.doStartTag();
  String output = ((MockHttpServletResponse)mockPageContext.getResponse()).getContentAsString(); 
  assert("Hello John
".equals(output)); }catch(JspException je){ assert(false); } }
The output string contains the actual http response (e.g. generated html code) of the tag when calling doStartTag method. So you can right specific assertion logic against the output of the tag.


The complete test class source is shown below:

public class TestDataInquiryTag {  
 @BeforeClass
 public static void init(){
  // mock ServletContext
  mockServletContext = new MockServletContext();
  //Add the following lines if your test depends on spring context to be loaded
  //For example, you have a referenced class that implements 
  //org.springframework.beans.factory.InitializingBean interface
  String configLocations = "/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-mock.xml";
  mockServletContext.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, configLocations);
  ContextLoader loader = new ContextLoader();  
  loader.initWebApplicationContext(mockServletContext);
 }
 
 @Before
 public void setup(){
  // mock PageContext
  mockPageContext = new MockPageContext(mockServletContext);  
  tag = new MyTag();
  tag.setPageContext(mockPageContext);  
 }

 @Test
 public void testDoStartTag() throws Exception{  
  try{
   tag.setName("John");
   tag.doStartTag();
   String output = ((MockHttpServletResponse)mockPageContext.getResponse()).getContentAsString(); 
   assert("Hello John
".equals(output)); }catch(JspException je){ assert(false); } } }

30 comments :

  1. Hi, I am trying to do the same but I get an UnsupportedOperationException (Spring 3.0.5) when trying to get the JspWriter throuth getOut().

    I checked the source code and there is nothing implemented in that method. It just throws an UnsupportedOperationException.

    I checked the API and it says:
    "Does not support writing to a JspWriter, request dispatching, and handlePageException calls."
    http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/mock/web/MockPageContext.html

    How is it possible it works for you?

    ReplyDelete
    Replies
    1. Machine Learning Projects for Final Year machine learning projects for final year

      Deep Learning Projects assist final year students with improving your applied Deep Learning skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include Deep Learning projects for final year into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Deep Learning Projects for Final Year even arrange a more significant compensation.

      Python Training in Chennai Project Centers in Chennai

      Delete
  2. Hi alexcuesta,

    I'm using spring-2.5.6.jar and spring-test-2.5.6.SEC01.jar for my testing and it works fine. When I call getOut() method from y mocked PageContext instance, I abtained a MockedJspWriter object which writes http response properly.
    I have not tested the code in Spring 3.x so it could be compatibility issue. Since you are using Spring 3, you should use the org.springframework.test-3.x.jar included in spring 3 download package.

    ReplyDelete
  3. Informative post. Keep sharing such a useful post.

    ppc training in chennai

    ReplyDelete
  4. In your example ContextLoader is it from org.springframework.test.context or org.springframework.web.context

    ReplyDelete
  5. Your way to enlighten everything on this blog is actually pleasant, everyone manage to efficiently be familiar with it, Thanks a great deal.
    UX agency

    ReplyDelete
  6. The work did to validate our idea gave us the confidence how to start a website design company to stay the course and build our product

    ReplyDelete
  7. Their personalized service and knowledge of the market are appreciated and important for the company's digital channels' success
    best branding agency

    ReplyDelete
  8. They assigned project manager to streamline workflow, which has been really effective.
    media agencies in San Francisco

    ReplyDelete
  9. Ability to turn constructive feedback into high-quality best app designers, on-time deliverables are hallmarks of their work.

    ReplyDelete
  10. They delivered excellent design work for the client but had difficulty with other aspects of the project including quality assurance and delivering on schedule.
    brand companies

    ReplyDelete
  11. UX design
    The team was punctual and integrated feedback quickly to ensure efficient collaboration. They managed the project capably to support a partnership that exceeded expectations.

    ReplyDelete
  12. Thank you I am glad about the encouragement! I love your site, you post outstanding.
    UX design studios

    ReplyDelete
  13. Impressive web site, Distinguished feedback that I can tackle. Im moving forward and may apply to my current job as a pet sitter, which is very enjoyable, but I need to additional expand. Regards. check this site

    ReplyDelete
  14. This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free. Indian vixaj neeg ncig tebchaws

    ReplyDelete
  15. You guys present there are performing an excellent job.
    logo designing agency

    ReplyDelete
  16. Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me. new zealand visa

    ReplyDelete
  17. Thanks meant for sharing this type of satisfying opinion, written piece is fastidious, that’s why I’ve read it completely.
    top web designers in the world

    ReplyDelete
  18. Hello, this is very useful material. This article is really inspiring me to do the same thing as you.. You can read online info about Turkey evisa.The Turkey e visa is an electronic visa that allows entry into Turkey.

    ReplyDelete
  19. This is my very first time that I am visiting here and I’m truly pleasurable to see everything at one place.
    experience design companies

    ReplyDelete
  20. I see the greatest contents on your blog and I extremely love reading them.
    UI design company

    ReplyDelete
  21. Keep up the work, great job! The Kenya government has introduced an online visa system that allows US citizens to obtain a visit visa electronically. kenya visa for us citizens, US citizens can now easily apply for kenya visa from their home or office.

    ReplyDelete
  22. Nice post. keep up the good work... visacent.com linkedin, You can get info about Visa & IT Services. You can visit visacent linkedin page and read all info.

    ReplyDelete
  23. I am enjoying every bit of it. It is a good site, as well as the stock is well-chosen. Thank you India electronic business visa, you can get an online India business e visa through the India e visa website. India e business visa is more convenient and hassle-free.

    ReplyDelete
  24. Incredible website your website layout & themes so good it's amazing, and your blog post for many useful for me. Keep up the.. great work. India start issuing long term tourist visas again, you can apply for an Indian e Visa online via evisa Indian website.

    ReplyDelete
  25. What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much. !!!

    real estate document management

    ReplyDelete
  26. I just wanted to say thank you for a wonderful post that I found. As a matter of fact, I've been looking for a similar type of post for a week and didn't find it until today. Thanks a lot and l'll look forward to reading more from you. I'm so happy to announce that the Myanmar visa online is available for you to obtain. You can generate request via the internet from the comfort of your home.

    ReplyDelete