测试类的创建junit

在业务实现类上创建测试类,

1:鼠标右键新建
测试类的创建junit
2:选择junit单元测试
测试类的创建junit

测试类的创建junit

测试类的创建junit测试类的创建junit测试类的创建junit
创建后代码如下

import cn.itcast.mybatis.po.User;


public class UserDaoImplTest {
private SqlSessionFactory sqlSessionFactory;
// @Before注解标识 的方法在测试方法执行之前去执行
@Before
public void setUp() throws Exception {
// 创建sqlSessionFactory
// mybatis全局配置文件
String resource = "SqlMapConfig.xml";
// 根据mybatis的全局配置文件构造 一个流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
}
@Test
public void testFindUserById() throws Exception {
UserDao userDao = new UserDaoImpl(sqlSessionFactory);
//调用userDao的方法
User user = userDao.findUserById(1);
System.out.println(user);
}
}