Application of Junit in SSM Framework

applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"
	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
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<context:annotation-config />
	<context:component-scan base-package="cn.*" />
    <context:property-placeholder location="classpath:jdbc.properties" />

	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
			<value>${driverClassName}</value>
		</property>
		<property name="url">
			<value>${url}</value>
		</property>
		<property name="username">
			<value>${username}</value>
		</property>
		<property name="password">
			<value>${password}</value>
		</property>
		<property name="initialSize">
			<value>${initialSize}</value>
		</property>
		<property name="minIdle">
			<value>${minIdle}</value>
		</property>
		<property name="maxActive">
			<value>${maxActive}</value>
		</property>
		<property name="maxIdle">
			<value>${maxIdle}</value>
		</property>
	</bean>

	<!-- Define global transaction control-->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- Open annotation method to declare transaction-->
	<tx:annotation-driven />

	<!-- Define SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mapperLocations" value="classpath*:com/ssm/mapping/*.xml" />
		<property name="typeAliasesPackage" value="com.mybatis.model" />
	</bean>

	<!-- Automatically scan mapper, allowing automatic injection (matching according to type), no need to configure mapper one by one -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ssm.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>

	<!-- The configuration here is the annotation + automatic scanning method in spring, which is much simpler than the XML file configuration. -->
	<!-- @Repository: DAO layer components, @Service: Business layer components, @Controller: Control layer components -->
	<context:component-scan base-package="com.ssm"
		use-default-filters="false">
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Controller" />
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Service" />
		<context:include-filter type="annotation"
			expression="org.springframework.stereotype.Repository" />
	</context:component-scan>
</beans>

 

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!-- mybatis alias definition -->
   <typeAliases>
        <typeAlias alias="User" type="com.ssm.model.User"/>
    </typeAliases>
    
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/lucky" />
				<property name="username" value="cool" />
				<property name="password" value="cool" />
			</dataSource>
		</environment>
	</environments>

	<mappers>
		<mapper resource="com/ssm/mapping/UserMapper.xml" />
	</mappers>

</configuration>

  

 

LoginServiceImplTest.java

/**
 *
 */
package com.ssm.service.impl;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.ssm.model.UserKey;
import com.ssm.service.LoginService;

/**
 * @author guanhw
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:mybatis-config.xml"})
public class LoginServiceImplTest {
	
	@Autowired
	// Choose which one to inject when multiple of the same type
	@Qualifier("loginServiceImpl")
    public LoginService loginService;  

	/**
	 * Test method for {@link com.ssm.service.impl.LoginServiceImpl#IsLogin(com.ssm.model.UserKey)}.
	 */
	@Test
	public void testIsLogin() {
		UserKey userKey = new UserKey();
		userKey.setUsername("cool");
		userKey.setPassword("cool");
		boolean selResult = loginService.IsLogin (userKey);
		
		assertEquals(selResult, true);
	}

}

 

TestMapperUser.java

package com.ssm.test;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.ssm.dao.UserMapper;
import com.ssm.model.User;
import com.ssm.model.UserKey;
import com.ssm.utils.MyBatisUtil;

public class TestMapperUser {
	static SqlSessionFactory sqlSessionFactory = null;

	static {
		sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
	}

	void testQueryByUserNameAndPassword() {

		SqlSession sqlSession = sqlSessionFactory.openSession();

		try {
			UserMapper mapper = sqlSession.getMapper(UserMapper.class);

			UserKey userKey = new UserKey();
			userKey.setUsername("cool");
			userKey.setPassword("cool");
			User user = mapper.selectByPrimaryKey(userKey);

			System.out.println(
					user.getUsername() + "," + user.getFullname());
		} finally {
			sqlSession.close();
		}
	} // end of testQueryByCondition
}

 

MyBatisUtils.java

package com.ssm.utils;

import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
	
	private final static SqlSessionFactory sqlSessionFactory ;

	static {
		String resouce = "mybatis-config.xml";
		
		Reader reader = null;
		
		try {
			reader = Resources.getResourceAsReader(resouce);
			
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}

		sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
	}

	public static SqlSessionFactory getSqlSessionFactory() {
		return sqlSessionFactory ;
	}
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326114814&siteId=291194637