Junit测试 - Spring的配置
第一种:
@ContextConfiguration(locations = {"classpath*:/spring-core.xml"}) public class UserMapperTest extends AbstractJUnit4SpringContextTests { @Resource private UserMapper userMapper; @Test public void testQueryAllUser () { User user = userMapper.queryById("1"); System.out.println(user.getName()); } }
第二种:
public class UserMapperTest2 { private UserMapper userMapper; @Before public void init(){ ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath*:/spring/applicationContext.xml"); userMapper = (UserMapper)ctx.getBean("userMapper"); } public UserMapper getUserMapper() { return userMapper; } public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } }
UserMapper需要加注解:
/** * 用户信息数据持久化操作接口 */ @Repository public interface UserMapper extends BaseMapper<User>
IT技术和行业交流群 417691667