路径识别不了,test跑就报错

不知道这个有没有用

你在web.xml中设置加载applicationContext.xml文件路径了吗

你pom文件里面没加junit的依赖吧

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
  </dependency>

如果你加了,那就是你的text类没有开启spring对junit的支持,下面是一个单元测试类,你是不是没有加runWith呀,我写的注释,你应该能看懂吧

//直接使用JUnit测试Spring项目,需要手动编码获取context工厂,需要手动从工厂中获取对象,测试繁
//        琐。
//        解决方案:使用Spring-test,简化了Spring项目的测试。
//开启Spring对Junit的支持
@RunWith(SpringJUnit4ClassRunner.class)
//设置配置文件的路径
@ContextConfiguration("classpath:applicationContext.xml")
public class AddressTest {
xxxxx
}

 

你mapper配置文件在哪

web.xml你根据我的再改改看看

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--告知springmvc,springmvc的配置文件的路径-->
    <init-param>
      <!--这个标签内容配置的是servlet里面的属性名,不能随便写-->
      <param-name>contextConfigLocation</param-name>
      <!--springmvc配置文件的地址。文件名可以根据情况调整-->
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

 

@ContextConfiguration(locations = {"classpath*:/*.xml"})

 

在test目录下建一个resources,把配置文件copy进去。