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

    
    
    
  • 相关阅读:
    kubeadm High availability cluster(1.23)
    OpenSSH升级版本到最新(8.9)
    如何修复 Linux 中的“passwd:鉴定令牌操作错误”
    dd命令
    Docker 更新版本
    iftop命令命令详解
    云原生时代的DevOps之道
    yum获取rpm软件包的三种方法
    Kubernetes使用helm部署单机版mysql(使用hostPath数据卷)
    The connection to the server localhost:8080 was refused did you specify the right host or port?
  • 原文地址:https://www.cnblogs.com/superhonors/p/11646088.html
Copyright © 2020-2023  润新知