企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
> 本章介绍 JUnit4 在实际项目中的使用,实例说明如何使用 JUnit4 测试 Spring 和 Hibernate 的整合 1. Spring 与 Hibernate 的整合测试 ``` package com.dodoke.entity; import javax.persistence.*; @Entity @Table(name = "T_USER") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String password; public User() {} public User(String name, String password) { this.name = name; this.password = password; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", password='" + password + '\'' + '}'; } } ``` ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="tom" class="com.dodoke.entity.User"> <property name="name" value="tom"/> <property name="password" value="123456"/> </bean> <context:annotation-config/> <context:component-scan base-package="com.dodoke"/> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${mysql.driverClass}"/> <property name="jdbcUrl" value="${mysql.jdbcUrl}"/> <property name="user" value="${mysql.user}"/> <property name="password" value="${mysql.password}"/> <property name="initialPoolSize" value="${jdbc.initPoolSize}"/> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="packagesToScan"> <list> <value>com.dodoke.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="show_sql">true</prop> <prop key="format_sql">true</prop> <prop key="hbm2ddl.auto">update</prop> </props> </property> </bean> <!-- 配置txManager--> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans> ``` ``` package com.dodoke; import com.dodoke.entity.User; import org.hibernate.SessionFactory; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { private static ApplicationContext applicationContext; @BeforeClass public static void setUpBeforeClass() throws Exception { applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Test public void testUser() { User tom = (User) applicationContext.getBean("tom"); System.out.println(tom); } @Test public void testSessionFactory() { SessionFactory factory = (SessionFactory) applicationContext.getBean("sessionFactory"); System.out.println(factory); } } ``` ``` User{id=null, name='tom', password='123456'} org.hibernate.internal.SessionFactoryImpl@e72dba7 ```