在做《Junit In Action》关于cactus和jetty结合进行集成测试的例子,看看源代码很简单,但总是运行不起来,一波三折了好几个小时才搞定。我用的cactus是1.8.1,闲言少叙,上源代码,2个类:

  待测试的servlet:

package junitbook.container;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SampleServlet extends HttpServlet
{
    public boolean isAuthenticated(HttpServletRequest request)
    {
        HttpSession session = request.getSession(false);

        if (session == null)
        {
            return false;
        }
      
        String authenticationAttribute =
            (String) session.getAttribute("authenticated");
          
        return Boolean.valueOf(
            authenticationAttribute).booleanValue();
    }
}