springtest+juint开发测试如下:
项目结构目录如下:
UserMapper.java 为接口文件。User 为实体类。UserMapper.xml 为对应mybatis的xml文件。test为对应的测试包
applicationtest.xml文件为对于的spring配置文件的。db.propertise 为数据库配置。log4j是对应的日志文件
UserDao为操作数据库的类
public class UserDao extends SqlSessionDaoSupport implements UserMapper {
@Autowired(required=true)
public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
super.setSqlSessionFactory(sqlSessionFactory);
}
public User selectByPrimaryKey(Integer userid) {
UserMapper sqlSession = (UserMapper) getSqlSession().getMapper(UserMapper.class);
return sqlSession.selectByPrimaryKey(1);
}
}
UserDaoTest类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationtest.xml")
public class UserDaoTest extends AbstractJUnit4SpringContextTests {
public UserDao userDao;
private final Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void test1(){
User user = userDao.selectByPrimaryKey(1);
System.out.println(user);
logger.info("111111111111111111");
}
public UserDao getUserDao() {
return userDao;
}
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
}