Search code examples
javajunitjmockitfacescontext

how to mock FacesContext with JMockit, All the tests fail with NullPointerException


All the junit tests fail with NullPointerException when the bean uses FacesContext or creates an object of a class which uses FacesContext. Please suggest a way to test the code with junit.

Bean

@ManagedBean(name = "displayIssuesBean")
@SessionScoped
public class DisplayIssuesBean implements Serializable {

    private static final long serialVersionUID = -3017739535520843880L;
    private static final Logger LOGGER = Logger.getLogger(AddIssueBean.class);
    private transient AddIssueDTO addNewIssueDTO = new AddIssueDTO();
    private transient IDisplayIssues displayIssuesService = new DisplayIssuesService();
    private transient List<String> projectNames = displayIssuesService
            .getProjectNames(getEmailId());
    private transient List<IssueDisplayDTO> issues = new ArrayList<IssueDisplayDTO>();
    private transient List<IssueDisplayDTO> modifiedList = new ArrayList<IssueDisplayDTO>();
    private String projectName;
    private int projectId;
    int firstId;
    int secondId;
    private transient IssueDisplayDTO backup1;
    private transient IssueDisplayDTO backup2;

public String getEmailId() {
        String emailid = FacesContext.getCurrentInstance().getExternalContext()
                .getRemoteUser();
        LOGGER.info(emailid);
        return emailid;
    }

public String setIssueTypeWithIssueTypeId(int issueTypeId) {
        String issueType = null;
        if (issueTypeId == 1) {
            issueType = "Bug";
        } else if (issueTypeId == 2) {
            issueType = "Story";
        } else if (issueTypeId == 3) {
            issueType = "Task";
        }
        return issueType;
    }

Test

@Test
    public void testIssueTypeWithIssueTypeId() {
        DisplayIssuesBean bean = new DisplayIssuesBean();
        assertEquals("Bug", bean.setIssueTypeWithIssueTypeId(1));
    }

ERROR

java.lang.NullPointerException
    at com.jts1.controller.DisplayIssuesBean.getEmailId(DisplayIssuesBean.java:104)
    at com.jts1.controller.DisplayIssuesBean.<init>(DisplayIssuesBean.java:28)
    at com.jts1.test.DisplayIssuesTest.testIssueTypeWithIssueTypeId(DisplayIssuesTest.java:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Solution

  • Mock FacesContext and record an expectation for ExternalContext#getRemoteUser():

    @Test
    public void testIssueTypeWithIssueTypeId(
        @Mocked FacesContext faces, @Mocked final ExternalContext extCtx
    ) {
        new Expectations() {{ extCtx.getRemoteUser(); result = "test"; }};
    
        DisplayIssuesBean bean = new DisplayIssuesBean();
        assertEquals("Bug", bean.setIssueTypeWithIssueTypeId(1));
    }