欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring整合junit

程序员文章站 2022-06-13 20:24:38
...

IDEA中创建maven项目
数据库表
Spring整合junit

整体构架

Spring整合junit

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.uek</groupId>
    <artifactId>spring02-study03-ioc-account-annotation-junit</artifactId>
    <version>1.0-SNAPSHOT</version>


    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <!--spring整合junit的jar-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

</project>

entity


/**
 * 账户的实体类
 */
public class Account implements Serializable {

    private Integer id;
    private String name;
    private Float money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Float getMoney() {
        return money;
    }

    public void setMoney(Float money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

dao层

/**
 * 账户的持久层接口
 */
public interface IAccountDao {

    //查询所有
    List<Account> findAllAccount() throws SQLException;

    //根据id查询
    Account findAccountById(Integer accountId);

    //插入
    void saveAccount(Account account);

    //更新
    void updateAccount(Account account);

    //删除
    void deleteAccount(Integer accountId);

}

/**
 * 账户的持久层实现类
 */
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {

    @Autowired
    private QueryRunner runner;

    //查询所有
    public List<Account> findAllAccount() {
        try {
            return runner.query("select * from account",new BeanListHandler<Account>(Account.class));
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    //根据id查询
    public Account findAccountById(Integer accountId) {
        try {
            return runner.query("select * from account where id = ? ",new BeanHandler<Account>(Account.class),accountId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //插入
    public void saveAccount(Account account) {
        try {
            runner.update("insert into account(name,money)values(?,?)",account.getName(),account.getMoney());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //更新
    public void updateAccount(Account account) {
        try {
            runner.update("update account set name=?,money=? where id = ?",account.getName(),account.getMoney(),account.getId());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    //删除
    public void deleteAccount(Integer accountId) {
        try {
            runner.update("delete from account where id =?",accountId);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}

service


/**
 * 账户的业务层接口
 */
public interface IAccountService {

    //查询所有
    List<Account> findAllAccount() throws SQLException;

    //根据id查询
    Account findAccountById(Integer accountId);

    //插入
    void saveAccount(Account account);

    //更新
    void updateAccount(Account account);

    //删除
    void deleteAccount(Integer accountId);


}


/**
 * 账户的业务层实现类
 */
@Service("accountService")
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private IAccountDao accountDao;


    //查询所有
    public List<Account> findAllAccount() throws SQLException {
        return accountDao.findAllAccount();
    }

    //根据id查询
    public Account findAccountById(Integer accountId) {
        return accountDao.findAccountById(accountId);
    }

    //插入
    public void saveAccount(Account account) {
        accountDao.saveAccount(account);
    }

    //更新
    public void updateAccount(Account account) {
        accountDao.updateAccount(account);
    }

    //删除
    public void deleteAccount(Integer accountId) {
        accountDao.deleteAccount(accountId);
    }
}

jdbcConfig.properties

jdbc.Driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false
jdbc.username=root
jdbc.password=root

测试类

/**
 * 使用Junit单元测试
 * Spring整合junit的配置
 *    1、导入spring整合junit的jar
 *    2、使用junit提供的一个注解把原有的main方法替换了,替换成spring提供的
 *       @Runwith
 *    3、告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且说明位置
 *       @ContextConfiguration
 *       locations:指定xml文件的位置,加上classpath关键字,表示在类路径下
 *       classes:指定注解所在的位置
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {

    @Autowired
    private IAccountService ias;

    @Test
    public void testFindAll() throws SQLException {
        //3.执行方法
        List<Account> accounts = ias.findAllAccount();
        for (Account account : accounts) {
            System.out.println(account);
        }

    }

    @Test
    public void testFindOne(){
        //3.执行方法
        Account account = ias.findAccountById(2);
        System.out.println(account);
    }

    @Test
    public void testSave(){
        Account account = new Account();
        account.setName("张三");
        account.setMoney(2000f);
        //3.执行方法
        ias.saveAccount(account);

    }

    @Test
    public void testUpdate(){

        //3.执行方法
        Account account = ias.findAccountById(4);
        account.setName("123");
        account.setMoney(1234f);
        ias.updateAccount(account);
    }

    @Test
    public void testDelete(){

        //3.执行方法
        ias.deleteAccount(4);

    }

}

相关标签: Spring spring