ocadotechnology / test-arranger

Arranges test data as fully populated objects
Apache License 2.0
23 stars 2 forks source link

JaCoCo code coverage is not working with test-arranger #33

Closed awbit closed 2 years ago

awbit commented 2 years ago

JaCoCo is not working with test-arranger site. There is an exception during setting value for boolean method e.q. setEnabled() Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 0 Too fix it please just filter out all synthetic fields in DecoratedObjectFactory

ReflectionUtils.getDeclaredFields(result).forEach(field -> { --> ReflectionUtils.getDeclaredFields(result).stream() .filter(field -> !field.isSynthetic()) .forEach(field -> {

Could you also create patch for version 1.4 ? Currently we still have Java 11 in project and we can't use most recent version.

Please see details on JaCoCo faq page https://www.jacoco.org/jacoco/trunk/doc/faq.html

My code uses reflection. Why does it fail when I execute it with JaCoCo?
To collect execution data JaCoCo instruments the classes under test which adds two members to the classes: A private static field $jacocoData and a private static method $jacocoInit(). Both members are marked as synthetic.

Please change your code to ignore synthetic members. This is a good practice anyways as also the Java compiler creates synthetic members in certain situation.

Best regards, Adrian Wójcik

mjureczko commented 2 years ago

Hi @awbit can you provide a test case to speed things up?

awbit commented 2 years ago

Thank you for fix.

mjureczko commented 2 years ago

@awbit the issue you found is in fact in the arranger's engine, i.e. easy-random. There is the following code:

            // inner classes (and static nested classes) have a field named "this$0" that references the enclosing class.
            // This field should be excluded
            if (type.getEnclosingClass() != null) {
                fields.removeIf(field -> field.getName().equals("this$0"));
            }

So that it filters out only some synthetic fields as not all of the are named this$0. If you like, you may report it in the easy-random project.

awbit commented 2 years ago

@mjureczko
We still have issue on our end. I am going to prepare test scenario for that case. I will come back with test to you. With proposed fix issue didn't occur: ReflectionUtils.getDeclaredFields(result).stream() .filter(field -> !field.isSynthetic()) For now seems that lombok + spock + jacoco could make a problem here - for now I couldn't confirm it yet

mjureczko commented 2 years ago

@awbit while I didn't manage to create a test case for the issue you described, I see no harm in adding the line you asked for. So it is there in versions 1.5.2 and 1.4.2.