如何使用Jmockit模拟FaceScontext,所有测试都以NullPoInterException失败



当bean使用faceScontext或创建使用faceScontext的类的对象时,所有JUNIT测试都会失败。请建议一种使用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
    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)

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

相关内容

  • 没有找到相关文章

最新更新