想用junit测试数据层的实现效果,但是发现,无法利用自动装配取到mapper接口的对象,解决方法如下
背景:首先要保证mapper(dao)已经注入到容器中去
比如:
两种方式:
1.自动装配
一定要在测试类上加上两行注解才能够实现自动装配
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
2.利用ApplicationContext
新建一个容器,从这个容器中使用getBean()方法取出mapper接口对象
public class MyTest2 {
@Test
public void t1() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
TicketMapper ticketMapper = (TicketMapper) context.getBean("ticketMapper");
List list= ticketMapper.userList(11);
System.out.println("======================");
System.out.println(list);
}
}



