diff --git a/src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java b/src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java index edc519bc..c639b505 100644 --- a/src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java +++ b/src/main/java/pl/pojo/tester/internal/utils/MethodUtils.java @@ -16,7 +16,7 @@ public static Method findSetterFor(final Class clazz, final Field field) { return Arrays.stream(clazz.getMethods()) .filter(methodHasOnlyOneParameter()) .filter(areParameterAndFieldTypeAssignable(field)) - .filter(returnTypeIsVoid()) + .filter(returnTypeIsVoidOrClass()) .filter(method -> prefixMatchesSettersPrefixAndHasExpectedLength(method, field.getName())) .findAny() .orElseThrow(() -> new SetterNotFoundException(clazz, field)); @@ -31,8 +31,8 @@ public static Method findGetterFor(final Class clazz, final Field field) { .orElseThrow(() -> new GetterNotFoundException(clazz, field)); } - private static Predicate returnTypeIsVoid() { - return method -> method.getReturnType() == void.class; + private static Predicate returnTypeIsVoidOrClass() { + return method -> Arrays.asList(method.getDeclaringClass(), void.class).contains(method.getReturnType()); } private static Predicate areParameterAndFieldTypeAssignable(final Field field) { diff --git a/src/test/java/pl/pojo/tester/internal/tester/SetterTesterTest.java b/src/test/java/pl/pojo/tester/internal/tester/SetterTesterTest.java index a1b3e067..0b99de9b 100644 --- a/src/test/java/pl/pojo/tester/internal/tester/SetterTesterTest.java +++ b/src/test/java/pl/pojo/tester/internal/tester/SetterTesterTest.java @@ -47,7 +47,7 @@ void Should_Pass_All_Setter_Tests_Including_Fields() { // given final SetterTester setterTester = new SetterTester(); final Class clazz = BadPojoSetter.class; - final List includedFields = newArrayList("a", "b"); + final List includedFields = newArrayList("a", "b", "e"); // when final Throwable result = catchThrowable(() -> setterTester.test(clazz, FieldPredicate.include(includedFields))); @@ -76,6 +76,7 @@ private class BadPojoSetter { private int b; private int c; private int d; + private int e; public void setA(final int a) { this.a = a; @@ -88,6 +89,10 @@ public void setB(final int b) { public void setX(final char x) { } + public BadPojoSetter setE(int e) { + this.e = e; + return this; + } } @Setter @@ -97,7 +102,12 @@ private class GoodPojoSetter { private int b; private int c; private int d; + private int e; + public GoodPojoSetter setE(int e) { + this.e = e; + return this; + } } private class Setters { diff --git a/src/test/java/pl/pojo/tester/internal/utils/MethodUtilsTest.java b/src/test/java/pl/pojo/tester/internal/utils/MethodUtilsTest.java index 879b439f..925ff00b 100644 --- a/src/test/java/pl/pojo/tester/internal/utils/MethodUtilsTest.java +++ b/src/test/java/pl/pojo/tester/internal/utils/MethodUtilsTest.java @@ -211,6 +211,12 @@ private class Setters { private int otherId; private String name; private String otherName; + private Object object; + + public Setters setObject(Object object) { + this.object = object; + return this; + } public void setOtherId(final int otherId) { this.otherId = otherId;