spring自身模块的测试功能如下:
package sy.test;
import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sy.model.User; import sy.service.UserServiceI;
//spring结合junit利用spring-test包测试
//1、加上注解或public class TestMybitsSpring extends SpringJUnit4ClassRunner @RunWith(SpringJUnit4ClassRunner.class) //2、或者@ContextConfiguration(locations = {"spring.xml" , "spring-mybatis.xml"}) //下面是更精确的写法: //有了这两个注解,就不需要@Before和private ApplicationContext ac; @ContextConfiguration(locations = {"classpath:spring.xml" , "classpath:spring-mybatis.xml"}) public class TestMybitsSpring { //3、定义userService属性 //4、Alt+Shift+S生成get和set方法 private UserServiceI userService; public UserServiceI getUserService() { return userService; } //5、加@Autowired表示自动注入 @Autowired public void setUserService(UserServiceI userService) { this.userService = userService; }
//6、@Test是junit测试方法的定义 @Test public void test1() { //7、通过User模型接受userService获取对象u User u = userService.getUserById(1); //8、利用java的System类打印u对象 System.out.println(u); System.out.println(u.getName()); }
}
|