• 【源码】hamcrest 源码阅读 定制 Matcher


    前言

    实现 LowestPriceMatcher

    判断价格是否为最低价。lowestPrice 是变化的,每次都需要实例化一个 LowestPriceMatcher

    public class LowestPriceMatcher extends TypeSafeMatcher<BigDecimal> {
    
        private final BigDecimal lowestPrice;
    
        public LowestPriceMatcher(BigDecimal lowestPrice) {
            this.lowestPrice = lowestPrice;
        }
    
        @Override
        public void describeTo(Description description) {
            description.appendText("使用最低价格购入, 花费").appendValue(lowestPrice).appendText("元");
        }
    
        @Override
        protected boolean matchesSafely(BigDecimal item) {
            return lowestPrice.equals(item);
        }
    
        @Override
        protected void describeMismatchSafely(BigDecimal item, Description mismatchDescription) {
            mismatchDescription.appendText("当前产品的价格: ").appendValue(item).appendText("元")
                    .appendText(" 不是最低价格。最低价格为:").appendValue(lowestPrice);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 使用
        @Test
        public void test() {
            LowestPriceMatcher matcher = new LowestPriceMatcher(BigDecimal.ZERO);
    		// 使用 MatcherAssert.assertThat;
            assertThat(BigDecimal.TEN, matcher);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 效果
      在这里插入图片描述

    结合 allOf

    allOf 包含的 Matcher必须全部满足。以下代码执行结果与上文相同。

        @Test
        public void test() {
            LowestPriceMatcher equalLowerPrice = new LowestPriceMatcher(BigDecimal.ZERO);
            BigDecimalCloseTo oneCentOverLowestPrice = new BigDecimalCloseTo(BigDecimal.ZERO, new BigDecimal("0.01"));
    
            // 使用 org.hamcrest.core.AllOf.allOf;
            assertThat(new BigDecimal("0.01"), allOf(equalLowerPrice, oneCentOverLowestPrice));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    结合 anyOf

    anyOf 包含的 Matcher,只要满足一个条件即可。

        @Test
        public void test() {
            LowestPriceMatcher equalLowerPrice = new LowestPriceMatcher(BigDecimal.ZERO);
            BigDecimalCloseTo oneCentOverLowestPrice = new BigDecimalCloseTo(BigDecimal.ZERO, new BigDecimal("0.01"));
    
            // 使用 org.hamcrest.core.AllOf.anyOf;
            assertThat(new BigDecimal("0.01"), anyOf(equalLowerPrice, oneCentOverLowestPrice));
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 结果
      测试通过
      在这里插入图片描述

    后记

    拓展一个实现,能够独立使用,也能加入到已有的体系里面组合使用。这种设计很合理,希望以后复杂的业务场景能够把这种思想用起来。

  • 相关阅读:
    自媒体MCN公司选择企业云盘:哪个更适合?
    Redis 技术整理
    告别繁琐流程,让你轻松成为电子画册制作达人!
    关于我用xhtmlrenderer将html转换img结果样式飞了的这档事
    SSM+医保业财一体化管理系统 毕业设计-附源码151023
    信息学奥赛一本通:1163:阿克曼(Ackmann)函数
    Python基础-面向对象编程之特性(property)
    自用工具类整理
    桥接模式-C++实现
    防火墙---双机热备技术
  • 原文地址:https://blog.csdn.net/chenghan_yang/article/details/133637569