1、测试代码的包名应与所要测试的Service包名一致,如果要测试的Service为:org.mybatis.jpetstore.service.AccountService,那么测试的类为:org.mybatis.jpetstore.service.AccountServiceTest,名字只能是这样的。

2、注入配置文件的方式为

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")

3、配置文件一定要放到test下面的resource中去,否则在Junit测试中像jdbc.properties这样的配置文件无法读到

 

4、applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright 2010 The myBatis Team Licensed under the Apache License, Version 
	2.0 (the "License"); you may not use this file except in compliance with 
	the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
	Unless required by applicable law or agreed to in writing, software distributed 
	under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
	OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
	the specific language governing permissions and limitations under the License. -->

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">





	<!-- DataSource -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>WEB-INF/jdbc.properties</value>
			</list>
		</property>
	</bean>

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${driverClass}" />
		<property name="jdbcUrl" value="${url}" />
		<property name="user" value="${username}"></property>
		<property name="password" value="${password}"></property>
	</bean>




	<!-- transaction manager, use JtaTransactionManager for global tx -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- enable component scanning and autowire (beware that this does not enable 
		mapper scanning!) -->
	<context:component-scan base-package="org.mybatis.jpetstore.service" />

	<!-- enable transaction demarcation with annotations -->
	<tx:annotation-driven />

	<!-- define the SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="typeAliasesPackage" value="org.mybatis.jpetstore.domain" />
	</bean>

	<!-- scan for mappers and let them be autowired -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="org.mybatis.jpetstore.persistence" />
	</bean>
</beans>

 5、jdbc.properties

driverClass=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@192.168.1.99:1521:orcl
username=BLBJ
password=hzzy0123

 6、测试

package org.mybatis.jpetstore.service;

import static org.junit.Assert.assertEquals;
import net.sourceforge.stripes.integration.spring.SpringBean;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mybatis.jpetstore.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AccountServiceTest {

	@Autowired
	private transient AccountService accountService;

	@Before
	public void init() {
		// ClassPathXmlApplicationContext ctx = new
		// ClassPathXmlApplicationContext(
		// "applicationContext.xml");
		System.out.println("*******************************************test");
	}

	@Test
	public void testLogin() {
		String username = "j2ee";
		String password = "j2ee";
		Account account = accountService.getAccount(username, password);
		assertEquals(account.getUsername(), username);
		assertEquals(1, 1);
	}

	@After
	public void doAfter() {

	}
}

 7、项目基础框架的项目下载地址:

https://codeload.github.com/mybatis/jpetstore-6/zip/master