Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取JUnit中的java.lang.ClassNotFoundException:javax.servlet.ServletContext_Java_Spring_Spring Mvc_Junit - Fatal编程技术网

获取JUnit中的java.lang.ClassNotFoundException:javax.servlet.ServletContext

获取JUnit中的java.lang.ClassNotFoundException:javax.servlet.ServletContext,java,spring,spring-mvc,junit,Java,Spring,Spring Mvc,Junit,我正在应用程序中使用SpingMVC,并为DAO编写JUnit测试用例。运行测试时,我收到错误:java.lang.ClassNotFoundException:javax.servlet.ServletContext 在stacktrace中,我看到此错误是在getApplicationContext期间引起的。在我的applicationContext中,我没有定义任何servlet。Servlet映射只在web.xml中完成,所以我不明白为什么会出现这个错误 这是我的application

我正在应用程序中使用SpingMVC,并为DAO编写JUnit测试用例。运行测试时,我收到错误:
java.lang.ClassNotFoundException:javax.servlet.ServletContext

在stacktrace中,我看到此错误是在
getApplicationContext
期间引起的。在我的
applicationContext
中,我没有定义任何servlet。Servlet映射只在
web.xml
中完成,所以我不明白为什么会出现这个错误

这是我的
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"

xmlns:tx="http://www.springframework.org/schema/tx">

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/testdb"/>
    <property name="user" value="username"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
            <prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/myWorld_test</prop>
            <prop key="hibernate.connection.username">username</prop>
        </props>
    </property>
    <property name="packagesToScan">
        <list>
            <value>com.myprojects.pojos</value>
        </list>
    </property>
</bean>

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<context:component-scan base-package="com.myprojects"/>

<context:annotation-config/> 
<mvc:annotation-driven/>
</beans>
测试等级:

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class UserServiceTest {

@Autowired
private UserService service;

public UserServiceTest() {
}

@BeforeClass
public static void setUpClass() {
}

@AfterClass
public static void tearDownClass() {
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}
}

甚至在编写任何测试方法之前,我就遇到了这个错误。

您的
应用程序上下文有一个xml文件,该文件中有一个
标记。此标记加载不同的web相关资源(视图解析器、处理程序映射等),因此需要servlet api可用

您应该已经在类路径上拥有ServletAPI,作为maven中提供的依赖项

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

javax.servlet
javax.servlet-api
3.0.1
假如

接下来,您可能需要删除
标记,并将其放在单独的配置文件中。这也是一个标签,通常应由
DispatcherServlet
加载。(这里我假设applicationContext.xml默认由
ContextLoaderListener
加载。)

Maven无法加载3.0.1版本中的
javax.servlet api
:/

这个依赖关系解决了我的问题:

    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-servlet_3.0_spec</artifactId>
        <version>1.0</version>
        <scope>test</scope>
    </dependency>

org.apache.geronimo.specs
geronimo-servlet_3.0_规范
1
测试

它包含javax.servlet的所有类。

对于我来说,我添加了@WebAppConfiguration,它解决了我的问题

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestConfig.class})
@WebAppConfiguration
public class WebAppTest {


    @Test
    public void test() throws Exception {

    }

}
一,


这些建议帮助了我。

对我来说,原因是tomcat版本。我将tomcat版本从7.5改为8.5,问题解决了。

尝试删除范围。。。为我工作

转到pom.xml并删除以下内容:

提供

最后,结果如下所示:

<dependencies>
    <dependency>
        <groupId>org.glassfish.metro</groupId>
        <artifactId>webservices-rt</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
    </dependency>
</dependencies>

org.glassfish.metro
Web服务rt
2.3
爪哇
javaeewebapi
7

祝你好运

由于
这要求web类在类路径上可用。@M.Deinum在这种情况下,您建议将其移动到servlet上下文而不是根应用程序上下文吗?不,您需要将servlet API添加到类路径。你是如何执行测试的?专家蚂蚁?格拉德尔?您的IDE?您可以添加依赖项,也可以将其拆分为不同的上下文(通常我都这样做:)。
中已经暗示了一些其他建议。
hibernate.connection
属性在注入
DataSource
时是无用的,因此请删除它们。不建议再使用
HibernateTemplate
,直接使用
SessionFactory
。maven存储库中有一个名为“servlet api”而不是“javax.servlet api”的artifactId。这个版本没有3.0.1版本。我使用了上面的依赖项,它解决了我的“java.lang.ClassNotFoundException:javax.servlet.ServletContext”问题。我尝试了它,但它对我不起作用,但是我发现它说我们需要servlet API的实现,是吗?我还必须添加
来解决异常。不起作用,在提供或测试范围的单元测试中仍未找到
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/spring-mvc.xml")
@WebAppConfiguration
<dependencies>
    <dependency>
        <groupId>org.glassfish.metro</groupId>
        <artifactId>webservices-rt</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
    </dependency>
</dependencies>