如何使用JMockit模拟FacesContext,所有测试都失败并出现NullPointerException

时间:2015-05-15 08:21:56

标签: java junit jmockit facescontext

当bean使用FacesContext或创建使用FacesContext的类的对象时,所有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$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)

1 个答案:

答案 0 :(得分:0)

模拟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));
}