Regarding the junit testing in sightly based components | Community
Skip to main content
October 16, 2015
Solved

Regarding the junit testing in sightly based components

  • October 16, 2015
  • 3 replies
  • 1177 views

Hi,

I am facing the problem with junit testing for sightly based components can you people please suggest how to write the test cases for below code which i mentioned?

 

public void activate() throws Exception {

        LOGGER.info("Class : " + this.getClass().getName()
                + " : Method : activate() : [Start]");

        bioDetailsBean = new BioDetailsBean();

        String prsnInfoPath = getProperties().get(
                BioDetailsConstants.PERSON_DETAILS, String.class);
        Session session = getResourceResolver().adaptTo(Session.class);
        try {

            Node node = session.getNode(prsnInfoPath);

            String description = getProperties().get(
                    BioDetailsConstants.JCR_DESCRIPTION, String.class);
            String introText = getProperties().get(
                    BioDetailsConstants.JCR_INTROTEXT, String.class);

            String firstName = node
                    .getProperty(BioDetailsConstants.JCR_FSTNAME).getValue()
                    .getString();
            String image = node.getProperty(BioDetailsConstants.JCR_IMAGE)
                    .getValue().getString();

            String recrtUniv = node
                    .getProperty(BioDetailsConstants.JCR_RECRTUNIV).getValue()
                    .getString();
            String title = node.getProperty(BioDetailsConstants.JCR_TITLE)
                    .getValue().getString();

            bioDetailsBean.setDescription(description);
            bioDetailsBean.setFirstName(firstName);
            bioDetailsBean.setImage(image);
            bioDetailsBean.setIntroText(introText);
            bioDetailsBean.setRecrtUniv(recrtUniv);
            bioDetailsBean.setTitle(title);

        } catch (PathNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RepositoryException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        LOGGER.info("Class : " + this.getClass().getName()
                + " : Method : activate() : [End]");

    }

 

 

And i am getting the problem like how to call my controller methods in test class using mock objects

Ex: when(wcmuse_mock.getProperties()).thenReturn(V_MOCK); ?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by joerghoh

Hi,

just to make your question more specific: You do a unittest of the backing WCM-Use class, but not of the sightly component (the HTML output generated by sightly) itself. Is this correct?

If you want to unit-test your models, you should try to avoid the use of JCR session. Stay on the sling level. And you work only with the page or component properties, just mock the ValupMap containing the properties.

Kind regards,
Jörg

3 replies

edubey
Level 10
October 16, 2015
Feike_Visser1
Adobe Employee
Adobe Employee
October 16, 2015

Looking at the code here, you are creating a model object based on resource-properties.

To me this is *the* use-case for using sling-models, and reducing the Java-classes you have here.

Overall I would try to avoid Node-class, and use the ValueMap this is much less error-prone.

joerghoh
Adobe Employee
joerghohAdobe EmployeeAccepted solution
Adobe Employee
October 16, 2015

Hi,

just to make your question more specific: You do a unittest of the backing WCM-Use class, but not of the sightly component (the HTML output generated by sightly) itself. Is this correct?

If you want to unit-test your models, you should try to avoid the use of JCR session. Stay on the sling level. And you work only with the page or component properties, just mock the ValupMap containing the properties.

Kind regards,
Jörg