`

spring-test环境搭建

 
阅读更多

1,我的目录结构

2,test/resource/spring-context.xml 与 src/resource/spring-context.xml  相同

    包括:扫描包、声明事务等

   

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

     <context:component-scan base-package="com.xxx.entity" />  
    <context:component-scan base-package="com.xxx.dao" />  
    <context:component-scan base-package="com.xxx.service" />  
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
        <property name="configLocation">
            <value>classpath:resources/hibernate.cfg.xml</value>
        </property>
        <property name="packagesToScan">  
            <list>  
                <value>com.xxx.entity</value>
            </list>  
        </property>  
    </bean>  


    <!-- 将事务与Hibernate关联 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory"/>
        </property>
    </bean>
     
<!--     事务(注解 ) -->
<!--     <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/> -->
    <tx:advice id="txAdvice" 
    	transaction-manager="transactionManager">
        <tx:attributes>
        	<tx:method name="*"/>
        </tx:attributes>
    </tx:advice>
    <aop:config>
    	<aop:pointcut expression="execution(* com.xxx.dao.impl.*Impl.*(..))"  id="myPointcut"/>
    	<aop:advisor advice-ref="txAdvice"
             pointcut-ref="myPointcut"/>
    </aop:config>
</beans>  

 注意:若session的建立(不知道改怎么说)在service层,且你只想测试dao层,请将上面标红的地方配置正确。test中的包结构要与src中的一致,若不一致请执行配置test下的spring-context.xml 。

否则会报:org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

3,UserDaoTest.java

 

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"classpath*:resource/spring-context.xml"})// 加载spring配置文件路径  
public class UserDaoTest {
	
	@Autowired
	private IUserDao userDao;
	
	@Test
	public void getUserById_Test(){
		System.out.println(userDao);
		User user = userDao.getUserById(1);
		System.out.println(user.getUserName());
	}
}

 加载的resource/spring-context.xml 是test下的。

@RunWith(SpringJUnit4ClassRunner.class) 一定要有。

4,spring-test方便我们对项目进行单元测试,而不必启动项目
 

  • 大小: 12.2 KB
分享到:
评论

相关推荐

    springboot版本jar包.zip

    搭建springboot项目要用到的springboot的一些jar包(spring-boot,spring-boot-actuator,spring-boot-actuator-autoconfigure,spring-boot-autoconfigure,spring-boot-configuration-processor,spring-boot-...

    spring MVC环境搭建 所需jar包.zip

    spring MVC环境搭建 所需jar包 ├── commons-logging-1.1.1.jar ├── jstl.jar ├── spring-aop-4.3.0.RELEASE.jar ├── spring-aspects-4.3.0.RELEASE.jar ├── spring-beans-4.3.0.RELEASE.jar ├...

    搭建springboot项目要用到的springboot的各个版本的jar包

    搭建springboot项目要用到的springboot的一些jar包(spring-boot,spring-boot-actuator,spring-boot-actuator-autoconfigure,spring-boot-autoconfigure,spring-boot-configuration-processor,spring-boot-...

    spring-hook-test.rar

    Spring框架中常见的hook扩展接口,基于Bean生命周期的容器管理扩展,常见的注解使用方法,为了简化环境搭建,采用了springboot框架。

    spring-boot-test:springboot学习笔记!

    可正常运行,只是了解Springboot的搭建步骤 使用IDEA开发工具,以Maven方式搭建的Springboot应用 使用Controller的方式: 讲述Springboot中读取核心配置文件的两种方法 讲述Springboot读取自定义配置文件的方法 使用...

    spring-boot-2.1.x.zip

    Spring Boot源码,直接下载下来,import进idea工具,以maven的方式导入,下载依赖可能时间有点长,然后运行spring-boot-project目录下的spring-boot-test右键run----&gt;all in java,大概需要半分钟,源码就编译构建好...

    基于框架的Web开发-spring基础环境搭建.doc

    1.1 Spring环境搭建 1 注意事项 在利用spring框架做web开发之前(引入spring MVC之前),仅创建java工程即可。 本文档仅引入基本的spring jar包,用于学习IOC基础。...创建一个Java工程springtest,右键工程名---proper

    spring-framework-4.2.0相关jar

    包含搭建spring相关jar包,aop,core,context,instrument,jdbc,web,test,webmvc等等

    spring源码分析环境搭建

    我编译的版本为spring framework 5.x版本,所用软件:idea ...坑1:plugin with id ‘java-test-fixtures’ not found;  解决办法gradle版本换成5.6.4。构建的 gradle版本不能低于5.6 也不能高于6.0. 坑2:gradle

    springboot学习思维笔记.xmind

    springboot学习笔记 spring基础 ... spring-boot-starter-test spring-boot-starter-thymeleaf spring-boot-starter-velocity spring-boot-starter-web spring-boot-starter-

    spring-cloud+注册中心eureka+配置中心demo

    spring-cloud+注册中心eureka+配置中心...访问地址http://localhost:3011/test/getStr。可以输出在配置中心读取到的配置。注册中心地址http://localhost:8080/。配置中心地址http://localhost:1201/my-client/master

    Spring框架

    搭建开发环境: 1&gt; 下载开发包,在工程中添加jar包; a&gt; IOC:spring-core.jar\spring-context.jar\spring-test.jar\spring-beans.jar\spring-expression.jar\commons-logging.jar b&gt; 准备一个配置文件 c&gt; 从容器中...

    spring-boot2.0-test001

    本仓库用于学习收集整理 掘金 Mr_初晨 Spring Boot2 教程及源码 Mr_初晨教程入口: Mr_初晨码云链接: 从零搭建自己的springboot后台框架 项目所需环境:jdk1.8,maven,mysql数据库 十四:添加shiro权限保护接口...

    spring_test.zip

    一时感兴趣,自己动手搭建的springmvc整合MyBatis框架,请注意没有使用Maven项目构建,可以支持json传参的方式,可以用来学习,多积累些经验。

    spring-boot-elasticsearch-sample.zip

    Elasticsearch入门(六):SpringBoot整合Elasticsearch项目搭建,基础实战,文章中的源代码。注意,文中我请求Controller所用的测试数据位于【src/test/java/com/zereao/es/http 目录下的 book.http 文件】中。不...

    使用MyEclipse创建Spring Boot项目demo

    上面的 N 步和下面的2步形成巨大的反差,这仅仅只是在开发环境搭建的这个方面。 Spring Boot 使测试变得更简单 Spring Boot 对测试的支持不可谓不强大,Spring Boot 内置了7种强大的测试框架: JUnit: 一个 Java ...

    springMVC+Spring+Mybatis+Maven整合代码案例

    3、搭建Spring配置:spring.xml(扫描service),config.properties,log4j.properties; 4、搭建spring和mybatis的配置:spring-mybatis.xm(扫描dao)) 5、测试Spring+mybatis的框架搭建,写单元测试JUnit,测试事务配置...

    spring+springmvc+mybatis+log4j+springtest的demo

    spring+springmvc+mybatis+log4j+springtest的demo,并且配置了多环境的配置文件,xml文件在源码中,配置了service层的事务处理,

    课程设计通用型后台管理系统项目

    为提升开发效率,搭建一个后台通用的管理系统脚手架系统,里面的内容都可以便捷式修改。 软件架构说明 - 前端框架:基于layui的kit-admin - 页面模板:采用FreeMaker 与 Jsp 双视图 - 后端主要框架:SpringMVC+...

    ssm整合案例.zip

    使用了spring-test与junit编写单元测试代码 使用了restful设计风格,以ajax发送put与delete类型请求 采用了双端校验,前端校验 JQuery,后端校验符合 JSR303 规范 还涉及到许多的开发细节:Bootstrap搭建页面,...

Global site tag (gtag.js) - Google Analytics