/images/avatar.jpg

添雨

记录生活、记录历史

使用Mockito模拟静态方法

1. 概述 在某项目单元测试编码过程中,当前覆盖率大约在50%左右。一直无法得到有效提升的原因之一便是无法针对HttpUtil.postByJson这类方法进行Mock。 经过代码调查,将此类问题抽象为如何Mock静态方法?本教程将针对这一问题进行说明——如何针对静态方法的模拟进行说明。 2. 一个简单的静态类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 /** * @author taliove * @author taliove 2019/4/10 09:42 新增针对不同的编码格式返回 */ public class HttpUtil { private HttpUtil() {} /** * 使用JSON格式发送POST请求 * * @param url 请求地址 * @param param 请求参数 * @return */ public static String postByJson(String url, Object param) throws IOException { return "ok"; } /** * 无参静态方法示例 * * @return */ public static String name() { return "test"; } } 为了方便于演示,此处封装了一个简单的返回"ok"的方法:postByJson。

记一次kafka问题排查记录

问题 生产环境下发现某个数据库中的某张表同步。 由于我们使用的是canal进行数据库的同步。 canal中针对kafka的主要配置如下: 1 2 canal.mq.partitionsNum=10 canal.mq.partitionHash=.*\\..* 该配置定义了根据库及表名,发送数据到指定的分区。最大分区数为10。 经过研究发现,未同步的表会将数据发送至分区5。至此发现主要的队列的分区5已不存在。 排查 使用命令查看kafka的分区信息: 1 ./bin/kafka-topics.sh --zookeeper localhost:2181 --describe --topic topic 分区信息如结果如下: 1 2 3 4 5 6 7 8 9 10 11 Topic:topic PartitionCount:10 ReplicationFactor:1 Configs: Topic: topic Partition: 0 Leader: 1001 Replicas: 1001 Isr: 1001 Topic: topic Partition: 1 Leader: 1003 Replicas: 1006,1003 Isr: 1003 Topic: topic Partition: 2 Leader: 1001 Replicas: 1001 Isr: 1001 Topic: topic Partition: 3 Leader: 1002 Replicas: 1006,1002 Isr: 1002 Topic: topic Partition: 4 Leader: 1001 Replicas: 1001 Isr: 1001 Topic: topic Partition: 5 Leader: -1 Replicas: 1006 Isr: 1006 Topic: topic Partition: 6 Leader: 1001 Replicas: 1001 Isr: 1001 Topic: topic Partition: 7 Leader: 1003 Replicas: 1006,1003 Isr: 1003 Topic: topic Partition: 8 Leader: 1001 Replicas: 1001 Isr: 1001 Topic: topic Partition: 9 Leader: 1002 Replicas: 1006,1002 Isr: 1002 5号分区的Leader未选举出来。原因是brokers为1006的机器,已禁用kafka服务导致。