51Testing软件测试论坛

标题: ssm中spring单元测试出现的问题 need your help [打印本页]

作者: 测试积点老人    时间: 2021-7-28 13:16
标题: ssm中spring单元测试出现的问题 need your help
  1. 20:38:24.001 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@420745d7] to prepare test instance [com.lzw.test.MvcTest@7e11ab3d]
  2. java.lang.IllegalStateException: Failed to load ApplicationContext
  3. Caused by: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/dbconfig.properties]
  4. Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/dbconfig.properties]
  5. java.lang.IllegalStateException: Failed to load ApplicationContext
  6. Caused by: org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/dbconfig.properties]
  7. Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/dbconfig.properties]
复制代码
xml配置文件:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xmlns:aop="http://www.springframework.org/schema/aop"
  6.        xmlns:tx="http://www.springframework.org/schema/tx"
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
  8.                             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  9.     <context:component-scan base-package="com.lzw">
  10.         <context:exclude-filter type="annotation"
  11.                                 expression="org.springframework.stereotype.Controller" />
  12.     </context:component-scan>

  13.     <!-- 配置jdbc 引入外部jdbc信息 -->
  14.     <context:property-placeholder location="dbconfig.properties"/>
  15.     <bean id="duridDataSource" class="com.alibaba.druid.pool.DruidDataSource">
  16.         <property name="url" value="${jdbc.url}"></property>
  17.         <property name="driverClassName" value="${jdbc.driverClassName}"></property>
  18.         <property name="username" value="${jdbc.username}"></property>
  19.         <property name="password" value="${jdbc.password}"></property>
  20.     </bean>

  21.     <!-- 整合mybatis -->
  22.     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  23.         <property name="configLocation" value="mybatis-config.xml"/>
  24.         <property name="dataSource" ref="duridDataSource"/>
  25.         <property name="mapperLocations" value="mapper/*.xml"/>
  26.     </bean>
  27.     <!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
  28.     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  29.         <property name="basePackage" value="com.lzw.crud.dao"/>
  30.     </bean>
  31.     <!-- 配置一个可以执行批量的sqlSession -->
  32.     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
  33.         <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
  34.         <constructor-arg name="executorType" value="BATCH"/>
  35.     </bean>
  36.     <!-- 事务控制配置 -->
  37.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  38.         <property name="dataSource" ref="duridDataSource"/>
  39.     </bean>
  40.     <!--开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式)  -->
  41.     <aop:config>
  42.         <!-- 切入点表达式 -->
  43.         <aop:pointcut expression="execution(* com.lzw.crud.service..*(..))" id="txPoint"/>
  44.         <!-- 配置事务增强 -->
  45.         <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
  46.     </aop:config>
  47.     <!--配置事务增强,事务如何切入  -->
  48.     <tx:advice id="txAdvice" transaction-manager="transactionManager">
  49.         <tx:attributes>
  50.             <!-- 所有方法都是事务方法 -->
  51.             <tx:method name="*"/>
  52.             <!--以get开始的所有方法  -->
  53.             <tx:method name="get*" read-only="true"/>
  54.         </tx:attributes>
  55.     </tx:advice>
  56. </beans>
复制代码
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.        xmlns:context="http://www.springframework.org/schema/context"
  5.        xmlns:mvc="http://www.springframework.org/schema/mvc"
  6.        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
  7.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  8.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  9. <!-- springmvc的配置文件 网站跳转逻辑的控制配置 -->
  10.     <context:component-scan base-package="com.lzw">
  11.     <!-- 只扫描控制器 -->
  12.         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  13.     </context:component-scan>
  14. <!-- 配置视图解析器 页面返回 -->
  15.     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  16.         <property name="prefix" value="/WEB-INF/views"></property>
  17.         <property name="suffix" value=".jsp"></property>
  18.     </bean>
  19. <!-- spring处理不了的交给tomcat -->
  20.     <mvc:default-servlet-handler/>
  21. <!-- springmvc更加高级的功能 JSR303校验 快捷的ajax 映射动态请求 -->
  22.     <mvc:annotation-driven/>
  23. </beans>
复制代码
  1. **Service以及Controller**
  2. package com.lzw.crud.controller;
  3. import com.github.pagehelper.PageHelper;
  4. import com.github.pagehelper.PageInfo;
  5. import com.lzw.crud.bean.Employee;
  6. import com.lzw.crud.service.EmployeeService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.ui.Model;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import java.util.List;

  13. @Controller
  14. public class EmployeeController{
  15.     @Autowired
  16.     EmployeeService employeeService;
  17.     // 查询员工数据
  18.     @RequestMapping("/emps")
  19.     public String getEmps(@RequestParam(value = "pn", defaultValue = "1") Integer pn, Model model){
  20.         // 分页查询插件 插第几页 几条数据
  21.         PageHelper.startPage(pn,5);
  22.         List<Employee> emps =   employeeService.getAll();
  23.         // 包装查询结果 只需要将pageinfo交给页面
  24.         PageInfo page = new PageInfo(emps);
  25.         model.addAttribute("pageInfo",page);

  26.         return "list";
  27.     }
  28. }

  29. package com.lzw.crud.service;
  30. import com.lzw.crud.bean.Employee;
  31. import com.lzw.crud.dao.EmployeeMapper;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.stereotype.Service;
  34. import java.util.List;

  35. @Service
  36. public class EmployeeService{
  37.     @Autowired
  38.     EmployeeMapper employeeMapper;
  39.     public List<Employee> getAll(){
  40.         return employeeMapper.selectByExampleWithDept(null);
  41.     }
  42. }
复制代码
单元测试


  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. // web ioc也能拿到
  3. @WebAppConfiguration
  4. @ContextConfiguration(locations = {"classpath:applicationContext.xml","file:D:/Teaching/Idea/SpringBoot/ssm_crud/ssm_01/web/WEB-INF/dispatcherServlet-servlet.xml"})
  5. public class MvcTest{
  6.     // 传入springmvc的ioc
  7.     @Autowired
  8.     WebApplicationContext context;
  9.     // 虚拟mvc请求 获取处理结果
  10.     MockMvc mockMvc;
  11.     @Before
  12.     public void initMockMvc(){
  13.         mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
  14.     }
  15.     @Test
  16.     public void testPage() throws Exception{
  17.         // 模拟请求拿到返回值
  18.         MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/emps").param("pn", "1")).andReturn();
  19.         // 请求成功后 请求域中会有pageInfo 取出该值进行验证
  20.         MockHttpServletRequest request = result.getRequest();
  21.         PageInfo pi = (PageInfo) request.getAttribute("pageInfo");
  22.         System.out.println("当前页码:"+pi.getPageNum());
  23.         System.out.println("总页码:"+ pi.getPages());
  24.         System.out.println("总记录数:"+pi.getTotal());
  25.         int[] nums = pi.getNavigatepageNums();
  26.         for (int i :
  27.                 nums) {
  28.             System.out.println(" "+i);
  29.         }
  30.         List<Employee> list = pi.getList();
  31.         for (Employee e :
  32.                 list) {
  33.             System.out.println("ID:"+e.getEmpId()+"==>Name"+e.getEmpName());
  34.         }
  35.     }

  36. }
复制代码
请君助我 困扰我一下午了


作者: qqq911    时间: 2021-7-29 10:26
检查下配置文件路径dbconfig.properties 不存在
作者: bellas    时间: 2021-7-29 11:57
来学习




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2