junit进行单元测试通常有两种:
1. 不使用spring封装的junit,手动加载
package com.puhua.test;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import com.puhua.model.Users;
import com.puhua.service.UsersService;
public class TestUsersMapper extends AbstractJUnit4SpringContextTests{
private UsersService usersService;
@SuppressWarnings("resource")
@Before
public void before(){
ApplicationContext context = new ClassPathXmlApplicationContext(new String []{"classpath*/spring-mybatis.xml","classpath*:/spring-mvc.xml"});
System.out.println("_________________________");
usersService = (UsersService) context.getBean("usersServiceImpl");
}
@Test
public void testSelectByPrimaryKey(){
Long id = new Long(1);
Users user = usersService.selectByPrimaryKey(id);
System.out.println(user);
}
}
2.通过spring封装的junit实现注解测试:
package com.puhua.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.puhua.model.Users;
import com.puhua.service.UsersService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/spring-mybatis.xml","classpath*:/spring-mvc.xml"})
@WebAppConfiguration
public class SpringTest extends AbstractJUnit4SpringContextTests {
@Resource
private UsersService usersService;
@Test
public void testSelectByPrimaryKey(){
Long id = new Long(1);
Users user = usersService.selectByPrimaryKey(id);
System.out.println(user.getUserName());
}
}
下面重点讲通过spring注解方式进行junit测试出现的问题及解决方法:
1.基类上的三个注解一个都不能少:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/spring-mybatis.xml","classpath*:/spring-mvc.xml"})
@WebAppConfiguration
其中@ContextConfiguration用来加载spring的配置文件,如果有多个,需要以逗号分开,也可以用通配符如:
@ContextConfiguration(locations = { "classpath*:/spring-*.xml"})
但需要重点注意的是:classpath后面的*号不能少,如果没有星号,会报如下错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.puhua.test.SpringTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.puhua.service.UsersService com.puhua.test.SpringTest.usersService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.puhua.service.UsersService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at ...
除此之外,@WebAppConfiguration的作用是:表明该类会使用web应用程序的默认根目录来载入ApplicationContext, 默认的更目录是”src/main/webapp”, 如果需要更改这个更目录可以修改该注释的value值。如果没加此注解,报错和上面一样
2.一切准备就绪,开始测试,发现又报错了,报错为
Can't find bundle for base name javax.servlet.LocalStrings, locale zh_CN
原因是缺少WEB服务运行的相关包,右键项目->build path->add Library->Server Runtime->选择你的tomcat,OK!
再次测试,可以查到数据!