java svfrclient.jar_工具篇-Java中一些utils

使用迭代器可以边访问集合边删除元素

Iterator iterator = all.iterator();//实例化迭代器

while(iterator.hasNext()){

String str=iterator.next();//读取当前集合数据元素

if("b".equals(str)){//all.remove(str);//使用集合当中的remove方法对当前迭代器当中的数据元素值进行删除操作(注:此操作将会破坏整个迭代器结构)使得迭代器在接下来将不会起作用

iterator.remove();

}else{

System.out.println( str+" ");

}

}

十三、单元测试

1. jacoco工具

最近用了jacoco工具,感觉挺好使的,单元测试当然也可以使用IDE自带的run with coverag。Jacoco有命令行和Pom配置两种方式,命令行方式就是在工程根目录下执行以下命令,如果多个模块可以到子模块下执行:

1 mvn clean test org.jacoco:jacoco-maven-plugin:0.7.4.201502262128:prepare-agent install -Dmaven.test.failure.ignore=true

然后在target文件下生成jacoco-ut文件夹,文件夹下可以看到index.html文件,用浏览器打开就是单元覆盖率报告。

另一种方式就是Pom配置文件的方式,注意dataFile和outputDirectory一定要写清楚,另外一定要有test/java这个目录

1

2 org.testng

3 testng

4 6.9.6

5 test

6 true

7

8

9 org.jacoco

10 jacoco-maven-plugin

11 0.7.4.201502262128

12 test

13

14

15 junit

16 junit

17 4.12

18 test

19

1

2 org.apache.maven.plugins

3 maven-shade-plugin

4 2.4.1

5

6

7

8 package

9

10 shade

11

12

13

14

15

16

17

18

19

20

21 org.apache.flink:*

22

23

24 org/apache/flink/shaded/com/**25 web-docs/**26 27 28 29 31 *:*32 33 META-INF/*.SF34 META-INF/*.DSA35 META-INF/*.RSA36 37 38 39 false40 41 42 43 44

45 46 org.apache.maven.plugins47 maven-compiler-plugin48 3.149 50 1.851 1.852 53 54 55 org.apache.maven.plugins56 maven-surefire-plugin57 2.1958 59 60 61 62 63 64

65

66

67

68

69

70

71 org.jacoco

72 jacoco-maven-plugin

73 0.7.4.201502262128

74

75

76 77 78 79 80 81 pre-test82 83 prepare-agent84 85 86 jacocoArgLine87 88 89 90 post-test91 test92 93 report94 95 96 97 post-unit-test98 test99 100 report101 102 103 /Users/admin/Desktop/test/target/jacoco.exec104 /Users/admin/Desktop/test/target/target/jacoco-u105 106 107 108

十四、Protobuf

1. Protobuf里的数组

Protobuf文件中使用repeated定义数组类型元素,Java设置的时候使用addEntitites而不是set

十五、Maven

1. -DskipTests和-Dmaven.test.skip=true跳过单元测试

mvn clean package -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。

mvn clean package -Dmaven.test.skip=true,不执行测试用例,也不编译测试用例类

2. 本地仓库导入官方仓库没有的jar包

用到Maven如下命令:

mvn install:install-file

-DgroupId=包名

-DartifactId=项目名

-Dversion=版本号

-Dpackaging=jar

-Dfile=jar文件所在路径

比如执行以下命令:

mvn install:install-file -Dfile=D:\lib\jnotify-0.94.jar -DgroupId=net.contentobjects -DartifactId=jnotify -Dversion=0.94 -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true

工程中使用时在pom.xml中添加如下内容:

net.contentobjects

jnotify

0.94

<jar

十六、日志打印

1. 异常打印

/**

* String getMessage() :返回此 throwable 的详细消息字符串。

* String toString() : 返回此 throwable 的简短描述。

* void printStackTrace():将此 throwable 及其追踪输出至标准错误流。 (即 调用此方法会把完整的异常信息打印到控制台)

* @author 郑清

*/

public class Demo {

public static void main(String[] args) {

test(6,0);

}

public static void test(int a,int b){

try{

System.out.println(a/b);

}catch(Exception e){

//catch里面是出现异常的处理方式 下面err是以红色打印信息

System.err.println(e.getMessage());//打印异常原因 ==》 一般给用户看

System.err.println(e.toString());//打印异常名称以及异常原因 ==》 很少使用

e.printStackTrace();//打印异常原因+异常名称+出现异常的位置 ==》 给程序员debug的时候看

}

System.out.println("===try-catch结束===");

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值