spring+junit进行单元测试

来源:互联网 发布:网络舆情汇报 编辑:程序博客网 时间:2024/04/30 20:15

1.junit 注解加载,进行测试。需添加@RunWith 和@ContextConfiguration注解,直接上代码

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath:applicationContext.xml","classpath:mybatis-config.xml","classpath:spring-mvc.xml" }) // 加载配置public class TestDaoSupport {@Resource(name="userService")private UserService userService;@Testpublic void saveUserByDaoSupport() throws Exception {User user = new User();user.setUserName("lisi");user.setPassword("12345");user.setAddress("北京市朝阳区"); userService.saveUser(user);}}

2.需要的jar包spring-test,maven添加法:

       <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.1.7.RELEASE</version><scope>compile</scope></dependency>

好了 ,这样就可以开始测试了。

0 0