Home > Hippo, Java > HST and mockito sitting on a tree

HST and mockito sitting on a tree

Unit testing your Hippo Site Toolkit components with mockito is so easy, there really is no excuse to not test your hst components any more.
Say you have a component that retrieves a bean from a node, that code might look like this:

public class AnHstComponent extends BaseHstComponent{
  @Override
  public void doBeforeRender(HstRequest request, HstResponse response) throws HstComponentException {
  HippoBean bean = getContentBean(request);
  if(bean!=null) {
    request.setAttribute("document",bean);
    }
  }
}

All we would want to test here is that the HippoBean is retrieved and put on the request
This is not an integration test, but a unit test. So we don’t need or want any interaction with any real JCR repository.
To prevent this we mock the HstRequest and HstResponse interfaces and stub the getContentBean(HstRequest) method.
The test code would then look like this:

@RunWith(MockitoJUnitRunner.class)
public class AnHstComponentMockitoTest {
  @Mock public HstRequest request;
  @Mock public HstResponse response;
  @Spy public AnHstComponent component = new AnHstComponent();
  @Test
  public void testDocumentOnRequestAfterDoBeforeRender() throws Exception {
    //stub
    doReturn(new HippoDocument()).when(component).getContentBean(request);
    //test
    component.doBeforeRender(request, response);
    //verify
    verify(request).setAttribute(anyString(), any(HippoDocument.class));
  }
}

An explanation of the test code

The request and response interfaces are mocked, we can use these to stub any methods we need and verify any operations on it.
These mocked interfaces are passed on to the doBeforeRender method of our HSTComponent.

Since the getContentBean method is part of the BaseHstComponent, we need to partially mock the AnHstComponent.
This is done using mockito’s @Spy technique, which we can use to stub the getContentBean method.
In mockito stubbing is usually done with the When() method.
During stubbing the getContentBeans method is actually called upon using this technique on our real object. This will generate a null pointer exception.
To work around this you can use the doReturn method, which will prevent the real getContentBean method ever to get called.

It is this easy to create a simple testcase for your HstComponent, now go on an create your own!! 🙂

ps. Jeroen Reijn has an example testing hst components with easy mock.

Categories: Hippo, Java Tags: , ,
  1. 30/09/2011 at 13:47

    This may be obvious if you’ve used mockito before, but you also need:

    import static org.mockito.Mockito.*;

    Along with the other imports. (Eclipse failed to figure this one out though it did find the correct imports for the annotations. Strange)

  2. 30/09/2011 at 14:09

    I’m now running up against:

    java.lang.IllegalArgumentException: The component does not have ParametersInfo annotation.
    at org.hippoecm.hst.utils.ParameterUtils.getParametersInfo(ParameterUtils.java:62)
    at org.hippoecm.hst.component.support.bean.BaseHstComponent.getParametersInfo(BaseHstComponent.java:578)
    at com.lukkien.test.HstComponentTest.testDocumentOnRequestAfterDoBeforeRender(HstComponentTest.java:32)

    Trying to write a unit test for the default ‘Overview’ component that comes from the Hippo artifact, because Overview has @ParametersInfo(type = PageableListInfo.class) on it I guess.

    Any ideas? It’s not looking so easy after all. We really want to unit test our Hippo classes…

    • 17/10/2011 at 22:09

      I’m sorry for this late reaction, I just saw you comment in my inbox.

      Testing with parameters info shouldn’t be an issue really. We do ad a mock for it in the test and stub the getParametersInfo with it: ie

      @Mock SiteMenuInfo siteMenuInfo;

      …snip …

      doReturn(siteMenuInfo).when(siteMenuComponent).getParametersInfo(requestMock);

      Not shure though if I would spend my time unit testing default framework components, I would personally scope it to my own components.

      Good luck with your project!

  3. 27/02/2015 at 11:09

    Hi iprofs, thank you but I’m getting NullPointerException on the line component.doBeforeRender(request, response);.

    What am I missing?

  1. No trackbacks yet.

Leave a comment