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.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sy.model.User;
import sy.service.UserServiceI;
//相当于junit猪似的部分
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml", "classpath:spring-mybatis.xml" })
public class testMabatis {
/*private ApplicationContext ac;*/
private UserServiceI userService;
/* @Before
public void before(){
ac=new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});
userService=(UserServiceI)ac.getBean("userService");
}*/
public UserServiceI getUserService() {
return userService;
public void setUserService(UserServiceI userService) {
this.userService = userService;
}
@Test
public void test1(){
User u=userService.getUserById("23");
System.out.println(u.getName());
}
}
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.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sy.model.User;
import sy.service.UserServiceI;
//相当于junit猪似的部分
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml", "classpath:spring-mybatis.xml" })
public class testMabatis {
/*private ApplicationContext ac;*/
private UserServiceI userService;
/* @Before
public void before(){
ac=new ClassPathXmlApplicationContext(new String[]{"spring.xml","spring-mybatis.xml"});
userService=(UserServiceI)ac.getBean("userService");
}*/
public UserServiceI getUserService() {
return userService;
}
//将service自动注入到spring的上下文。
@Autowiredpublic void setUserService(UserServiceI userService) {
this.userService = userService;
}
@Test
public void test1(){
User u=userService.getUserById("23");
System.out.println(u.getName());
}
}