spring整合junit问题分析
spring整合Junit分析
1、应用程序的入口
main方法
2、junit单元测试中,没有main方法也能执行
junit集成了一个main方法
该方法就会判断当前测试类中哪些方法有 @Test注解
junit就让有Test注解的方法执行
3、junit不会管我们是否采用spring框架
在执行测试方法时,junit根本不知道我们是不是使用了spring框架
所以也就不会为我们读取配置文件/配置类创建spring核心容器
4、由以上三点可知
当测试方法执行时,没有Ioc容器,就算写了Autowired注解,也无法实现注入
<?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.learn</groupId><artifactId>day02_learn_04account_annoioc_withoutxml</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><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.45</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>
package com.learn.domain;import java.io.Serializable;/*** 账户的实体类*/
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;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + '\'' +", money=" + money +'}';}
}
package com.learn.service;import com.learn.domain.Account;import java.util.List;/*** 账户的业务层接口*/
public interface IAccountService {/*** 查询所有* @return*/List<Account> findAllAccount();/*** 查询一个* @return*/Account findAccountById(Integer accountId);/*** 保存* @param account*/void saveAccount(Account account);/*** 更新* @param account*/void updateAccount(Account account);/*** 删除* @param acccountId*/void deleteAccount(Integer acccountId);}
package com.learn.service.impl;import com.learn.dao.IAccountDao;
import com.learn.domain.Account;
import com.learn.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;/*** 账户的业务层实现类*/
@Service("accountService")
public class AccountServiceImpl implements IAccountService{@Autowiredprivate IAccountDao accountDao;public List<Account> findAllAccount() {return accountDao.findAllAccount();}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 acccountId) {accountDao.deleteAccount(acccountId);}
}
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123456
package config;import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;import javax.sql.DataSource;/*** 和spring连接数据库相关的配置类*/
public class JdbcConfig {@Value("${jdbc.driver}")private String driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;/*** 用于创建一个QueryRunner对象* @param dataSource* @return*/@Bean(name="runner")@Scope("prototype")public QueryRunner createQueryRunner(@Qualifier("ds2") DataSource dataSource){return new QueryRunner(dataSource);}/*** 创建数据源对象* @return*/@Bean(name="ds2")public DataSource createDataSource(){try {ComboPooledDataSource ds = new ComboPooledDataSource();ds.setDriverClass(driver);ds.setJdbcUrl(url);ds.setUser(username);ds.setPassword(password);return ds;}catch (Exception e){throw new RuntimeException(e);}}@Bean(name="ds1")public DataSource createDataSource1(){try {ComboPooledDataSource ds = new ComboPooledDataSource();ds.setDriverClass(driver);ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");ds.setUser(username);ds.setPassword(password);return ds;}catch (Exception e){throw new RuntimeException(e);}}
}
package com.learn.test;import com.learn.domain.Account;
import com.learn.service.IAccountService;
import config.SpringConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.List;/*** 使用Junit单元测试:测试我们的配置* Spring整合junit的配置* 1、导入spring整合junit的jar(坐标)* 2、使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的* @Runwith* 3、告知spring的运行器,spring和ioc创建是基于xml还是注解的,并且说明位置* @ContextConfiguration* locations:指定xml文件的位置,加上classpath关键字,表示在类路径下* classes:指定注解类所在地位置** 当我们使用spring 5.x版本的时候,要求junit的jar必须是4.12及以上*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfiguration.class)
public class AccountServiceTest {@Autowiredprivate IAccountService as = null;@Testpublic void testFindAll() {//3.执行方法List<Account> accounts = as.findAllAccount();for(Account account : accounts){System.out.println(account);}}@Testpublic void testFindOne() {//3.执行方法Account account = as.findAccountById(1);System.out.println(account);}@Testpublic void testSave() {Account account = new Account();account.setName("test anno");account.setMoney(12345f);//3.执行方法as.saveAccount(account);}@Testpublic void testUpdate() {//3.执行方法Account account = as.findAccountById(4);account.setMoney(23456f);as.updateAccount(account);}@Testpublic void testDelete() {//3.执行方法as.deleteAccount(4);}
}
package com.learn.test;import config.SpringConfiguration;
import org.apache.commons.dbutils.QueryRunner;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;/*** 测试queryrunner是否单例*/
public class QueryRunnerTest {@Testpublic void testQueryRunner(){//1.获取容易ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfiguration.class);//2.获取queryRunner对象QueryRunner runner = ac.getBean("runner",QueryRunner.class);QueryRunner runner1 = ac.getBean("runner",QueryRunner.class);System.out.println(runner == runner1);}
}
spring整合junit问题分析相关推荐
- Spring的新注解——Configuration、ComponentScan、Bean、Import、PropertySource || spring整合Junit分析
spring中的新注解 spring整合Junit分析 1.应用程序的入口 main方法 2.junit单元测试中,没有main方法也能执行 junit集成了一个main方法 ...
- Spring 整合 Junit
Spring 整合 Junit 问题 在测试类中,每个测试方法都有以下两行代码: ApplicationContext ac = new ClassPathXmlApplicationContext( ...
- java day58【 案例:使用 spring 的 IoC 的实现账户的 CRUD 、 基于注解的 IOC 配置 、 Spring 整合 Junit[掌握] 】...
第1章 案例:使用 spring 的 IoC 的实现账户的 CRUD 1.1 需求和技术要求 1.1.1 需求 1.1.2 技术要求 1.2 环境搭建 1.2.1 拷贝 jar 包 1.2.2 创建数 ...
- 如何使用 Spring 整合 junit 单元测试
文章目录 1.测试类中的问题和解决思路 1.1.问题 1.2.解决思路分析 2.配置步骤 2.1.第一步:拷贝整合 junit 的必备 jar 包到 lib 目录 2.2.第二步:使用@RunWith ...
- Spring4.x(8)---Spring整合Junit
Spring整合Junit 在开发基于Spring框架的项目时,发现通过Spring进行对象管理之后,做测试变得复杂了.因为所有的Bean都需要在applicationContext.xml中加载好, ...
- Spring 整合junit
junit需要是4.12版本以上 <dependency><groupId>org.springframework</groupId><artifactId& ...
- spring入门第二讲 bean的生命周期 装配方式 Spring整合Junit
bean的生命周期 实体类 //初始化 public void init(){System.out.println("--初始化--"); }//销毁 public void de ...
- junit 引入spring 注解管理_第05章 Spring 整合 Junit
3.1 测试类中的问题和解决思路 3.1.1 问题 在测试类中,每个测试方法都有以下两行代码: ApplicationContext 这两行代码的作用是获取容器,如果不写的话,直接会提示空指针异常.所 ...
- java元婴期(21)----java进阶(spring(5)---事务管理AOP事务管理(全自动)spring整合Junit)
事务管理 事务:一组业务操作ABCD,要么全部成功,要么全部不成功. 特性:ACID 原子性:整体 一致性:完成 隔离性:并发 持久性:结果 隔离问题: 脏读:一个事务读到另一个事务没有提交的数据 不 ...
最新文章
- Open-E DSS V7 应用系列之三 Web管理简介
- MindSpore!这款刚刚开源的深度学习框架我爱了!
- CentOS 7.2.1511 x64下载地址
- 解决ie8及低版本浏览器不支持html5标签属性
- 微博关注者数量在计算中的作用
- IAR下μCosIII移植心得
- 同一目录下拷贝文件夹里_protobuf在C++下的安装使用
- 用于最优控制的简单软件
- ldr和adr的区别
- L1-066 猫是液体 (5 分)-PAT 团体程序设计天梯赛 GPLT
- windows如何安装MySql(包含一些安装时问题的解决)
- 《技术人创业攻略》-用技术改变世界!
- Crontab 每隔整点1小时2小时执行一次任务
- 基于yolov5的目标检测和单目测距
- 【游戏渲染】【译】Unity3D Shader 新手教程(1/6)
- Linux命令分隔符
- MySQL5.7官方下载链接导航
- 氢os关闭android键盘,氢OS11到来前,先听听这些一加用户对氢OS的吐槽
- app卡在启动页面android,uni-app运行时卡在启动界面
- 字符编码问题三个不可见的字符(0xEF-0xBB-0xBF,即BOM)