码迷,mamicode.com
首页 > 编程语言 > 详细

junit4测试 Spring MVC注解方式

时间:2014-10-29 18:43:43      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   使用   java   

本人使用的为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);
  }

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

 

junit4测试 Spring MVC注解方式

标签:style   blog   http   io   color   os   ar   使用   java   

原文地址:http://www.cnblogs.com/mengzw/p/4060125.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!