Spring工程,测试类编写demo

来源:互联网 发布:js 图片跟随鼠标移动 编辑:程序博客网 时间:2024/06/16 07:15
public class TestService {

ApplicationContext ac;
private UserService userService;
@Before
public void setUp() throws Exception {
ac = new ClassPathXmlApplicationContext("classpath:spring/spring.xml");
userService = ac.getBean(UserService.class);
}


/**
* 获取所有bean名字
*/
@Test
public void testUserService() {
int count = ac.getBeanDefinitionCount();

String[] beans = ac.getBeanDefinitionNames();
for (String string : beans) {
System.out.println(string);
}

}


/**
* 测试添加
*/
@Test
public void testAdd() {
UserMapper mapper = ac.getBean(UserMapper.class);
User user = new User();
user.setUsername("qqqq");
user.setPassword("1111123");
mapper.insert(user);

}

/**
* 集合查询
*/
@Test
public void testGetList(){
List<User> users = userService.getList();
for (User user : users) {
System.out.println(user);
}

}

}

原创粉丝点击