junit spring demo

来源:互联网 发布:js数字和字符串计算 编辑:程序博客网 时间:2024/05/24 06:04
package com.scottwong.test;import java.util.List;import org.junit.BeforeClass;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.scottwong.bean.Employee;import com.scottwong.service.EmployeeService;public class EmployeeTest {private static EmployeeService employeeService;@BeforeClasspublic static void setUpBeforeClass() throws Exception{try {ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");employeeService = (EmployeeService)ctx.getBean("employeeService");} catch (RuntimeException e) {e.printStackTrace();}}@Testpublic void save(){ //实例化spring容器 生成表结构//employeeService.save(new Employee("张三丰",1500,3));}@Testpublic void findById(){Employee e =this.employeeService.findById(10);System.out.println(e.getEmployeeName());}@Testpublic void findAll(){List<Employee> list =  this.employeeService.findAll();}@Testpublic void findByHQL(){List<Employee> list = this.employeeService.findByHQL(30, "张5");for (Employee e : list) {System.out.println(e.getEmployeeId()+"  "+e.getEmployeeName());}}@Testpublic void findObjectByName(){List<Employee> list = this.employeeService.findEmployeesByemployeeName("张2");for (Employee e : list) {System.out.println(e.getEmployeeId()+"  "+e.getEmployeeName());}}}

原创粉丝点击