Java自动化测试(Maven 8)

Java自动化测试(Maven 8)

Maven作用

  • Maven主要作用是使用它来导入第三方jar包

  • 管理项目

下载Maven

下载地址:http://maven.apache.org/download.cgi

image-20200718084505519

  • Binary:编译之后的二进制文件;

  • Source:表示可以查看源代码的,比Binary大一点;

  • tar.gz archive:Linux、macOS系统使用;

  • zip archive:windows系统使用;

修改配置

打开./apache-maven-3.6.3/conf/settings.xml

修改mirrors

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
-->
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>https://maven.aliyun.com/repository/central</url>
</mirror>
</mirrors>

修改本地存储路径localRepository

1
<localRepository>/Users/zhongxin/maven_download</localRepository>

设置环境变量

将解压后的文件夹移动到/usr/local/apache-maven-3.6.3

1
2
3
4
5
6
$ vi ~/.bash_profile
进入文件后添加下面两行
export MAVEN_HOME=/usr/local/apache-maven-3.6.3
export PATH=$PATH:$MAVEN_HOME/bin
添加后保存
$ source ~/.bash_profile

测试

测试

IDEA中配置

IDEA中配置

新建Maven项目

选择新建Maven项目

1

选择存放路径

2

查看项目结构

项目结构

新建存放非代码的文件夹resources

新建文件夹

添加第三方包的方法

第三方包仓库https://mvnrepository.com/

以解析json的第三方库fastjson为例

  1. 进入mvnrepository搜索想要使用的第三方包

  2. 找到需要使用的版本

  3. 拷贝xml内容

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>

第三方包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>learn_java_maven</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.72</version>
</dependency>
</dependencies>

</project>

fastjson

fastjson工具类,静态方法

静态方法不需要创建对象,直接用 类名点 调用

导入

1
import com.alibaba.fastjson.JSONObject;

json字符串转成java对象

  1. 待转化json
1
{"name": "张三","age": 18,"source": 100}
  1. 定义一个Student类来接收
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.zhongxin.json;

public class Student {
private String name;
private int age;
private int score;

public Student(String name, int age, int score) {
this.name = name;
this.age = age;
this.score = score;
}

public Student() {
}

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", score=" + score +
'}';
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getScore() {
return score;
}

public void setScore(int score) {
this.score = score;
}
}
  1. 转化主函数
1
2
3
4
5
6
7
8
9
10
11
12
package com.zhongxin.json;

import com.alibaba.fastjson.JSONObject;

public class Json {
public static void main(String[] args) {
String json = "{\"name\": \"张三\",\"age\": 18,\"source\": 100}";
Student s = JSONObject.parseObject(json, Student.class);
System.out.println(s);

}
}
  1. 输出:Student{name=’张三’, age=18, score=0}

运行提示

警告提示

pom.xml中添加如下内容:

1
2
3
4
5
6
7
8
9
10
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<!-- 文件拷贝时的编码 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 编译时的编码 -->
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<aspectj.version>1.9.2</aspectj.version>
</properties>

不再提示警告

java对象转化为json字符串

1
2
3
Student s2 = new Student("李四", 20, 90);
String json2 = JSONObject.toJSONString(s2);
System.out.println(json2);

输出{“age”:20,”name”:”李四”,”score”:90}

json字符串转Map

1
2
Map<String, Object> map = JSONObject.parseObject(json, Map.class);
System.out.println(map);

输出{name=张三, source=100, age=18}

map转json字符串

1
2
String json3 = JSONObject.toJSONString(map);
System.out.println(json3);

输出{“name”:”张三”,”source”:100,”age”:18}

json数组字符串转java对象

待转化:[{“name”: “张三”,”age”: 18},{“name”: “李四”,”age”: 16}]

定义Teacher

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
27
28
29
30
31
32
33
34
35
36
37
38
package com.zhongxin.json;

public class Teacher {
private String name;
private int age;

public Teacher() {
}

public Teacher(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}

public void setAge(int age) {
this.age = age;
}

public void setName(String name) {
this.name = name;
}
}

转化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.zhongxin.json;

import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class Json2 {
public static void main(String[] args) {
String json = "[{\"name\": \"张三\",\"age\": 18},{\"name\": \"李四\",\"age\": 16}]";

List<Teacher> list = JSONObject.parseArray(json, Teacher.class);
for (Teacher t : list) {
System.out.println(t);
}

}
}

输出内容:

Teacher{name=’张三’, age=18}
Teacher{name=’李四’, age=16}

Properties

使用Properties一般不使用Map接口的方法

Properties没有泛型,默认String

先要修改文件编码

修改编码

新建一个config.properties文件../learn_java_maven/src/test/resources/config.properties

写入配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.zhongxin.properties;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropDemo {
public static void main(String[] args) throws IOException {
Properties prop = new Properties();
prop.setProperty("USERNAME", "张三");
prop.setProperty("PASSWORD", "123456");
System.out.println(prop.getProperty("USERNAME"));
System.out.println(prop.getProperty("PASSWORD"));
FileOutputStream fos = new FileOutputStream("src/test/resources/config.properties");
prop.save(fos, "注释");
fos.close();
}
}

查看config.properties文件

1
2
3
4
#注释
#Sat Jul 18 10:05:03 CST 2020
USERNAME=张三
PASSWORD=123456

读取配置文件

1
2
3
FileInputStream fis = new FileInputStream("src/test/resources/config.properties");
prop.load(fis);
System.out.println(prop);

输出:{USERNAME=张三, PASSWORD=123456}

异常捕获

  1. throws
  2. try

throws

往外抛出异常

try

可以进行异常捕获

1
2
3
4
5
6
7
8
try{
// 可能异常的代码
}catch(Exception e){
// 捕获异常,出现异常的解决方案
}finally{
// 释放资源
// 无论是否异常,一定会执行的代码
}
 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!