三瓢水 2021-11-15 10:53 采纳率: 25%
浏览 28

javaEE 单元测试报错!求慧眼!

折磨了我一天一夜,人彻底傻了!
这是单元测试报错的

img

代码如下

这是配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 1 配置数据源--> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 数据库驱动--> 
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        
        <!-- 连接数据库的url--> 
        <property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
        
        <!-- 连接数据库的用户名--> 
        <property name="username" value="root"></property>
        
        <!-- 连接数据库的密码--> 
        <property name="password" value="123456"></property>
    </bean>
    
    <!-- 2 配置JDBC模板-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 默认必须使用数据源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    
    <!-- 定义id为accountDao的Bean -->
    <bean id="accountDao" class="com.itheima.jdbc.AccountDaoImpl">
        <!-- 将jdbcTemplate注入到accountDao实例中 -->
        <property name="jdbcTemplate" ref="jdbcTemplate" />
    </bean>   
    
</beans>

这是Account.java


```java

package com.itheima.jdbc;

public class Account {
    private Integer id;           //账户id
    private String username;      //用户名
    private Double balance;       //账户余额
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Double getBalance() {
        return balance;
    }
    public void setBalance(Double balance) {
        this.balance = balance;
    }
    
    public String toString() {
        return "Account [id=" + id + ", "
                + "username=" + username +
                ", balance=" + balance + "]";
        
    }
    
    
}



这是AccountDao.java
package com.itheima.jdbc;

import java.util.List;

public interface AccountDao {
    
    //添加
    public int addAccount(Account account);
    
    //更新
    public int updateAccount(Account account);
    
    //删除
    public int deleteAccount(int id);
    
    //通过id查询
    public Account findAccountById(int id);
    
    //查询所有账户
    public List<Account> findAllAccount();
    

}


这是AccountDaoImpl.java




```java

package com.itheima.jdbc;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
public class AccountDaoImpl implements AccountDao {
    
    //声明jdbcTemplate属性及其setter方法
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    // 添加账户
        public int addAccount(Account account) {
            // 定义SQL
            String sql = "insert into account(username,balance) values(?,?)";
            // 定义数组来存放SQL语句中的参数
            Object[] obj = new Object[] { 
                               account.getUsername(), 
                               account.getBalance() 
             };
            // 执行添加操作,返回的是受SQL语句影响的记录条数
            int num = this.jdbcTemplate.update(sql, obj);
            return num;
        }
        
        // 更新账户
        public int updateAccount(Account account) {
            // 定义SQL
            String sql = "update account set username=?,balance=? where id = ?";
            // 定义数组来存放SQL语句中的参数
            Object[] params = new Object[] { 
                                   account.getUsername(), 
                                   account.getBalance(), 
                                   account.getId() 
              };
            // 执行添加操作,返回的是受SQL语句影响的记录条数
            int num = this.jdbcTemplate.update(sql, params);
            return num;
        }
        
    //删除账户
    public int deleteAccount(int id) {
        // 定义SQL
        String sql="delete  from account where id=  ? ";
        //执行删除操作,返回的是受SQL语句影响的记录条数
        int num=this.jdbcTemplate.update(sql, id);
        return num;
    }
    
    //通过id查询账户信息
    public Account findAccountById(int id) {
        //定义sql语句
        String sql="select * from account where id =?";
        //创建一个新的BeanPropertyRowMapper对象
        RowMapper<Account> rowMapper=
                new BeanPropertyRowMapper<Account>(Account.class);
        //将id绑到SQL语句中,并通过RowMapper返回一个Object类型的单行记录
        return this.jdbcTemplate.queryForObject(sql, rowMapper, id);
    }
    //查询所有账户信息
    public List<Account> findAllAccount(){
        //定义SQL语句
        String sql="select * from account";
        //创建一个新的BeanPropertyRowMapper对象
        RowMapper<Account> rowMapper=
                new BeanPropertyRowMapper<Account>(Account.class);
        //执行静态的SQL查询,并通过RowMapper返回结果
        return this.jdbcTemplate.query(sql, rowMapper);
        
    }

}

接下来的是三个一运行就报错的单元测试!

@Test
        public void addAccountTest() {
        
        //加载配置文件
        ApplicationContext applicationContext=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //获取AccountDao实例
         AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao");
        
        //创建Account对象,并向Account对象中添加数据
         Account account=new Account();
         account.setUsername("tom");
         account.setBalance(1000.00);
         
         //执行addAccount()方法,并获取返回结果
         int num=accountDao.addAccount(account);
         if(num>0) {
             System.out.println("成功插入了"+ num + "条数据!");
         }else {
            System.out.println("插入操作执行失败!");
        }
        }
        
        
        @Test
        public void updateAccountTest() {
        
        //加载配置文件
        ApplicationContext applicationContext=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //获取AccountDao实例
         AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao");
        
        //创建Account对象,并向Account对象中添加数据
         Account account=new Account();
         account.setId(1);
         account.setUsername("tom");
         account.setBalance(2000.00);
         
         //执行updateAccount()方法,并获取返回结果
         int num=accountDao.addAccount(account);
         if(num>0) {
             System.out.println("成功修改了"+num + "条数据!");
         }else {
            System.out.println("修改操作执行失败!");
        }
        }
        

 @Test
        public void findAccountByIdTest() {
        
        //加载配置文件
        ApplicationContext applicationContext=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //获取AccountDao实例
         AccountDao accountDao=(AccountDao) applicationContext.getBean("accountDao");
        
         
         //执行findAccountById()方法,并获取返回结果
         Account account = accountDao.findAccountById(1);
         System.out.println(account);
        }
        
  • 写回答

2条回答 默认 最新

  • BCS-点心 2021-11-15 11:07
    关注

    发报错信息

    评论

报告相同问题?

问题事件

  • 创建了问题 11月15日

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 易优eyoucms关于二级栏目调用的问题
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题