zoukankan      html  css  js  c++  java
  • 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
    ",
            log.getLog());
      }
    
    }

    观察代码可知,有两处使用断言:编写代码的时候我们总会做出一些假设,比如假设3+5的值为10,然后取验证,发现不等于10,而等于8,而我们用代码捕捉我们的假设称为断言

      第一处断言:断言cd这个自动化装配的bean已经装配进来了

      第二处断言:断言控制台输出的结果与 "Playing Sgt. Pepper's Lonely Hearts Club Band by The Beatles " 相同

      两种断言的表达式均为Boolean值

    还有一个不太常见的StandardOutputStreamLog 对象,此对象是基于控制台的输出去做断言,不给过该方法已经不推荐使用,替代的是  org.junit.contrib.java.lang.system.SystemOutRule

      同样在自学Spring的小伙伴,如果看到我的博客,希望留下你的联系方式,我们一起加油!!!

    
    
    
  • 相关阅读:
    git 删除所有提交下的某个文件
    Sublime Text 中文
    git 常用文件目录介绍
    设计模式之——单例模式
    Mysql系统知识梳理
    Spring系列之——使用了哪些设计模式
    JAVA基础之——三大特征、接口和抽象类区别、重载和重写区别、==和equals区别、JAVA自动装箱和拆箱
    集合系列问题
    做一个优秀的职场人才
    Spring系列之——Spring事务以及两大核心IOC和AOP
  • 原文地址:https://www.cnblogs.com/superhonors/p/11646088.html
Copyright © 2011-2022 走看看