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的小伙伴,如果看到我的博客,希望留下你的联系方式,我们一起加油!!!

    
    
    
  • 相关阅读:
    hdu2574 Hdu Girls' Day (分解质因数)
    python------logging模块
    python之异常
    python之反射
    python面向对象之封装
    python之面向对象2
    pyhton之路---面向对象
    python之路模块与包
    python常用模块
    匿名函数
  • 原文地址:https://www.cnblogs.com/superhonors/p/11646088.html
Copyright © 2011-2022 走看看