zoukankan      html  css  js  c++  java
  • junit4测试 Spring MVC注解方式

    本人使用的为junit4进行测试

    spring-servlet.xml中使用的为注解扫描的方式

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
        <context:annotation-config />
    
        <!-- 自动扫描所有注解该路径 -->
        <context:component-scan base-package="com.carrefour" />
        <!--
            主要作用于@Controller,激活该模式, 下面是一种简写形式,完全可以手动配置替代这种简写形式,
            它会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter,
            是spring MVC为@Controllers分发请求所必须的
        -->
        <mvc:annotation-driven />

    控制层(action层)使用@Controller

    业务层(service层)使用@Service,

    持久层(Dao层)使用@Repository

    也可以三层统一使用@Component也是可以的,但推荐使用上方的方式,便于区分

    详细介绍也可查看链接

    http://blog.csdn.net/yi3040/article/details/6447289

    junit测试

    package com.example.thread.test;
    
    import java.util.List;
    
    import org.apache.xbean.spring.context.FileSystemXmlApplicationContext;
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    
    import com.carrefour.dao.CheckP4AccountDao;
    import com.carrefour.model.SimpleModel;
    
    public class CheckAccountThTest {
      
        private CheckP4AccountDao checkP4AccountDao ;
    
        @Before
        public  void init() throws Exception {
          // 加载spring容器,此种方式为绝对路径方式,spring配置文件在WEB-INF下
    // ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{ // "E:\Workspaces\MyEclipse 8.6\carrefour\WebRoot\WEB-INF\spring-servlet.xml" // });
          // 加载spring容器,此种方式为相对路径,spring配置文件在WEB-INF下 ApplicationContext context = new FileSystemXmlApplicationContext( "WebRoot/WEB-INF/spring-servlet.xml" );      // 获取Dao层实现类的Bean,此处注意,由于使用的为注解形式,spring会默认将类的首字母小写作为Bean的名称
           checkP4AccountDao
    = (CheckP4AccountDao) context.getBean("checkP4AccountDaoImpl"); } @Test public void testAll() throws Exception{
        // 此处可以根据实际情况调用dao层的方法 List
    <SimpleModel> list = checkP4AccountDao.getAllRegion(); System.out.println(list.size());
         System.out.println(finish);
      }

    测试方法还有很多中,此种方法个人感觉较好用和理解,如有喜欢可以收藏

  • 相关阅读:
    Gin 使用 websocket
    7天用Go从零实现Web框架Gee教程
    docker-compose 搭建 Redis Sentinel 测试环境
    关闭禁用 Redis 危险命令
    Redis Cluster 设置密码
    使用 twine 上传自己的 python 包到 pypi
    Redis 5.0.7 讲解,单机、集群模式搭建
    Redis 单机模式,主从模式,哨兵模式(sentinel),集群模式(cluster),第三方模式优缺点分析
    django 重写 mysql 连接库实现连接池
    编译安装httpd-2.4
  • 原文地址:https://www.cnblogs.com/mengzw/p/4060125.html
Copyright © 2011-2022 走看看