gpt4 book ai didi

java - 为单元测试创​​建对象 MockHttpServletResponse 时出错

转载 作者:搜寻专家 更新时间:2023-10-31 19:33:18 25 4
gpt4 key购买 nike

  1. 我尝试使用 sprint-test 为 Servlet 编写单元测试模拟对象

  2. 我的 Maven 依赖项是:

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.0.3.RELEASE</version>
    </dependency>

我只想测试 java servlet,如下面的书 PRACTICAL TDD AND ACCEPTANCE TDD对于 JAVA 开发人员:

package sample;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {
private boolean isValid;

/**
*
*/
private static final long serialVersionUID = 2473252741884321641L;

@Override
public void init() throws ServletException {
super.init();

}

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String user = req.getParameter("j_username");
String pass = req.getParameter("j_password");
if (isValidLogin(user, pass)) {
resp.sendRedirect("/frontpage");
req.getSession().setAttribute("username", user);
} else {
resp.sendRedirect("/invalidlogin");
}

}

private boolean isValidLogin(String user, String pass) {
return isValid;
}

public void setValid(boolean isValid) {
this.isValid = isValid;
}

}

我的代码是:

    package sample;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

public class SprintTestProb {

@Test
public void wrongPasswordShouldRedirectToErrorPage() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.addParameter("j_username", "gyanu");
request.addParameter("j_password", "wrongpassword");
LoginServlet login = new LoginServlet();
login.setValid(false);
login.doPost(request, response);
assertEquals("/invalidlogin", response.getRedirectedUrl());
}

}

我在行 MockHttpServletResponse response = new MockHttpServletResponse(); 上出错如下:

java.lang.ExceptionInInitializerError
at org.springframework.mock.web.MockHttpServletResponse.<init>(MockHttpServletResponse.java:76)
at sample.SprintTestProb.wrongPasswordShouldRedirectToErrorPage(SprintTestProb.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
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:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.util.MissingResourceException: Can't find bundle for base name javax.servlet.LocalStrings, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499)
at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322)
at java.util.ResourceBundle.getBundle(ResourceBundle.java:721)
at javax.servlet.ServletOutputStream.<clinit>(ServletOutputStream.java:87)
... 25 more

最佳答案

我需要用 javax.selvlet-api 替换 javaee-api 的依赖项,如下所示:

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

关于java - 为单元测试创​​建对象 MockHttpServletResponse 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23386234/

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