2014-04-30 66 views
5
  1. 我试图用使用 模拟对象错误创建对象MockHttpServletResponse

  2. 我Maven的依赖冲刺测试编写Servlet的单元测试是:

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

我想测试像下面的Java servlet下面的书实用的TDD和接受TDD FO ř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响应误差=新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 
+0

测试的代码,就像你提供它上面,但它的工作原理对我来说,没有错误。 –

+0

您可以发布您的pom配置 – gyanu

+2

在POM:JUnit的4.7的javax.servlet-API 3.0.1,弹簧试验4.0.3,弹簧背景4.0.3 –

回答

6

我需要更换为JavaEE的-API与使用javax依赖性.selvlet-api如下:

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

所以我有同样的问题,除了我有j2ee-API 7列为依赖(提供)。我认为这包括了servlet api,但只要我像上面那样添加了servlet api +提供它解决了我的问题。为什么这个工作呢? – JoshC13

+0

看起来像j2ee-api只包含j2ee-api,可以用于编译。它不包含方法体,因此不能用于运行或部署。 for more http://www.mkyong.com/maven/how-to-download-j2ee-api-javaee-jar-from-maven/ – gyanu

+0

正确 - 我的理解是servlet-api只是一个子类,一套更大的j2ee-api。你是说这是不是这种情况? – JoshC13

1

按照Spring Framework Reference for Testing你应该使用注释自动装配你的嘲弄。在弹簧参考的例如:

`

@WebAppConfiguration 
    @ContextConfiguration 
    public class WacTests { 

    @Autowired WebApplicationContext wac; // cached 

    @Autowired MockServletContext servletContext; // cached 

    @Autowired MockHttpSession session; 

    @Autowired MockHttpServletRequest request; 

    @Autowired MockHttpServletResponse response; 

    @Autowired ServletWebRequest webRequest; 

    //... 
} 

`

一个不同的例子(不注解)可发现here

0

我有一个类似的问题,并通过添加这种依赖关系到我的POM解决它,也没有必要改变你的JavaEE的API来的javax.servlet

<dependency> 
<groupId>org.mortbay.jetty</groupId> 
<artifactId>servlet-api-2.5</artifactId> 
<version>6.1.11</version> 
</dependency> 
+0

在java中,我们可以根据我们正在使用的服务器找到许多库实现,并且在您使用jetty实现的情况下。你也可以检查glassfish,tomcat等等。 – gyanu