gpt4 book ai didi

java - 如何创建 ServletContext 的模拟属性?

转载 作者:太空宇宙 更新时间:2023-11-04 11:23:34 25 4
gpt4 key购买 nike

我有在数据库中添加用户的 Servlet。 Servlet 使用模块实例来处理数据库,他的别名是 DBJoint。 Servlet 从 ServletContext 获取 DBJoint 的实例。

@WebListener
public class ContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
final ServletContext servletContext =
servletContextEvent.getServletContext();

final DBJoint joint = new DBJointHandler(
"database_scripts",
"authentication_database");

servletContext.setAttribute("db", joint);
}
}

在每个 Servlet 中,当我需要使用数据库时,我调用 ServletContext 并通过键 "db" 获取 DBJoint

public class AddUserServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

req.setCharacterEncoding("UTF8");

try {

final boolean success = addUserInDatabase(req);

if (success) req.setAttribute("serverAnswer", EDIT_SUCCESS.get());
else req.setAttribute("serverAnswer", ERR_UNIQUE_L_P.get());

req.getRequestDispatcher(ANSWER.get())
.forward(req, resp);

} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* Addition user in database.
*
* @return true if addition success, else false.
*/
private boolean addUserInDatabase(final HttpServletRequest req)
throws SQLException {

final User user = getUserFromRequest(req);
return getDatabaseExecutor().addUserAndGetSuccess(user);
}

/**
* Extracts user's data from HttpServletRequest.
*
* @return user from request.
*/
private User getUserFromRequest(final HttpServletRequest req) {
return new User(
req.getParameter("name"),
req.getParameter("login"),
req.getParameter("password"),
req.getParameter("email"),
req.getParameter("role")
);
}

/**
* Get executor database requests.
*/
private ScriptExecutor getDatabaseExecutor() throws SQLException {
final DBJoint db = (DBJoint) getServletContext().getAttribute("db");
return db.getDBScriptExecutor();
}
}

我需要测试我的 Servlet,并使用模拟对象而不是原始对象:

在此测试中,我尝试验证数据库脚本执行器中 doPost 调用 addUser(User u) 的主体。但获得 NPE。

@Test
public void whenUserAddThenAddUserCall() throws ServletException, IOException, SQLException {

final AddUserServlet servlet = new AddUserServlet();

//mock http.
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);

User user = new User("name", "login", "password", "email");

//mock database.
ScriptExecutor executor = mock(ScriptExecutor.class);

DBJoint joint = mock(DBJointHandler.class);
when(joint.getDBScriptExecutor()).thenReturn(executor);


final ServletContext context = request.getServletContext();
//In this place I get NPE but why?
context.setAttribute("db", joint);

servlet.doPost(request, response);

verify(executor).addUser(user);
}

为什么我有NullPointerException?如何测试这个类?谢谢!

最佳答案

您还需要包含这些

ServletContext context = mock(ServletContext .class)
when(request.getServletContext()).thenReturn(context );

关于java - 如何创建 ServletContext 的模拟属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44638809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com