SSM框架中Junit的应用

阅读更多

applicationContext.xml




	
	
    

	
		
			${driverClassName}
		
		
			${url}
		
		
			${username}
		
		
			${password}
		
		
			${initialSize}
		
		
			${minIdle}
		
		
			${maxActive}
		
		
			${maxIdle}
		
	

	
	
		
	
	
	

	
    
		
		
		
	

	
	
		
		
	

	
	
	
		
		
		
	

 

mybatis-config.xml





    
    
         
    
    
	
		
			
			
				
				
				
				
			
		
	

	
		
	


  

 

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
	// 多个相同类型时选择注入哪一个
	@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 ;
	}
}

 

 

你可能感兴趣的:(SSM框架中Junit的应用)