spring mvc +junit测试方法

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class TestUser {
    
    @Autowired
    private UserService userService;
    
    @Test
    public void Test(){
	
	User user = userService.getUserByName("admin");
	
	Assert.assertNotNull(user);
    }
    
    @Test
    public void test() {
	ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"classpath:applicationContext.xml" });
	
	User user = userService.getUserByName("admin");
			
	Assert.assertNotNull(user);
    }

 步骤:

   1、导入spring-test-3.1.3.RELEASE.jar包(版本应与spring的jar相同,否则很容易出现版本冲突现象)
   2、导入junit相关jar包

   classpath路径默认为src下的文件

猜你喜欢

转载自gamehiboy.iteye.com/blog/2047972