lombok&junit

460759461-zeze / 2024-07-08 / 原文

lombok&junit

1 lombok

  • 先去官网或者maven仓库下载jar包

https://mvnrepository.com/

  • 导入第三方包到项目中

  • 右键lib文件夹,点击add as library

  • 默认jvm不解析第三方注解,需要手动开启

  • 使用
//@Setter  // 生成set方法      1
//@Getter  // 生成get方法      2
//@ToString // 生成toString方法   3
//@EqualsAndHashCode // 生成equals和hashCode方法   4
//@NoArgsConstructor // 无参构造   5
//@AllArgsConstructor // 全参构造  6
@Data   // 相当于 1  2  3   4  5
//@AllArgsConstructor
@Accessors(chain = true)
public class Person {

    private String name;

    private int age;

    private String address;


}
  • 链式调用
public static void main(String[] args) {
        Person person = new Person().setName("小谷").setAddress("洛阳").setAge(18);
        System.out.println(person);
    }

2 Junit

Junit称之为单元测试

public class TestDemo {

    // 在每一个单元测试方法执行之前执行
    @Before
    public void before(){
        System.out.println("before...");
    }

    // 在每一个单元测试方法执行之后执行
    @After
    public void after(){
        System.out.println("after...");
    }

    @Test
    public void m(){
        System.out.println("HelloWorld");
    }

    // junit单元测试的方法要求"三无" : 无返回值  无参数 无静态
    @Test
    public void test(){
        System.out.println("test");
    }

    @Test
    public void test1(){
        // 测试驱动开发
        double discount = discount(100);
        // 断言
        assert discount == 90;

        double discount1 = discount(300);
        assert discount1 == 240;
    }

    public double discount(double money){
        if (money >= 100 && money < 300){
            money = money * 0.9;
        }else if (money >= 300 && money < 1000){
            money = money * 0.8;
        }else if (money >= 1000){
            money = money * 0.7;
        }
        return money;
    }
}