Answer the question
In order to leave comments, you need to log in
How to perform Dependency Injection of local variables?
Good afternoon everyone!
Suppose we have a "library" function like this:
// Возвращает полный возраст человека на текущий день
public static int getYearsOld(Date birthday)
{
// Эту переменную необходимо мокировать
Date today = new Date();
return getYear(today) - getYear(birthday);
}
@Test
public void testGetYearsOld()
{
assertEquals( 23, getYearsOld(new SimpleDateFormat("dd-MM-yyyy").parse("15-07-1991")) );
}
public static int getYearsOld(Date birthday)
{
@InjectForTest(name = "Date1")
Date today = new Date();
return getYear(today) - getYear(birthday);
}
@Test
public void testGetYearsOld()
{
MockContext ctx = getContext();
ctx.put( "Date1", new Date() );
assertEquals( ..., getYearsOld(...) );
}
Answer the question
In order to leave comments, you need to log in
we make a mock for the Date class and that's it. an example can be seen here
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question