使用Maven + Hibernate + SpringTest框架进行单元测试Demo
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>floyd.welsney</groupId> <artifactId>orm-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <repositories> <repository> <id>jboss-hibernate</id> <url>https://repository.jboss.org/nexus/content/groups/public/</url> </repository> </repositories> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>2.2.4</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate</artifactId> <version>3.2.6.GA</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>3.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>3.0.6.RELEASE</version> </dependency> </dependencies></project>
<bean id="jdbcDataSource" value="org.hsqldb.jdbc.JDBCDriver"></constructor-arg><constructor-arg name="url" value="jdbc:hsqldb:mem:xdb"></constructor-arg><constructor-arg name="username" value="SA"></constructor-arg><constructor-arg name="password" value=""></constructor-arg> </bean> <bean id="simpleJdbcTemplate" ref="jdbcDataSource"></constructor-arg> </bean><bean id="hibernateSessionFactory"ref="jdbcDataSource" /><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.HSQLDialecthibernate.show_sql=truehibernate.use_sql_comments=truehibernate.format_sql=true</value></property><property name="mappingResources"><list> <value></value> </list></property></bean><bean id="hibernateTemplate" ref="hibernateSessionFactory"></property></bean>?
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations="classpath:applicationContext.xml")public class BaseSpringManagedContextJunitTest {protected final Logger logger = LogManager.getLogger(this.getClass());@Autowiredprotected SimpleJdbcTemplate simpleJdbcTemplate;@Autowiredprotected HibernateTemplate hibernateTemplate;protected final void execSqlScript(String scriptPath) {Resource resource = new ClassPathResource(scriptPath);SimpleJdbcTestUtils.executeSqlScript(simpleJdbcTemplate, resource, false);}@Beforepublic void setUp() {execSqlScript("floyd/welsney/test/sql/schema.sql");execSqlScript("floyd/welsney/test/sql/data.sql");logger.debug("hsqldb is setup");}@Afterpublic void tearDown() {execSqlScript("floyd/welsney/test/sql/clear.sql");logger.debug("hsqldb is tearDown");}@Testpublic void testXXXX() {logger.debug("Perform unit test");}}?
说明一下代码,
指定◎ContextConfiguration的Location来加载Spring管理的上下文环境配置文件。
◎Autowired说明需要注入的外部依赖。
◎Before用来加载全新的DB Schma和相应的数据。
◎After清理DB环境的脏数据,以为下一次test准备。
?
最后执行命令mvn test即可,Eclipse下可以Alt+Shift+X+T运行
1 楼 zmoxga 2012-01-15 log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).