Expand my Community achievements bar.

SOLVED

Regarding the junit testing in sightly based components

Avatar

Former Community Member

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); ?

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

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

View solution in original post

3 Replies

Avatar

Correct answer by
Employee Advisor

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

Avatar

Employee

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.