无法在我的单元测试中初始化MockHttpServletRequest

时间:2014-06-18 06:34:56

标签: spring unit-testing maven

我很难让我的单元测试进行编译和执行。我收到以下错误。

java.lang.ClassFormatError: JVMCFRE074 no Code attribute specified; class=javax/servlet/ServletInputStream, method=<init>()V, pc=0
    at java.lang.ClassLoader.defineClass(ClassLoader.java:295)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:154)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:711)
    at java.net.URLClassLoader.access$400(URLClassLoader.java:92)
    at java.net.URLClassLoader$ClassFinder.run(URLClassLoader.java:1159)
    at java.security.AccessController.doPrivileged(AccessController.java:314)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:594)
    at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:743)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:711)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:325)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:690)
    at java.lang.J9VMInternals.verifyImpl(Native Method)
    at java.lang.J9VMInternals.verify(J9VMInternals.java:93)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:170)
    at com.bofa.ecom.olb.pipad.simulator.interceptor.SimulatorInterceptorTest.setup(SimulatorInterceptorTest.java:28)

我的单元测试源代码

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@RunWith(JUnit4.class)
public class SimulatorInterceptorTest {

  private SimulatorInterceptor simulatorInterceptor;

  private MockHttpServletRequest servletRequest;

  @Before
  public void setup() {
    this.simulatorInterceptor = new SimulatorInterceptor();
    this.servletRequest = new MockHttpServletRequest();
    ServletRequestAttributes requestAttributes =
        new ServletRequestAttributes(this.servletRequest);
    RequestContextHolder.setRequestAttributes(requestAttributes);
  }

  @Test
  public void testTrue() {

  }

}

我的maven属性如下:

    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>3.1.0.RELEASE</org.springframework.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-library</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${org.springframework.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.easymock</groupId>
        <artifactId>easymock</artifactId>
        <version>3.2</version>
        <scope>test</scope>
    </dependency>
    <!-- DB dependencies -->
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.3.2</version>
    </dependency>
    <!-- HTTP Session requires javaee api. -->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>6.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>17.0</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

0 个答案:

没有答案