Spring入门(四)Spring-test模块
自动化转配bean的测试案例分析
package soundsystem;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.StandardOutputStreamLog;
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; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=CDPlayerConfig.class)
public class CDPlayerTest { @Rule
public final StandardOutputStreamLog log = new StandardOutputStreamLog(); @Autowired
private MediaPlayer player; @Autowired
private CompactDisc cd; @Test
public void cdShouldNotBeNull() {
assertNotNull(cd);
} @Test
public void play() {
player.play();
assertEquals(
"Playing Sgt. Pepper's Lonely Hearts Club Band" +
" by The Beatles\n",
log.getLog());
} }
观察代码可知,有两处使用断言:编写代码的时候我们总会做出一些假设,比如假设3+5的值为10,然后取验证,发现不等于10,而等于8,而我们用代码捕捉我们的假设称为断言
第一处断言:断言cd这个自动化装配的bean已经装配进来了
第二处断言:断言控制台输出的结果与 "Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles\n" 相同
两种断言的表达式均为Boolean值
还有一个不太常见的StandardOutputStreamLog 对象,此对象是基于控制台的输出去做断言,不给过该方法已经不推荐使用,替代的是 org.junit.contrib.java.lang.system.SystemOutRule
同样在自学Spring的小伙伴,如果看到我的博客,希望留下你的联系方式,我们一起加油!!!
Spring入门(四)Spring-test模块的更多相关文章
- spring入门(四) spring mvc返回json结果
前提:已搭建好环境 1.建立Controller package com.ice.controller; import com.ice.model.Person; import org.springf ...
- Spring入门(四):使用Maven管理Spring项目
让我们先回顾下本系列的前3篇博客: Spring入门(一):创建Spring项目 Spring入门(二):自动化装配bean Spring入门(三):通过JavaConfig装配bean 1.为什么要 ...
- Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作
开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...
- Spring重温(四)--Spring自动组件扫描
通常情况下,声明所有的Bean类或组件的XML bean配置文件,这样Spring容器可以检测并注册Bean类或组件. 其实,Spring是能够自动扫描,检测和预定义的项目包并实例化bean,不再有繁 ...
- Spring学习(四)-----Spring Bean引用同xml和不同xml bean的例子
在Spring,bean可以“访问”对方通过bean配置文件指定相同或不同的引用. 1. Bean在不同的XML文件 如果是在不同XML文件中的bean,可以用一个“ref”标签,“bean”属性引用 ...
- spring入门(六) spring mvc+mybatis
1.引入依赖 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> < ...
- spring入门(五) spring mvc+hibernate
核心是让SessionFactory由Spring管理 1.引入依赖 <!-- https://mvnrepository.com/artifact/org.springframework/sp ...
- spring入门(八) spring mvc设置默认首页
1.web.xml配置如下 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...
- spring入门(七) spring mvc+mybatis+generator
1.Mybatis-Generator下载 地址:https://github.com/mybatis/generator/releases 我使用的是 mybatis-generator-core- ...
- Spring Boot入门(四):开发Web Api接口常用注解总结
本系列博客记录自己学习Spring Boot的历程,如帮助到你,不胜荣幸,如有错误,欢迎指正! 在程序员的日常工作中,Web开发应该是占比很重的一部分,至少我工作以来,开发的系统基本都是Web端访问的 ...
随机推荐
- 常用Javascript方法
一,检测是否是Array 1,通过constructor检测 function isArray(value){ return value && typeof value === 'ob ...
- python-模块 time, os, sys
时间模块 和时间有关系的我们就要用到时间模块.在使用模块之前,应该首先导入这个模块. #常用方法 1.time.sleep(secs) (线程)推迟指定的时间运行.单位为秒. 2.time.time( ...
- esxi主机用命令行强行关闭通过前端界面无法关闭的ESXI虚拟机
环境:esxi5.1-esxi6.5 背景:如果esxi下面某一台vm死机了,并且esxi的控制台卡死不能用,为了不影响同一个esx下其他的vm正常使用,那么我们只能用命令行来单独重启此vm,保证一定 ...
- nuxt项目在windows环境下安装部署
1.nodejs安装,地址 https://nodejs.org/en/ 2.在本地项目中运行npm run build 命令将开发好的项目打包生成.nuxt文件夹,然后把.nuxt文件夹.nux ...
- js函数方法
/** * call()和apply * 这两个方法都是函数对象的方法,需要通过函数对象来调用 * 当对函数调用call()和apply()都会调用函数执行 * 在调用call()和apply()可以 ...
- RK3288编译 Android 5.1 固件
1 准备工作 编译 Android 对机器的配置要求较高: 64 位 CPU 16GB 物理内存+交换内存 30GB 空闲的磁盘空间用于构建,源码树另外占用大约 25GB Ubuntu 14.04 操 ...
- struct解决socket黏包问题 (指令传输)
服务端代码如下 import struct import subprocess import socket server = socket.socket() server.bind(()) serve ...
- 【leetcode】bash脚本练习
[192]Word Frequency Write a bash script to calculate the frequency of each word in a text file words ...
- mongodb的学习 (1)
1. 启动服务 (1)在D盘下新建mongodb 文件夹,在mongodb 文件夹里面新建db; (2)win+R 启动cmd ,输入 `mongod --dbpath "D:/mongod ...
- elasticsearch学习笔记001
<Elasticsearch 核心技术与实战>课程Github代码 https://github.com/onebirdrocks/geektime-ELK 运行的环境: windows ...