Answer the question
In order to leave comments, you need to log in
How to test private methods of a class?
There is a certain class, and the work of its public methods is based on the work of private auxiliary methods. Well let it be like this:
public class PersonDate
{
public PersonDate(Date birthday) { ... }
public int getYearsOld()
{
return isValid() ? getYear( getNextBirthdayDate() ) - getYear( getBornDate() ) + 1 : 0;
}
// (TODO: нужен тест) возвращает год для даты
private static int getYear( Date date ) { ... }
// (TODO: нужен тест) возвращает дату следующего дня рождения
private Date getNextBirthdayDate() { ... }
static final class DateUtils
{
public static int getYear( Date date ) { ... }
}
public class PersonDate
{
...
public static class PersonDateTest
{
@Test
public testGetNextBirthDate()
{
}
}
Answer the question
In order to leave comments, you need to log in
Colleagues, maybe someone knows other more beautiful ways?- through reflection! If you have the ability to change the scope, then you should not use it. If this is not possible, then here is a pattern for you:
Method method = PersonDate.class.getDeclaredMethod("getYear", Int.class);
method.setAccessible(true);
method.invoke(targetObject, argObjects);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question