使用Mockito实例化HttpServletRequest的问题

时间:2012-09-21 17:18:34

标签: java testing mockito

以下行失败,但有例外:

HttpServletRequest req = mock(HttpServletRequest.class);

例外:

java.lang.ClassFormatError: Absent Code attribute in method 
that is not native or abstract in class file 
javax/servlet/http/Cookie

有人遇到过这个问题吗?

UPD 找到了解决方案here

1 个答案:

答案 0 :(得分:3)

您应该将Servlet API的实现添加到测试/运行时类路径中。我建议使用这种依赖:

<dependency>
  <groupId>org.glassfish</groupId>
  <artifactId>javax.servlet</artifactId>
  <version>3.0</version>
  <scope>test</scope>
</dependency>