简体   繁体   中英

Regarding junit testing for sightly based adobe cq5 components?

I have one controller code like the below and i created some what testing in the below controller code but it is giving NullPointerException for when and thenReturn() method

BioDetailsController.java

package com.biodetails.controller;

import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.sightly.WCMUse;
import com.biodetails.bean.BioDetailsBean;
import com.biodetails.utility.BioDetailsConstants;

public class BioDetailsController extends WCMUse {

   private static final Logger LOGGER = LoggerFactory.getLogger(BioDetailsConstants.class);

   private BioDetailsBean bioDetailsBean;

   @Override
   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]");

   }

   /**
    * @return the bioDetailsBean
    */
   public BioDetailsBean getBioDetailsBean() {
      return this.bioDetailsBean;
   }

   /**
    * @param bioDetailsBean the bioDetailsBean to set
    */
   public void setBioDetailsBean(BioDetailsBean bioDetailsBean) {
      this.bioDetailsBean = bioDetailsBean;
   }

}

BioDetailsControllerTest.java

package com.biodetails;

import io.wcm.testing.mock.aem.junit.AemContext;

import javax.jcr.Node;

import org.apache.sling.api.resource.ValueMap;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;

import com.adobe.cq.sightly.WCMUse;
import com.biodetails.bean.BioDetailsBean;
import com.biodetails.controller.BioDetailsController;


public class BioDetailsControllerTest extends WCMUse {


   BioDetailsController underTest;

   @Mock
   WCMUse wcmuse_mock;

   @Mock
   Node node_mock;

   @Mock
   ValueMap V_MOCK;

   @Mock
   @Rule
   public final AemContext context = new AemContext();

   private BioDetailsBean bioDetailsBean;

   @Before
   public void setUp() throws Exception {
      //wcmuse_mock=mock(WCMUse.class);
      // wcmuse_mock = Mock(WCMUse.class);
      underTest = new BioDetailsController();


   }

   @Test
   public void activateTest() throws Exception {

      when(wcmuse_mock.getProperties()).thenReturn(V_MOCK);//HERE IAM GETTING NULL POINTER EXCEPTION
      underTest.activate();
      //assertTrue(true);
   }


}

Can you please suggest how to write the test cases for this simple controller I tried but it is giving NullPointerException ?

In your @Before method put

MockitoAnnotations.initMocks(this);

like

@Before
   public void setUp() throws Exception {
      MockitoAnnotations.initMocks(this);
      underTest = new BioDetailsController();
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM