spring整合junit进行测试

时间:2024-07-14 20:03:20

以下只是一个模板,大家记得改变配置文件

package cn.itcast.crm.dao;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itcast.crm.entity.Customer; @ContextConfiguration(locations= {"classpath:applicationContext_dao.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class CustomerDaoImplTest { /*
* @Test
* public void test() { ApplicationContext ac=new
* ClassPathXmlApplicationContext("classpath:applicationContext_dao.xml");
* CustomerDao customerDao = (CustomerDao) ac.getBean("customerDao"); Customer
* customer = customerDao.findCustomerById(23L); System.out.println(customer); }
*/
@Autowired
private CustomerDao customerDao; @Test
public void test() {
Customer customer = customerDao.findCustomerById(23L);
System.out.println(customer);
} }