File tree Expand file tree Collapse file tree
main/groovy/com/baeldung/metaprogramming/extension
test/groovy/com/baeldung/metaprogramming Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,10 +2,13 @@ package com.baeldung.metaprogramming.extension
22
33import com.baeldung.metaprogramming.Employee
44
5+ import java.time.LocalDate
6+ import java.time.Year
7+
58class BasicExtensions {
69
710 static int getYearOfBirth (Employee self ) {
8- return ( new Date ( ). getYear() + 1900 ) - self. age;
11+ return LocalDate . now( ). getYear() - self. age;
912 }
1013
1114 static String capitalize (String self ) {
Original file line number Diff line number Diff line change 11package com.baeldung.metaprogramming
22
3- import groovy.time.TimeCategory
3+
4+ import java.time.LocalDate
5+ import java.time.Period
6+ import java.time.Year
47
58class MetaprogrammingUnitTest extends GroovyTestCase {
69
@@ -57,8 +60,10 @@ class MetaprogrammingUnitTest extends GroovyTestCase {
5760 }
5861
5962 void testEmployeeExtension () {
60- Employee emp = new Employee (age : 28 )
61- assert emp. getYearOfBirth() == 1992
63+ def age = 28
64+ def expectedYearOfBirth = Year . now() - age
65+ Employee emp = new Employee (age : age)
66+ assert emp. getYearOfBirth() == expectedYearOfBirth. value
6267 }
6368
6469 void testJavaClassesExtensions () {
You can’t perform that action at this time.
0 commit comments