1.下载所需jar包:
spring-test-4.0.2.RELEASE.jar
junit-4.11.jar
Maven
org.springframework
spring-test
4.0.2.RELEASE
junit
junit
4.11
test
以上2个包必须加入spring-test.jar集成了spring测试环境,缺了包不能正常注入。一般测试注入不成功应该是缺这包导致。junit 测试包尽可能应用高点版本,我所应用的是4.11版
@RunWith(SpringJUnit4ClassRunner.class)
org.springframework.test.annotation
包中提供了常用的Spring特定的注解集,如果你在Java5或以上版本开发,可以在测试中使用它@ContextConfiguration(locations={"classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
public class cTest {
@Autowired
public AdminService adminService;
@Test
public void test() {
Admin a = new Admin();
a.setUsername("user");
a.setPassword("sds");
adminService.insertSelective(a);
}
}
总结不好多多担待,文章只单纯个人总结,如不好勿喷,技术有限,有错漏麻烦指正提出。本人QQ:373965070