简体   繁体   English

如何在Junit测试中避免FaceContext中出现NullPointerException?

[英]How to avoid a NullPointerException from FaceContext in Junit tests?

My team and me made a Webapp in Spring as a project and we have to test most of our code. 我和我的团队在春季将Webapp作为一个项目进行了开发,我们必须测试大多数代码。 The problem is that we use FaceContext messages in quite a lot of methods and always when we try to test those methods we get NullPointerException s at the FaceContext-Messages. 问题在于我们在很多方法中使用FaceContext消息,并且总是在尝试测试这些方法时,在FaceContext-Messages处获得NullPointerException How can i avoid or catch them? 我如何避免或抓住它们?

Our current solution is to use @Test(expect = NullPointerException.class) but that is not really the way we want to go. 我们当前的解决方案是使用@Test(expect = NullPointerException.class)但这并不是我们想要的方式。 Is this possible in Junit or do i need Selenium for this? 在Junit中这可能吗,或者我需要硒吗?

An example would be: 一个例子是:

public boolean validateInfo(){
    boolean correct = true;
    if(startDate == null || endDate == null) {
        if(startDate == null)
   FacesContext.getCurrentInstance().addMessage("msg", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Gib ein Startdatum an!", ""));
        if(endDate == null)             FacesContext.getCurrentInstance().addMessage("msg", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Gib ein Enddatum an!", ""));

        return false;
    }
    if(startDate.after(endDate)) {
        FacesContext.getCurrentInstance().addMessage("msg", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Startdatum muss vor dem Enddatum sein!", ""));
        correct = false;
    }

    if(maxChildren < 1) {
        FacesContext.getCurrentInstance().addMessage("msg", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Max. Kinderanzahl muss größer 0 sein!", ""));
        correct = false;
    }

    if(bringTime.after(pickupTime)) {
        FacesContext.getCurrentInstance().addMessage("msg", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Bringzeit muss vor dem Abholzeit sein!", ""));
        correct = false;
    }
    return correct;
}

It goes to the first FaceContext Message and throws a NullPointerException . 它转到第一个FaceContext消息并抛出NullPointerException Can someone help? 有人可以帮忙吗? Other solutions here on StackOverflow haven't helped. StackOverflow上的其他解决方案没有帮助。

Simple: your production code is calling a static method that probably returns null. 很简单:您的生产代码正在调用一个可能返回null的静态方法。

You could use frameworks such as PowerMock or JMockit to mock that static call. 您可以使用PowerMock或JMockit之类的框架来模拟该静态调用。

Of course, the better way would be to avoid that static call and rely on dependency injection instead. 当然,更好的方法是避免该静态调用,而是依赖于依赖项注入。 As mocking static stuff is considered a bad practice. 由于嘲笑静态内容被认为是不良做法。 So the better approach would be to change your production code to be easier to test, instead of turning to such frameworks to "fix" your design problem. 因此,更好的方法是更改​​生产代码以使其更易于测试,而不是借助此类框架来“解决”您的设计问题。

the problem is that a facesContext is not a singleton, and starts a request upon running. 问题是facesContext不是单例,并且在运行时启动请求。 Junit wont be able to handle that request by itself, itll need to be mocked up, or stubbed in. see more about facescontext here Junit将无法单独处理该请求,需要对其进行模拟或插入。请在此处查看有关facescontext的更多信息

http://illegalargumentexception.blogspot.com/2011/12/jsf-mocking-facescontext-for-unit-tests.html http://illegalargumentexception.blogspot.com/2011/12/jsf-mocking-facescontext-for-unit-tests.html

and here is the javadoc http://docs.oracle.com/javaee/6/api/javax/faces/context/FacesContext.html 这是javadoc http://docs.oracle.com/javaee/6/api/javax/faces/context/FacesContext.html

To add to @JasonD, Its advised to passing FacesContext#getCurrentInstance() as to calling it, since it has performance and functionality implication. 要添加到@JasonD,它建议传递FacesContext#getCurrentInstance()进行调用,因为它具有性能和功能含义。

Also don't bundle validation logic directly into your code, use JSR 349 - Bean Validation- as it defines a metadata model and API for entity and method validation. 另外,不要将验证逻辑直接捆绑到您的代码中,请使用JSR 349(Bean验证),因为它定义了用于实体和方法验证的元数据模型和API。

Hibernate has an implementation and a good documentation on testing validation constraints Hibernate有一个实现和关于测试验证约束的良好文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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