Java 测试文件 LocalDate

Java testing file LocalDate

你好,我有一个人 class 和一个 PersonTest.java 来验证我的方法是否正常工作,我的一种方法确定并验证了人的年龄,但是我在测试中遇到错误class 表示 class Person 中的方法 getAge 不能应用于给定类型; 必填:本地日期 发现:没有参数

原因:实际和形式参数列表的长度不同

如有任何帮助,我们将不胜感激。下面是我的方法和测试方法。提前谢谢你

public int getAge(LocalDate dob)
    {
        LocalDate today = LocalDate.now();

        int age = Period.between(birthdate, today).getYears();
         if (age >= 14 && age <= 115) // valid employee dob
            this.birthdate = dob;
        else
            throw new IllegalArgumentException("the Person must be between 14-               115");    

        return age;
    }

------------------------ 这是我的测试方法---------------- ------

 public void testGetAge(int age, LocalDate dob) {
        System.out.println("getAge");
        int expResult = 16;
        int result = validPerson.getAge(); // heres the error
        assertEquals(expResult, result);``
    }

如我所见,getAge 方法获取 LocalDate 输入参数,但在您的测试 class 中您调用它时没有任何参数。这就是您收到错误的原因:

required: LocalDate found: no arguments

给它传递一个参数,或者让它使用Person年龄。