junit单元测试:@Before、@After

@Before:修饰的方法会在测试方法之前被自动执行

@After:修饰的方法会在测试方法执行之后自动被执行

案例1:

//预处理执行对象
public class JdbcDemo2 {
  Connection connection=null;
  PreparedStatement statement =null;
  ResultSet resultSet =null;
  //初始化加载
  @Before
  public  void init() throws Exception {
    String driver="com.mysql.jdbc.Driver";//驱动
    String url="jdbc:mysql://localhost:3306/heima";//数据库地址
    String username="root";//用户名
    String password="123";//密码
    //2.注册驱动
    Class.forName(driver);
    //3.获取数据库连接对象
    connection = DriverManager.getConnection(url, username, password);
 }
  //释放资源,先出现的后关闭,后出现的先关闭原则
  @After
  public  void destory() throws SQLException {
    if(resultSet!=null){
      resultSet.close();
   }
    if(statement!=null){
      statement.close();
   }
    if(connection!=null){
      connection.close();
   }
 }
  //查询
  @Test
  public  void query() throws SQLException {
    //1.编写sql语句
    String sql="select * from items";
    //2.执行对象
    statement=connection.prepareStatement(sql);
    //3.获取结果集
   resultSet = statement.executeQuery();
   //处理结果集
    while(resultSet.next()){
      String string = resultSet.getString("name");
      System.out.println(string);
   }
 }
  //条件查询
  @Test
  public  void queryForParam() throws SQLException {
    //1.编写sql语句
    String sql="select * from items where name like ? and price>?";
    //2.执行对象
    statement=connection.prepareStatement(sql);
   /* 3.为占位符赋值
        * SQL语句中?表示一个占位符
        * setxxx 两个参数,参数1表示占位符的索引,从1开始,如果当前sql语句中有一个?那么就是
1
        * 参数2表示为相应的占位符所赋得值
      */
    statement.setString(1,"%黑%");
    statement.setString(2,"100");
    //4.获取结果集
    resultSet = statement.executeQuery();
    //处理结果集
    while(resultSet.next()){
      String string = resultSet.getString("name");
      System.out.println(string);
   }
 }
  //DML语句
  @Test
  public void update() throws SQLException {
    //1.编写sql语句
    String sql="update items set price=?,name=? where id=?";
    //2.执行对象
    statement=connection.prepareStatement(sql);
    /* 3.为占位符赋值
    * SQL语句中?表示一个占位符
    * setxxx 两个参数,参数1表示占位符的索引,从1开始,如果当前sql语句中有一个?那么就是1
    * 参数2表示为相应的占位符所赋得值
    */
    statement.setString(1,"100");
    statement.setString(2,"张三");
    statement.setString(3,"7");
    //4.执行sql语句
    int i = statement.executeUpdate();
    System.out.println(i);
 }
}

案例2:

public class HibernateTest {
    Transaction tx = null;
    Session session = null;
    @Before
    public void init() {
        //初始化注册服务对象
        //默认加载hibernate.cfg .xmL配置文件,如果配置文件名称被修改,configure("修改的名字")
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
        //从元信息获取session工厂
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        //从工厂创建session连接
        session = sessionFactory.openSession();
        //开启事务
        tx = session.beginTransaction();
    }
    @After
    public void destroy() {
        //提交事务
        tx.commit();
        //关闭
        session.close();
    }
    @Test
    public void test1() {
        //创建实例
        User user = new User();
        user.setName("zhangsan");
        user.setPasswd("123");
        session.save(user);
    }

}
原文地址:https://www.cnblogs.com/zwh0910/p/15686423.html