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 JohnThe 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.
".equals(output)); }catch(JspException je){ assert(false); } }
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); } } }
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().
ReplyDeleteI 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?
Hi alexcuesta,
ReplyDeleteI'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.
Interesting Article
ReplyDeleteJava Training in CHennai | Online Java Training
Informative post. Keep sharing such a useful post.
ReplyDeleteppc training in chennai
In your example ContextLoader is it from org.springframework.test.context or org.springframework.web.context
ReplyDeleteYour way to enlighten everything on this blog is actually pleasant, everyone manage to efficiently be familiar with it, Thanks a great deal.
ReplyDeleteUX agency
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