Search code examples
javaunit-testingmethodsintlocaldate

Java testing file LocalDate


Hello I have a Person class and a PersonTest.java to validate that my methods work properly, one of my methods determines and validates the persons age, however I get an error in my testclass that says method getAge in class Person cannot be applied to given types; required: LocalDate found: no arguments

reason: actual and formal argument lists differ in length

Any help would be greatly appreciated. Underneath is my method and the test method. Thank you in advance

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;
    }

------------------------- Here is my testmethod ----------------------

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

Solution

  • As I see, the getAge method gets LocalDate input parameter, but in your test class you have called it without any parameter. That's why you get the error:

    required: LocalDate found: no arguments
    

    Just pass a parameter to it, or make it to use the Person age.