如何使用 JMockit 模拟 FacesContext,所有测试都因 NullPointerException 而失败

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

当 bean 使用 FacesContext 或创建一个使用 FacesContext 的 class 对象时,所有 junit 测试都失败并出现 NullPointerException。请建议一种使用 junit 测试代码的方法。

豆子

@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
    public void testIssueTypeWithIssueTypeId() {
        DisplayIssuesBean bean = new DisplayIssuesBean();
        assertEquals("Bug", bean.setIssueTypeWithIssueTypeId(1));
    }

错误

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.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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.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)

模拟 FacesContext 并记录对 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));
}